Modify next_posts_link( ); URL in Wordpress to include #hash extension

﹥>﹥吖頭↗ 提交于 2020-01-15 15:16:33

问题


Am looking for a good method to add a #hash to Wordpress Paged Navigation

The basic code, something like this

<nav id="<?php echo $html_id; ?>" class="navigation" role="navigation">
    <ul>
        <li class="previous">
             <?php next_posts_link( __( '<span class="meta-nav">Load more</span>', '' ) ); ?>
       </li>
  </ul>

Output

http://myurl.com/page/2/

Problem

Am running 3 individual queries on index page so i would need to add a #hash to the url to make it load the right content. Am looking for a built in function or other smart / light way of achieving a url for my next_posts_link( ); looking something like this

http://myurl.com/page/2/#hash

Have been looking around but most solutions gravitate around breaking it apart, modify and rebuild. It works well but am thinking there must be a better way!!

Any smart suggestions?


回答1:


You could try

add_filter('next_post_link', 'changemylink');

function changemylink($link) 
{
    //you can also do some checks here, like if you want it to only be replaced on specific page or for specific post type, etc
    return preg_replace('#(href=["\']?[^"\'>]+)(["\']?)#','$1#yourtag$2',$output)
}


来源:https://stackoverflow.com/questions/32296874/modify-next-posts-link-url-in-wordpress-to-include-hash-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!