wordpress 上一篇文章链接/下一篇文章链接是否按字母顺序排列?

webghufk  于 2022-11-02  发布在  WordPress
关注(0)|答案(2)|浏览(189)

我正在开发一个wordpress网站与自定义职位是按字母顺序排列的循环。

<!-- THE ARGS -->
<?php global $query_string;
$args = wp_parse_args($query_string);
$args = array(
    'post_type' => 'custom_post',
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => -1,
    ); ?>

<!--  THE LOOP -->
<?php $wp_query = new WP_Query( $args ); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

没什么特别的,只是一个普通的循环。
当我打开一个帖子时,在它的页面上我通常会有previous_post_link和next_post_link,这样我就可以浏览帖子了。但是,这是按照帖子的时间顺序来做的,当然,我希望按照我在循环中使用的字母顺序来做。在这件事上的任何帮助都将是非常感谢的。提前感谢!

jdgnovmf

jdgnovmf1#

看起来this plug-in可以满足您的需求,例如:

<ul class="pager">
        <?php previous_post_link_plus( array('order_by' => 'post_title') ); ?>
        <?php next_post_link_plus( array('order_by' => 'post_title') ); ?>
</ul>
hm2xizp9

hm2xizp92#

您可以在get_adjacent_post函数中使用筛选器来执行此操作。
在functions.php文件中,添加:

function mytheme_previous_post_orderby_name($orderby){
    return "ORDER BY p.post_title DESC LIMIT 1";
}
function mytheme_previous_post_where_name(){
    global $post, $wpdb;
    return $wpdb->prepare( "WHERE p.post_title < %s AND p.post_type = %s AND ( p.post_status = 'publish' OR p.post_status = 'private' )", $post->post_title, $post->post_type );
}
function mytheme_next_post_orderby_name($orderby){
    return "ORDER BY p.post_title ASC LIMIT 1";
}
function mytheme_next_post_where_name(){
    global $post, $wpdb;
    return $wpdb->prepare( "WHERE p.post_title > %s AND p.post_type = %s AND ( p.post_status = 'publish' OR p.post_status = 'private' )", $post->post_title, $post->post_type );
}

然后在调用previous/next posts链接函数之前,在single.php页面中添加过滤器:

add_filter('get_previous_post_sort', 'mytheme_previous_post_orderby_name', 10, 1);
add_filter('get_next_post_sort', 'mytheme_next_post_orderby_name', 10, 1);

add_filter('get_previous_post_where', 'mytheme_previous_post_where_name', 10);
add_filter('get_next_post_where', 'mytheme_next_post_where_name', 10);

the_post_navigation();

remove_filter('get_previous_post_sort', 'mytheme_previous_post_orderby_name', 10);
remove_filter('get_next_post_sort', 'mytheme_next_post_orderby_name', 10);
remove_filter('get_previous_post_where', 'mytheme_previous_post_where_name', 10);
remove_filter('get_next_post_where', 'mytheme_next_post_where_name', 10);

如果要检查特定的post_type,可以在adding filter部分前后添加if:

if($post->post_type == 'my_custom_post_type'){
    add_filter(...);
    the_post_navigation();
    remove_filter(...);
}

或者,您可以只使用post_type特定的单个. php文件!
这对我来说效果很好,但如果你打算在同一个学期把这个和帖子结合起来,可能会有一些限制...

相关问题