一般我们获取wordpress文章的上下篇都是用get_previous_post()与get_next_post()函数来获取,那么如何获取同分类下的上下篇文章呢,只需要给这俩函数加一个参数即可。
使用
get_previous_post(true) get_next_post(true)
即可,获取到的就是一个post对象。
这里获取到的是分类1 or 2的文章,如果你想获取分类1 and 2的文章,那么得这样。
// Create a new filtering function that will add our where clause to the query function date_filter_where( $where = '' ) { global $post; $where .= " AND post_date >= '".$post->post_date."'"; return $where; } //then create your own get previous post function which will take an array of categories eg: // $cats = array('1','2'); function my_get_previous_post($cats){ global $post; $temp = $post; $args = array( 'posts_per_page' => 1, 'post_type' => 'post', 'post_status' => 'publish', 'category__and' => $cats ); add_filter( 'posts_where','date_filter_where' ); $q = new WP_Query($args); remove_filter( 'posts_where','date_filter_where' ); while ($q->have_posts()){ $q->the_post; echo '<a href="https://www.mobantu.com/9405.html'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'; } $post = $temp; wp_reset_query(); }
使用$cats = array(‘1′,’2’); my_get_previous_post($cats);
原文链接:https://www.dqzy.cn/2025/04/16/398.html,转载请注明出处。
1、本站所有源码资源(包括源代码、软件、学习资料等)仅供研究学习以及参考等合法使用,请勿用于商业用途以及违法使用。如本站不慎侵犯您的版权请联系我们,我们将及时处理,并撤下相关内容!
2、访问本站的用户必须明白,本站对所提供下载的软件和程序代码不拥有任何权利,其版权归该软件和程序代码的合法拥有者所有,请用户在下载使用前必须详细阅读并遵守软件作者的“使用许可协议”,本站仅仅是一个学习交流的平台。
3、如下载的压缩包需要解压密码,若无特殊说明,那么文件的解压密码则为:www.dqzy.cn。
4、点启资源网是一个免费且专业分享网站源码、图片素材、特效代码、教程文章、站长工具的平台。我们努力给站长提供好的资源!
评论0