Wordpress:previous_post_link / next_post_link按字母顺序排列?

时间:2012-03-29 12:11:38

标签: php wordpress custom-post-type alphabetical

我正在开发一个带有自定义帖子的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,因此我可以浏览帖子。但是,这是通过帖子的时间顺序来完成的,我希望按照我在循环中使用的字母顺序进行操作。对此事的任何帮助将非常感激。 提前谢谢!

3 个答案:

答案 0 :(得分:6)

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>

答案 1 :(得分:4)

您可以使用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 );
}

然后在您的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,可以在添加过滤器部分添加if:

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

或者,您可以使用post_type特定的single.php文件!

这对我来说很有用,但是如果你打算将它与同一个词中的帖子结合起来可能会有一些限制......

答案 2 :(得分:0)

该插件多年未更新,不确定它是否仍然有效。如果没有,I came up with a solution可以添加到您的functions.php。希望它有所帮助!