如何在WordPress中对/订单get_next_post_link进行排序?

发布于 2025-01-26 19:51:46 字数 294 浏览 3 评论 0 原文

我正在使用 get_next_post_link 用于下一个和上一篇文章,但我不了解该顺序。 我想按ACF值(一个数字)对帖子进行排序,并通过上升订单。

我的代码:

<?php
    if ( get_next_post_link() ) {
        next_post_link();
     }
?>

我的ACF字段:“位置”及其独特的价值,因此我可以对其进行排序。

I'm using get_next_post_link for next and previous posts but I don't understand the order.
I would like to sort posts by acf value (a number) and order by ascending.

My code:

<?php
    if ( get_next_post_link() ) {
        next_post_link();
     }
?>

My acf field: 'position' and its unique value so I can sort it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-02-02 19:51:46

尝试像这样的somethink

$wp_query = WP_Query('orderby' => 'title', 'order'   => 'DESC');

      while ($wp_query->have_posts()) {
           $wp_query->the_post();
          
          
           get_next_post_link(); //this should work

           //if not here is alternative
           $next_post = get_next_post(); // or $wp_query->next_post();
           echo get_permalink( $next_post->ID );
       
}

如果您还有其他逻辑可以获取下一篇文章,则需要使用wp_query来获取它,然后您可以使用get_permalink($ id)

或者您可以覆盖默认操作,该操作在WordPress中获取帖子。

// # template-functions.php

function order_posts_by_title( $query ) { 

   if ( $query->is_home() && $query->is_main_query() ) { 

     $query-set( 'orderby', 'title' ); 

     $query-set( 'order', 'ASC' ); 

   } 

} 

add_action( 'pre_get_posts', 'order_posts_by_title' );
// now the get_posts(); and get_next_post(), get_next_post_link() will be ordered by custom query

Try somethink like this

https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

https://themeisle.com/blog/re-order-wordpress-blog-posts/

$wp_query = WP_Query('orderby' => 'title', 'order'   => 'DESC');

      while ($wp_query->have_posts()) {
           $wp_query->the_post();
          
          
           get_next_post_link(); //this should work

           //if not here is alternative
           $next_post = get_next_post(); // or $wp_query->next_post();
           echo get_permalink( $next_post->ID );
       
}

If you have any other logic to get the next post you will need to use wp_query to fetch it and then you can use get_permalink($id)

Or you can overwrite the default action which fetches posts in WordPress.

// # template-functions.php

function order_posts_by_title( $query ) { 

   if ( $query->is_home() && $query->is_main_query() ) { 

     $query-set( 'orderby', 'title' ); 

     $query-set( 'order', 'ASC' ); 

   } 

} 

add_action( 'pre_get_posts', 'order_posts_by_title' );
// now the get_posts(); and get_next_post(), get_next_post_link() will be ordered by custom query
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文