Wordpress single page template, custom post type, pagination orderby title - alphabetical order

I've scoured for QUITE a while, am looking for method to paginate on a single page template, orderby title (client

I've scoured for QUITE a while, am looking for method to paginate on a single page template, orderby title (client likes first name sorting of team members)

I assume this would include a way to offset the pagination by title or ?.

The following will paginate just fine by post order, not by 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>

How do you manually paginate/query against a custom post type, orderby => title, offset by the title of the post that's populating the single page template? Thank you for any help.

I've scoured for QUITE a while, am looking for method to paginate on a single page template, orderby title (client likes first name sorting of team members)

I assume this would include a way to offset the pagination by title or ?.

The following will paginate just fine by post order, not by 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>

How do you manually paginate/query against a custom post type, orderby => title, offset by the title of the post that's populating the single page template? Thank you for any help.

Share Improve this question edited Sep 11, 2019 at 14:17 user3460707 asked Sep 10, 2019 at 21:58 user3460707user3460707 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Try this Solution:

  1. For this you should create custom shortcodes. Please add this code in your theme's or child theme's functions.php file:
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.

  1. Then, add this shortcode in your Content Template to display Previous and Next post links:
[prev_post] | [next_post]

Expected Output:

< Previous : Post Name | Post Name : Next >

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745167765a4614737.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信