WordPress.org

Ready to get started?Download WordPress

Forums

adding anchor tag to the_permalink() (5 posts)

  1. llizard
    Member
    Posted 6 years ago #

    I have a food blog and am trying to make it easier for users who have searched the blog to go directly to the recipes that are on posts.

    Right now, on the search results page, I have the following:

    <a href="<?php the_permalink() ?>">Read the complete post</a> entitled &quot;<?php the_title(); ?>&quot;. (For posts that are in the &quot;recipes&quot; category, you can <a href="<?php the_permalink() ?>#recipe" title="recipe">go directly to the recipe</a>.)

    I have tried to change it to

    <a href="<?php the_permalink() ?>">Read the complete post</a> entitled &quot;<?php the_title(); ?>&quot;. 
    
    <?php if (in_category(33)) {
     echo ' (Go directly to <a href="' . the_permalink() . '#recipe" title="recipe">the recipe</a>.)';
    }
    else {
    echo '';
    }

    but the above echoes the permalink BEFORE the link coding rather than placing it directly after a href=":

    <a href="permalinktopost350">Read the complete post</a> entitled "post title". http://mydomain.com/wordpress/?p=350(Go directly to <a href="#recipe" title="recipe">the recipe</a>.)

    Any ideas where have I gone wrong?

    -ejm
    etherwork.net/blog/

    In an effort to keep spammers at bay, I have purposely left the http:// out.

    (I do hope that the fact that I am using wordpress 2.0.11 will not prevent people from answering.... Also, I have searched as best I could for this and the closest to any answers are at http://codex.wordpress.org/Template_Tags/in_category)

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Any ideas where have I gone wrong?

    Yes. the_permalink does its own echo, it does not return a string value.

    So when you do this:
    echo ' (Go directly to <a href="' . the_permalink() . '#recipe" title="recipe">the recipe</a>.)';

    What you're doing is building a string which you then echo. However, in building that string, you're calling the_permalink, which does it's own echo first. That won't work.

    This will:

    echo ' (Go directly to <a href="';
    the_permalink();
    echo '#recipe" title="recipe">the recipe</a>.)';

    Alternatively, there is a function called "get_permalink" which you can use if you need the permalink as a string. However, I recommend using the_permalink in this case, due to potential filter-induced differences.

  3. llizard
    Member
    Posted 6 years ago #

    That has indeed fixed the problem. Many thanks, Otto42!

  4. dijon
    Member
    Posted 6 years ago #

    Great post. I have a follow-up question: What if I want to populate a variable with the permalink? If I call the_permalink();, then I automatically get an echo... which I don't want. Here is what I'm trying to do (unsuccessfully):

    $podPressContent .= '<a href="';
    $podPressContent .= the_permalink();
    $podPressContent .= '" title="Entire Post">Entire Post</a>';

    Thanks for your help!
    John

  5. Joey Yax
    Member
    Posted 6 years ago #

    I know this is an old post but I was looking for something similar and came across this thread, so figured I would post the solution for future reference.

    If you use the get_permalink() function in place of the_permalink() it does not automatically echo the link.

    So, in this case...

    $podPressContent .= '<a href="';
    $podPressContent .= get_permalink();
    $podPressContent .= '" title="Entire Post">Entire Post';

    would work.

    --
    Joey

Topic Closed

This topic has been closed to new replies.

About this Topic