问题描述
我试图简单地将所有项目纳入特定类别(例如“网站”或“特色”)。
使用时:
$args = array(
'post_type' => 'project','category_name' => 'websites','posts_per_page' => 10,);
没有返回任何东西。
简单使用时:
$args = array(
'post_type' => 'project',);
我确实得到了帖子(项目)的返回,只是不在我想要的类别中。
这是我的完整代码:
function gmb_register_project_section () {
$args = array(
'post_type' => 'project',);
$post_query = new WP_Query($args);
if ($post_query->have_posts()) :
$output = '<div class="gmb_custom-project-module">';
while ($post_query->have_posts()) {
$post_query->the_post();
$output .= '<a href="'. get_permalink(). '"><article class="gmb_project-item" style="background: url(\''. get_the_post_thumbnail_url(). '\') no-repeat center center;"><div class="inner"><h2>'.
get_the_title(). '</h2></div></article></a>';
}
wp_reset_postdata();
$output .= '</div>';
endif;
return $output; }
add_shortcode('gmb_project_section','gmb_register_project_section');
我使用的是 Divi 主题,带有一个代码模块,允许您输入短代码。
提前致谢, 本
解决方法
试试这个兄弟:
$args = array(
'post_type' => 'project','posts_per_page' => 10,'tax_query' => array(
array(
'taxonomy' => 'categories','field' => 'slug','terms' => 'websites',),);
,
function gmb_register_project_section () {
$args = array(
'post_type' => 'project','tax_query' => array(
array(
'taxonomy' => 'your_taxonomy_name',//Add your post type's taxonomy name
'field' => 'slug',);
$post_query = new WP_Query($args);
if ($post_query->have_posts()) :
$output = '<div class="gmb_custom-project-module">';
while ($post_query->have_posts()) {
$post_query->the_post();
$output .= '<a href="'. get_permalink(). '"><article class="gmb_project-item" style="background: url(\''. get_the_post_thumbnail_url(). '\') no-repeat center center;"><div class="inner"><h2>'.
get_the_title(). '</h2></div></article></a>';
}
wp_reset_postdata();
$output .= '</div>';
endif;
return $output; }
add_shortcode('gmb_project_section','gmb_register_project_section');