【问题标题】:List all posts in custom post type by taxonomy not working按分类列出自定义帖子类型中的所有帖子不起作用
【发布时间】:2020-05-28 20:15:48
【问题描述】:

我正在尝试按分类获取自定义帖子类型中所有帖子的列表,我在这段代码中被困了 3 天,现在我和父亲一起学习,他给了我一个提示,为什么我的代码不起作用一个说我有太多的参数我会告诉你代码我希望任何人都可以帮助我理解为什么它不工作,也许如果你真的用英文解释代码

print_r(Array(
    "1"=>"first",
    "2"=>"second"
    ));
// just try to remove args that you don't need 
//actually you need only one
$args = array(
    'tax_query' => array(
            'taxonomy' => 'your-custom-taxonomy',
            'field' => 'slug',
            'terms' => array( 'your-term' )
    ),
    'post_type' => 'your-post-type'
);
$loop = new WP_Query($args);
     if($loop->have_posts()) {
    $term = $wp_query->queried_object;
     while($loop->have_posts()) : $loop->the_post();
        //Output what you want      
   echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
}

【问题讨论】:

    标签: javascript php wordpress list


    【解决方案1】:
    1. 所以您有一个名为your-post-type 的自定义帖子类型
    2. 您有一个名为 your-custom-taxonomy 的自定义分类法,而您
    3. 想要获取具有名为your-term 的分类术语集的所有帖子。

    设置参数的方式正确。

    注意:如果您想获得所有自定义帖子类型的帖子,则不需要整个'tax_query'部分代码。

    我添加了一些 cmets 来描述代码在做什么:

    $args = array( // define your arguments for query
        'post_type' => 'your-post-type', // standard post type is 'post', you use a custom one
        'tax_query' => array( // you check for taxonomy field values
            array(
                'taxonomy' => 'your-custom-taxonomy', // standard is 'category' you use a custom one
                'field'    => 'slug', // you want to get the terms by its slug (could also use id)
                'terms'    => 'your-term', // this is the taxonomy term slug the post has set
            ),
        ),
    );
    $loop = new WP_Query( $args ); // get post objects
    
    // The Loop
    if ( $loop ->have_posts() ) { // check if you received post objects
        echo "<ul>"; // open unordered list
        while ( $loop ->have_posts() ) { // loop through post objects
            $loop ->the_post();
                echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; // list items
        }
        echo "</ul>"; // close unordered list
        /* Restore original Post Data */
        wp_reset_postdata(); // reset to avoid conflicts
    } else {
        // no posts found
    }
    

    希望这会有所帮助!


    编辑:如果您不知道如何使用 WP_Query

    此代码将使您的 wordpress 帖子按标题排序,并输出标题和内容。将其放入主题的模板文件中(了解有关模板文件的知识:https://developer.wordpress.org/themes/basics/template-hierarchy/)。

    <?php 
    
    $args = array(
     'post_type' => 'post',
     'posts_per_page' => -1, // limit the number of posts if you like to
     'orderby' => 'title',
     'order' => 'ASC'
    );
    
    $custom_query = new WP_Query($args); 
    
    if ($custom_query->have_posts()) : while($custom_query->have_posts()) : $custom_query->the_post(); ?> 
      <h1><?php the_title(); ?></h1> 
      <?php the_content();?>
    <?php endwhile; else : ?> 
      <p>No posts</p>
    <?php endif; wp_reset_postdata(); ?>
    

    您说过要使用自定义帖子类型并进行分类查询。因此,您可以通过更改 $args 中的参数来调整它。

    【讨论】:

    • 可以说我希望列表是这样的标题将是画廊然后 1-3 将发布 1 发布 2 发布 3(这是我的自定义帖子类型的名称
    • 每当我尝试加载网站、我的代码和您在此处发布的代码时,它都会给我一个错误
    • 我不明白你在写什么,对不起。 “发布”是在 wordpress 中发布帖子的正常方式。您可以添加自定义帖子类型,例如在商店中可能是“产品”。如果您有一个名为“画廊”的类别术语,您可以在参数“分类”中添加category,并通过将your_term 替换为galleries 来检查该术语
    • 我编辑了我的答案,向您展示了如何在 wordpress 中获取帖子并一一显示标题和内容的基础知识。希望它有助于解决问题!
    • 当我加载我的网站时我仍然会出错,我不知道我是 wordpress 的新手
    猜你喜欢
    • 2015-03-15
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多