简体   繁体   English

按分类法列出自定义帖子类型中的所有帖子不起作用

[英]List all posts in custom post type by taxonomy not working

I am trying to get a list of all the posts in custom post type by taxonomy, I am stuck 3 days at this code, now i study with my father and he gave me a hint why my code is'nt working he a said i have too many args i will show you the code i hope anyone can help me understand why its not working and maybe if you really kind a explanation of the code in english我正在尝试按分类法获取自定义帖子类型中所有帖子的列表,我在这段代码上停留了 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;
}
  1. So you have a custom post type called your-post-type所以你有一个名为your-post-type的自定义帖子类型
  2. and you have a custom taxonomy called your-custom-taxonomy and you并且您有一个名为your-custom-taxonomy ,并且您
  3. want to get all the posts with a taxonomy term set called your-term .想要获得所有带有名为your-term的分类术语集的帖子。

You are doing it the right way with setting your arguments.您设置 arguments 的方式是正确的。

Notice : If you want to get all the posts of a custom post type, you do not need the whole 'tax_query' part of code.注意:如果您想获取自定义帖子类型的所有帖子,则不需要整个代码'tax_query'部分。

I added some comments to describe what the code is doing:我添加了一些注释来描述代码的作用:

$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
}

Hope this helps!希望这可以帮助!


EDIT: If you do not know how to use WP_Query编辑:如果您不知道如何使用 WP_Query

This code will get your wordpress posts ordered by their titles and output title and content.此代码将使您的 wordpress 帖子按标题和 output 标题和内容排序。 Put this inside a template file of your theme (learn something about template files: https://developer.wordpress.org/themes/basics/template-hierarchy/ ).将其放入主题的模板文件中(了解有关模板文件的一些信息: 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(); ?>

You said you want to use a custom post type and do a taxonomy query.你说你想使用自定义帖子类型并进行分类查询。 So you can adjust this with changing the arguments in $args .因此,您可以通过更改$args中的 arguments 来调整它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM