凡科可以做社交网站吗,网站建设中html页面,杭州外贸网站,网站开发项目管理文档模板要在WordPress中调用分类ID为1、3、7的分类下#xff0c;具有相同标签的前10个内容#xff0c;可以使用自定义的WordPress查询(WP_Query)。以下是实现此功能的步骤和示例代码#xff1a;
步骤#xff1a;
确定共同标签#xff1a;
首先#xff0c;你需要确定分类1、3、…要在WordPress中调用分类ID为1、3、7的分类下具有相同标签的前10个内容可以使用自定义的WordPress查询(WP_Query)。以下是实现此功能的步骤和示例代码
步骤
确定共同标签
首先你需要确定分类1、3、7下所有文章共有的标签。这可以通过查询这些分类下的所有标签并找出交集来实现。
执行主查询
使用WP_Query来查询属于分类1、3、7且具有上述共同标签的文章并限制结果为10篇。
示例代码
?php
$post_num 10;
$specific_cat_ids array(1, 3, 7);// 获取当前文章的标签并转换为字符串
$tags ;
if ( get_the_tags() ) {$tags implode(-, array_map(function($tag) {return sanitize_title($tag-name);}, get_the_tags()));
}$myposts array();
foreach ( $specific_cat_ids as $catid ) {$args array(posts_per_page $post_num,category $catid,tag $tags,post__not_in array(get_the_ID()),suppress_filters false // 确保不会过滤掉任何帖子);$query new WP_Query($args);if ($query-have_posts()) {while ($query-have_posts()) {$query-the_post();$myposts[] $post;}}wp_reset_postdata(); // 重置后一个查询后的数据
}if ( !empty($myposts) ) {foreach ( $myposts as $post ) {setup_postdata($post);
?li classmedia mb-2 pb-2 styleborder-bottom:dashed 1px #f3f3f3;div classmedia-bodya href?php the_permalink() ? title?php the_title(); ?span classtext-customerbull;/span ?php the_title(); ?/a/div/li
?php}wp_reset_postdata(); // 重置全局$post数据
}
?
原文
http://wordpress-dulizhan.com/code/26.html