Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development The WordPress Loop Common WordPress Functions Used with the Loop

Eloise Adler
Eloise Adler
372 Points

Wordpress 'get_permalink' not working?

Hi,

I want to link the title of the blog post to the blog post url, in my index page.

I have used the following code: <h1><a href><?php get_permalink(); ?><?php the_title(); ?></a></h1>

However, this just links to the index page, that I am on. How can I get it to link to the page of the post?

Thanks, Eloise

2 Answers

Kenneth Kinsey
Kenneth Kinsey
14,897 Points

With the get_permalink() function, it defaults to the root if not provided a post. I recommend reading through the WP docs if you're ever having issues with something like this: https://developer.wordpress.org/reference/functions/get_permalink/

Also, the <a> tag is a little off if that's how you're using it, and the finished line should look something like this: <h1><a href="<?php echo get_permalink($post); ?>"><?php the_title(); ?></a></h1>

Or, if you're using it within a WP loop (which it looks like you are), you could instead just use the_permalink() like this: <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

The above should work at as well. I would "wrap" the a tag around the h1 and for the href use <?= get_post_permalink(); ?> that should also work. The main issue is wrapping the the h1 inside the anchor not the h1 wrapping inside the anchor.