微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wordpress Loop - 不返回特定类别的项目

如何解决Wordpress Loop - 不返回特定类别的项目

我试图简单地将所有项目纳入特定类别(例如“网站”或“特色”)。

使用时:

$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');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。