WordPress单页模板,自定义帖子类型,按标题分页顺序-字母顺序

时间:2019-09-10 作者:user3460707

我已经搜索了很长一段时间,正在寻找一种方法来在单个页面模板orderby title上分页(客户喜欢对团队成员的名字进行排序)

我假设这将包括一种通过标题或?来抵消分页的方法?。

以下内容将按邮政顺序分页,而不是按标题分页。

<div class="paginate">
<div class="paginate_left">
    <?php if( get_previous_post() ) : ?>
    <?php previous_post_link(\'%link \', \'<i class="fas fa-caret-square-left"></i>\') ?><?php previous_post_link(\'%link\') ?>
    <?php endif; ?>
</div>

<div class="paginate_right">
    <?php if( get_next_post() ) : ?>      
    <?php next_post_link(\'%link\') ?><?php next_post_link(\'%link \', \' <i class="fas fa-caret-square-right"></i>\') ?>
    <?php endif; ?>
</div>

如何根据自定义帖子类型orderby=>title手动分页/查询,并用填充单页模板的帖子标题进行偏移?谢谢你的帮助。

1 个回复
SO网友:Tanmay Patel

Try this Solution:

<为此,您应该创建自定义短代码。请在主题或子主题的函数中添加此代码。php文件:
add_shortcode( \'next_post\', \'next_shortcode\' );
function next_shortcode($atts) {
    global $post;
    ob_start();
    next_post_link( \'<span class="nav-next">%link : Next ></span>\', \'%title\' );
    $result = ob_get_contents();
    ob_end_clean();
    return $result;
}   
add_shortcode( \'prev_post\', \'prev_shortcode\' );
function prev_shortcode($atts) {
    global $post;
    ob_start();
    previous_post_link( \'<span class="nav-previous">< Previous : %link</span>\', \'%title\' );
    $result = ob_get_contents();
    ob_end_clean();
    return $result;
}
function my_custom_adjacent_post_where($sql) {
    if ( !is_main_query() || !is_singular() || \'your-custom-post-type-slug\' != get_post_type() )
        return $sql;
    $the_post = get_post( get_the_ID() );
    $patterns = array();
    $patterns[] = \'/post_date/\';
    $patterns[] = \'/\\\'[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\\'/\';
    $replacements = array();
    $replacements[] = \'post_title\';
    $replacements[] = $the_post->post_title;
    return preg_replace( $patterns, $replacements, $sql );
}
add_filter( \'get_next_post_where\', \'my_custom_adjacent_post_where\' );
add_filter( \'get_previous_post_where\', \'my_custom_adjacent_post_where\' );
function my_custom_adjacent_post_sort($sql) {
    if ( !is_main_query() || !is_singular() || \'your-custom-post-type-slug\' != get_post_type() )
        return $sql;
    $pattern = \'/post_date/\';
    $replacement = \'post_title\';
    return preg_replace( $pattern, $replacement, $sql );
}
add_filter( \'get_next_post_sort\', \'my_custom_adjacent_post_sort\' );
add_filter( \'get_previous_post_sort\', \'my_custom_adjacent_post_sort\' );

Whereas \'your-custom-post-type-slug\' is your custom post type slug, that you need to replace in the above code.

<然后,在内容模板中添加此短代码以显示上一篇和下一篇文章的链接:
[prev_post] | [next_post]

Expected Output:

< Previous : Post Name | Post Name : Next >

相关推荐

Show pagination in WP_Query

我正在编写一个foodblog,并试图在特定页面中显示收藏夹帖子列表,但我对分页有一些问题。我在前面的问题中尝试了不同的解决方案,但都没有解决问题。非常感谢 <?php $my_favs = get_user_meta(get_current_user_id(), \'user_favs\', true); $args = array( \'post_type\' => array(\'recipe\', \'post