简体   繁体   中英

Adding an anchor to previous_post_links and next_posts_links in WordPress

Struggling with this one...

In my WordPress theme I have a gallery at the top and blog posts below it. Each time I click the previous or next posts links it goes to the top which I don't want so I have created an anchor called #blog and placed in my html just below the gallery. How do add the anchor to the previous_post_links and next_posts_links to make it work? Is there a JavaScript solution for this?

I know this is an old question, but here is how I dealt with adding an anchor to the previous and next posts link. You would add this into functions.php between the opening and closing tags.

add_filter('get_pagenum_link', 'whatever_next_previous_anchor');

function whatever_next_previous_anchor($url) {
    return $url . '#blog';
}

Do the next and previous links have an ID or class that identifies them? Even if they don't you can still use Javascript or Jquery to append the #blog anchor to each link value. Check out this link Adding a parameter to the URL with JavaScript .

I think that should do it, by using get_previous_post() instead :

     $prev_post = get_previous_post();
     if (!empty( $prev_post )): ?>
      <a href="<?php echo get_permalink( $prev_post->ID ); ?>#blog">
        <?php echo $prev_post->post_title; ?></a>
     <?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM