php - WordPress 按类别名称获取帖子不起作用
问题描述
我需要按类别列出帖子顺序。我通过按类别显示帖子遇到问题没有人为我工作,例如“Category_name、tag_id、cat 等。”
$args1 = array(
'taxonomy'=> 'portfolio_cat',
'orderby' => 'slug',
'order' => 'ASC',
);
$cats = get_categories($args1);
foreach ($cats as $cat)
{
//echo $cat->slug;
$args2 = array(
'post_type' => 'advanced_portfolio',
'posts_per_page' => -1,
//'tag_id' => 25,
// 'category_name' =>$cat->slug,
'category_name' =>'video' //my category name **slug**
);
query_posts($args2);
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
}
解决方案
您正在使用自定义帖子类型,这意味着它不会有常规类别,而是会有一个用作类别的分类法,这意味着您需要使用税务查询,还需要检查条款,而不是经过测试但应该可以工作:
$taxonomy = 'portfolio_cat';
$cats = get_terms($taxonomy);
foreach ($cats as $cat) {
$args1 = array(
'post_type' => 'advanced_portfolio',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'portfolio_cat',
'field' => 'slug',
'terms' => 'video'
),
),
);
query_posts($args1);
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
}
wp_reset_query();
推荐阅读
- android - Android Studio / ADB 突然停止初始化/连接/工作
- django - 非调试模式下的 Django Whitenoise 500 服务器错误
- ios - Xcode - 在控制器中添加“分页标签”
- python - 仅显示嵌套字典的差异
- java - 如何在 ObservableList<> 中总结同名对象
- amazon-web-services - AWS API Gateway IP 流量限制 - 使用 DNS 而不是 IP 地址
- php - 多词值问题的聚合
- python - 如何将最大(或最小)适应度停止条件添加到多处理深度 Python 遗传算法
- python - 我想将随机数从它们的列表中更改为列表的最小含义
- pyspark-sql - 从大型数据集中删除重复列的函数