最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress Loop - Not returning projects in specific category

programmeradmin1浏览0评论

I am attempting to simply pull in all projects in a specific category (e.g. 'websites' or 'featured').

When using:

$args = array(
'post_type'     => 'project',
'category_name'  => 'websites',
'posts_per_page' => 10, 
);

Nothing is returned.

When using simply:

$args = array(
'post_type'     => 'project',
);

I do actually get posts (projects) returning, just not in the category I want.

Here is my full code:

function gmb_register_project_section () { 

$args = array(
    'post_type'     => 'project',
    'category_name'  => 'websites',
    'posts_per_page' => 10,
);

$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 class="gmb_arrow-icon"><?xml version="1.0" encoding="iso-8859-1"?><svg version="1.1" id="Capa_1" xmlns="; xmlns:xlink="; x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M506.134,241.843c-0.006-0.006-0.011-0.013-0.018-0.019l-104.504-104c-7.829-7.791-20.492-7.762-28.285,0.068 c-7.792,7.829-7.762,20.492,0.067,28.284L443.558,236H20c-11.046,0-20,8.954-20,20c0,11.046,8.954,20,20,20h423.557           l-70.162,69.824c-7.829,7.792-7.859,20.455-0.067,28.284c7.793,7.831,20.457,7.858,28.285,0.068l104.504-104 c0.006-0.006,0.011-0.013,0.018-0.019C513.968,262.339,513.943,249.635,506.134,241.843z"/></g></g></svg></div></div></article></a>';
    }

wp_reset_postdata();

$output .= '</div>';

endif;

return $output;

}
// Register shortcode
add_shortcode('gmb_project_section', 'gmb_register_project_section');

I am using the Divi theme, with a code module which allows you to input short codes into.

Thanks in advance, Ben

I am attempting to simply pull in all projects in a specific category (e.g. 'websites' or 'featured').

When using:

$args = array(
'post_type'     => 'project',
'category_name'  => 'websites',
'posts_per_page' => 10, 
);

Nothing is returned.

When using simply:

$args = array(
'post_type'     => 'project',
);

I do actually get posts (projects) returning, just not in the category I want.

Here is my full code:

function gmb_register_project_section () { 

$args = array(
    'post_type'     => 'project',
    'category_name'  => 'websites',
    'posts_per_page' => 10,
);

$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 class="gmb_arrow-icon"><?xml version="1.0" encoding="iso-8859-1"?><svg version="1.1" id="Capa_1" xmlns="http://www.w3/2000/svg" xmlns:xlink="http://www.w3/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"><g><g><path d="M506.134,241.843c-0.006-0.006-0.011-0.013-0.018-0.019l-104.504-104c-7.829-7.791-20.492-7.762-28.285,0.068 c-7.792,7.829-7.762,20.492,0.067,28.284L443.558,236H20c-11.046,0-20,8.954-20,20c0,11.046,8.954,20,20,20h423.557           l-70.162,69.824c-7.829,7.792-7.859,20.455-0.067,28.284c7.793,7.831,20.457,7.858,28.285,0.068l104.504-104 c0.006-0.006,0.011-0.013,0.018-0.019C513.968,262.339,513.943,249.635,506.134,241.843z"/></g></g></svg></div></div></article></a>';
    }

wp_reset_postdata();

$output .= '</div>';

endif;

return $output;

}
// Register shortcode
add_shortcode('gmb_project_section', 'gmb_register_project_section');

I am using the Divi theme, with a code module which allows you to input short codes into.

Thanks in advance, Ben

Share Improve this question asked Jan 31, 2021 at 22:21 benelwoodsbenelwoods 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

You're sure that you have actual posts (project post type), that have that category selected on them, right?

Make sure the posts have an actual category selected, not a tag. Also check in the categories configuration to make sure that websites is the actual slug of the category they are assigned to.

You can also try using the actual category id (find it while editing the category in admin area, in the URL bar), using this instead: 'cat' => '1,2,3,4'

or try 'category__in' => 4

I suspect the issue is probably with incorrect slug or some other configuration

https://developer.wordpress/reference/classes/wp_query/#category-parameters

You can also try using a direct tax query:

$args = array(
    'post_type'     => 'project',
    'tax_query' => array(
        array(
            'taxonomy'         => 'category',
            'terms'            => 'websites',
            'field'            => 'slug',
            'include_children' => true,
        )
    )
);
发布评论

评论列表(0)

  1. 暂无评论