WordPress pagination - Adding an Anchor link

2019-08-01 04:48发布

I have this anchor link in my index.php:

<a name="blog"></a>

I would like this anchor link to work When next_posts_link and previous_posts_link are clicked on so it doesn't go all the way to the top of the page. I have no idea how to go about doing this for WordPress pagination though.

Here is my code for the pagination:

<div class="pagenavi">
<?php if( function_exists( 'wp_pagenavi ' ) ) {
wp_pagenavi();
} else {
next_posts_link ('<div class="arrow-back"></div>'); }
previous_posts_link('<div class="arrow-forward"></div>') ; }

2条回答
混吃等死
2楼-- · 2019-08-01 05:24

unfortunately the wp_pagenavi function seems to encapsulate the complete link building.

If you can't configure a generic postfix in your wordpress or plugin configuration I don't recommend to change Wordpress code.

Instead you could execute a small JavaScript which moves the browser page to the anchor on load.

This example uses jQuery. Paste it into a bottom location of your template.

<script type="text/javascript">
    $(document).ready(function() {
        $('html, body').scrollTop( 
            $('[name="blog"]').offset().top 
        );
    });
</script>

Couldn't test this but should work. Even simple scripts are the hell to write on a iPhone keyboard..

EDIT: Works like a charm, check this out for a demo http://jsfiddle.net/mfeldheim/EkMfP/7/

查看更多
【Aperson】
3楼-- · 2019-08-01 05:48

Solution 2:

Adding anchor tag #blog to the prev/next links in a blogpost using jQuery

<script type="text/javascript">
$(document).ready(function() {
    $('.pagenavi a').each(function(i,a){$(a).attr('href',$(a).attr('href')+'#blog')});
});
</script>

Demo: http://jsfiddle.net/mfeldheim/EkMfP/12/

查看更多
登录 后发表回答