• Resolved CCeliaS

    (@ccelias)


    Hi all,

    I’m stuck. I’m working on a site that has a Parallax Banner that takes up most of the screen height.

    I’m trying to modify the function that creates my “read more” links after my excerpts in my Recent Posts to link to the #content id in my single.php (opening the page after the tall banner).

    I’m using the suggestion from the Codex on Customizing the Read More, (see below) but I don’t know enough .php to be able to insert the anchor tag (#content) that I’d like to postpend to the get_permalink without breaking the code.

    // Replaces the excerpt "Read More" text by a link
    function new_excerpt_more($more) {
           global $post;
    	return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read More...</a>';
    }
    add_filter('excerpt_more', 'new_excerpt_more');

    https://sottovocepress.com/brian/ and https://sottovocepress.com/brian/ashley-madison-hackers-come-together-to-fight-marital-infidelity/

    Thoughts?

    Many thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I think this is what you’re after:
    return '<a class="moretag" href="'. get_permalink($post->ID) . '#content"> Read More...</a>';

    Thread Starter CCeliaS

    (@ccelias)

    bcworkz! Thank you!

    That’s it!

    Many, many thanks! I had the single quote in the wrong place (actually several wrong places in different attempts). I can’t tell you what a relief it is to have it working!

    Many, many thanks!

    Moderator bcworkz

    (@bcworkz)

    You’re most welcome!

    It’s all too easy to get lost in quotes with this kind of thing. Even though it involves an extra step, I like to take advantage of PHP’s variable expansion feature within double quotes just to make things easier to read.

    $link = get_permalink($post->ID);
    return "<a class='moretag' href='$link#content'> Read More...</a>";

    If you dislike single quotes in the resulting HTML even though it validates just fine, you can use escaped double quotes.

    $link = get_permalink($post->ID);
    return "<a class=\"moretag\" href=\"$link#content\"> Read More...</a>";

    If PHP (or you) has trouble parsing what constitutes a variable (and with object properties or array elements), use curly braces to delineate the variable.
    return "<a class='moretag' href='{$link}#content'> Read More...</a>";

    YMMV, but it works for me ??
    https://php.net/manual/en/language.types.string.php#language.types.string.parsing

    Thread Starter CCeliaS

    (@ccelias)

    Vuderbar!

    Thank you for the tip and the link!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How do I postpend an anchor tag to get_permalink in this function?’ is closed to new replies.