【问题标题】:Wordpress Loop - Not returning projects in specific categoryWordpress Loop - 不返回特定类别的项目
【发布时间】:2021-01-31 21:29:41
【问题描述】:

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

使用时:

$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',
    '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></article></a>';
    }

wp_reset_postdata();

$output .= '</div>';

endif;

return $output; }

add_shortcode('gmb_project_section', 'gmb_register_project_section');

我使用的是 Divi 主题,带有一个代码模块,可让您输入短代码。

提前致谢, 本

【问题讨论】:

  • 您的代码是正确的。您确定 project 拼写和网站 category slug 正确吗?
  • 我确定! :/ - 谢谢
  • 请将'ignore_sticky_posts' =&gt; true, 添加到您的$arg 数组并再次测试。
  • 还是什么都没有! - 谢谢你的建议:)

标签: php wordpress divi


【解决方案1】:

试试这个兄弟:

$args = array(
'post_type'     => 'project',
'posts_per_page' => 10,
'tax_query' => array(
                array(
                    'taxonomy' => 'categories',
                    'field' => 'slug',
                    'terms' => 'websites',
                ),
            ),
 );

【讨论】:

    【解决方案2】:
    function gmb_register_project_section () { 
    
    $args = array(
        'post_type'     => 'project',
        'posts_per_page' => 10,
         'tax_query' => array(
                array(
                    'taxonomy' => 'your_taxonomy_name',  //Add your post type's taxonomy name
                    'field' => 'slug',
                    'terms' => 'websites',
                ),
            ),
    );
    
    $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');
    

    【讨论】:

      猜你喜欢
      • 2016-11-19
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 1970-01-01
      • 2015-09-21
      • 2011-11-18
      • 1970-01-01
      相关资源
      最近更新 更多