Php wordpress中的相关帖子类别(分类法)
如何使用此代码只按类别显示相关帖子? 此代码位于single.php中 我没有找到解决方案,但找不到解决方案。有人能帮我吗Php wordpress中的相关帖子类别(分类法),php,wordpress,Php,Wordpress,如何使用此代码只按类别显示相关帖子? 此代码位于single.php中 我没有找到解决方案,但找不到解决方案。有人能帮我吗 <?php $postsPerPageq = 5; $allargw = array( 'post_type' => 'audioplayer', 'posts_per_page' => $postsPerPageq, 'or
<?php
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => 'top-audio'
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();
?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
<?php endwhile; wp_reset_postdata();?>
在allargw数组值中添加类别名称
<?php
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'category_name'=>'Your category name',
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => 'top-audio'
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post();
?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
您可以尝试此操作。如果要按简单帖子类别检索帖子,请检查以下代码:
<?php
$category = get_the_category($post->ID);
$cat_id = $category[0]->term_id;
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'cat' => $cat_id
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
<?php endwhile; wp_reset_postdata();?>
否则,如果要按自定义分类法检索帖子,请检查以下代码:
<?php
$term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all"));
$term_id = $term_list[0]->term_id ;
$postsPerPageq = 5;
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'id',
'terms' => $term_id
)
)
);
$topaudio = new WP_Query($allargw);
while ( $topaudio->have_posts() ) : $topaudio->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3>
<?php endwhile; wp_reset_postdata();?>
希望,这将对您有所帮助。首先,您需要使用此功能获取当前的帖子术语
get_the_terms ( get_the_id(), 'your_custom_taxonomy' );
然后在WP_查询中获取用于tax_查询的术语slug
$postsPerPageq = 5;
$terms_slugs = array();
//get the current post terms slug
$terms = get_the_terms( get_the_id(), 'audiotop' );
foreach ( $terms as $term) {
$terms_slugs[] = $term->slug;
}
//WP_Query args
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => $terms_slugs,
)
)
);
我希望它能解决您的问题。请查看:-或您好,但如何自动添加类别,示例:用户访问类别1的帖子。然后,相关帖子将仅显示类别1帖子。第2类、第3类等也一样。实际上,您希望自动插入帖子和类别,对吗?您好,但是如何自动添加类别,示例:用户访问类别1的帖子。然后,在相关帖子中,只会自动显示第1类帖子。第2类、第3类等也一样。请澄清一件事:每个帖子只分配给一个类别:。即。post1->category1,post2->category2,post3->category1,post4->category1->category2,post2->category3,category1,不是这样的:post1->category1,category2,post2->category3,category1,否则很难识别。如果每个帖子都被分配到一个类别,那么找到我的更新代码。
$postsPerPageq = 5;
$terms_slugs = array();
//get the current post terms slug
$terms = get_the_terms( get_the_id(), 'audiotop' );
foreach ( $terms as $term) {
$terms_slugs[] = $term->slug;
}
//WP_Query args
$allargw = array(
'post_type' => 'audioplayer',
'posts_per_page' => $postsPerPageq,
'orderby' => 'title',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'audiotop',
'field' => 'slug',
'terms' => $terms_slugs,
)
)
);