Php 如何显示自定义帖子类型的所有帖子类别
我在一个学院的网站上工作,该网站提供课程。 我想将所有帖子显示为自定义帖子类型的列表类别。 例如: 类别名称1Php 如何显示自定义帖子类型的所有帖子类别,php,wordpress,Php,Wordpress,我在一个学院的网站上工作,该网站提供课程。 我想将所有帖子显示为自定义帖子类型的列表类别。 例如: 类别名称1 -类别名称1的帖子 -类别名称1的帖子 类别名称2 -类别名称2的帖子 -类别名称2的帖子 等等 下面是代码,显示所有类别的自定义帖子类型的所有帖子,但我想单独显示它们,请帮助我 <?php $type = 'course'; $args=array( 'post_type' => $type, 'post_status' => 'publish', 'p
-类别名称1的帖子
-类别名称1的帖子
类别名称2
-类别名称2的帖子
-类别名称2的帖子
等等 下面是代码,显示所有类别的自定义帖子类型的所有帖子,但我想单独显示它们,请帮助我
<?php
$type = 'course';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1);
$my_query = '';
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();
?>
您可能需要多次执行查询,或者每个类别执行一次查询。关键字是在查询中添加'category\u name'=>'slug\u name'
<?php
// query category 1
$type = 'course';
$args1=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
'caller_get_posts'=> 1);
// query category 2
$type = 'course';
$args2=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
'caller_get_posts'=> 1);
$my_query = '';
$my_query = new WP_Query($args1);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();
$my_query = '';
$my_query = new WP_Query($args2);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();
?>
100%使用此代码
$tax_query[] = array(
'taxonomy' => '< taxonomy_slug >',
'field' => 'term_id',
'terms' => < terms_ids >, // array(12,13,...)
);
$args = array(
'post_type' => '< post type name>',
'posts_per_page' => 10,
'tax_query' => $tax_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo get_the_ID();
endwhile;
$tax\u query[]=数组(
“分类法”=>“”,
'field'=>'term_id',
'terms'=>,//数组(12,13,…)
);
$args=数组(
“post_type'=>”,
“每页帖子数”=>10,
“tax\u query”=>$tax\u query
);
$loop=新的WP_查询($args);
而($loop->have_posts()):$loop->the_post();
echo获取_ID();
结束时;
@X Adams它不起作用,我们不必每次添加类别时都手动添加它。类别的名称是“slug”,尽管您可以编写脚本为每个slug创建代码。这不是现在的问题,我认为你不能在结果中按类别名称查询对吗?