• Resolved SaladGoat

    (@saladgoat)


    I have a child theme for twenty-eleven and I am using the following for pagination navigation in single.php:

    <div id="cooler-navnav-below" class="navigation">
    <?php $prevPost = get_previous_post(true);
    if($prevPost) {?>
    <div class="nav-box previous">
    <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) ); ?>
    <?php previous_post_link('%link',"$prevthumbnail  <p>%title</p>", TRUE); ?>
    </div>
    
    <?php } $nextPost = get_next_post(true);
    if($nextPost) { ?>
    <div class="nav-box next" style="float:right;">
    <?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?>
    <?php next_post_link('%link',"$nextthumbnail  <p>%title</p>", TRUE); ?>
    </div>
    <?php } ?>
    </div><!--#cooler-nav div -->
    

    It’s pretty sweet, but I need to add two things: one is to put the post’s excerpt below the title. But doing
    <p>%title<br>%excerpt</p>
    doesn’t work.
    So how can I do that?

    Also, my page has <base target=”_blank”> but I want the next/previous links to open in the same window, so how would I define that? Obviously %link is the <a> tag, but how can I enter target=”_self”?

    Thanks!

    • This topic was modified 4 years, 7 months ago by SaladGoat.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    %link, %title, and %date are the only placeholders defined. You don’t really need any others for single.php links since you can insert arbitrary content similar to $prevthumbnail. In a similar vein, assign the return from get_the_excerpt() to $excerpt and include $excerpt in the link format passed to *_post_link().

    The target attribute does not normally appear in these links, it has been added by your theme or a plugin. First verify that there is not a setting somewhere to turn the open in new tab functionality off. The attribute is likely added through “previous_post_link” and “next_post_link” filters. If you can determine how the filter hook was added, you could remove it in a subsequent action. If all else fails, hook the above filters with a large priority argument (so your code run=s last) and search/replace the offending attribute value.

    Thread Starter SaladGoat

    (@saladgoat)

    Thanks for the first part. Didn’t make much sense on first reading, but once I started putting it in the code, I got it. And it works as hoped, so thanks for that!

    The second part, about the target, I think I must have said it wrong. I *want* the base target for the rest of the page to be _blank. I’m the one who put that in the header. But I want the previous/next links to open in the same tab/window. And I am hoping there is a way to add target="_self" into the %link, somehow.

    Having now looked at the codex for that value, I think it may not be possible, but I understand very little of this code, so thought I would ask again, if it is possible, and if so, how do I do it?

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    Ah, I was confused about your desire with target attribute, apologies. Use get_next_post_link() and get_previous_post_link() instead. They return a link instead of echoing. Use str_replace() to insert the desired attribute into the returned link, then echo out the result.

    Thread Starter SaladGoat

    (@saladgoat)

    I’m sorry, I am completely lost now. I’m very new to php. I got this code from a website, so I only just barely understand what each bit of code does.
    Adding ‘get’ to the beginning of the lines shows nothing and I am unclear on what string I am meant to replace.
    And that’s after over an hour of googling and trying different things. Whatever they are telling me, it’s not sinking in.
    I really appreciate your sticking with me and hope you can further guide me to what I need to do.
    Thank you!

    Moderator bcworkz

    (@bcworkz)

    I’m sorry I didn’t give you enough information to go on. It’s difficult to assess others skill levels in a forum like this. I appreciate your efforts to try and figure it out on your own. Do something like this:

    <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) ); ?>
    <?php $link = get_previous_post_link('%link',"$prevthumbnail  <p>%title</p>", TRUE); ?>
    <?php echo str_replace('href', 'target="_self" href', $link ); ?>

    use this to replace the two existing lines similar to the first two here. Same idea applies for the next post link.

    Thread Starter SaladGoat

    (@saladgoat)

    I was so close! I just couldn’t figure out the $link part. I guess that’s pretty much the whole thing, though sooo…. lol

    Yes, this works perfectly.

    Thank you so much for your patience and your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘link target and excerpts needed’ is closed to new replies.