迭代通过自定义后类型的自定义分类类型? (按类别排序WordPress的职位,或者通过分类项显示自定义后型)

StackOverflow https://stackoverflow.com/questions/3848608

  •  27-09-2019
  •  | 
  •  

我想有一个页面,显示所有帖子,相隔类别。这个想法是让类别,然后通过对每个类别的所有讯息迭代。该问题是由我希望通过给定的自定义类型的所有文章进行迭代,使用自定义分类的类别的事实复杂化。 (运行的WordPress 3)

在我的functions.php,我的自定义后类型被登记为“视频”和自定义分类为“video_types”。

在这是为了显示按类别排列的所有影片我的自定义页面模板,这是不返回任何职位代码(和它们的存在,我查了一下):

<?php 
  $categories = get_categories(array(
    'taxonomy' => 'video_types'
  )); 
  foreach ($categories as $cat):
?>
 <section id="<?php $cat->slug ?>" class="video-category">
     <?php
  query_posts(array(
      'cat' => $cat->cat_ID,
      'posts_per_page' => -1
         ));
     ?>
     <h2><?php single_cat_title(); ?></h2>
    <p class="description"><?php echo category_description($cat->cat_ID); ?></p>
  <?php while (have_posts()) : the_post(); ?>
      <?php
       $category = get_the_category(); 
            echo $category[0]->cat_name;
      ?>
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <article class="video">
        <h3><?php the_title(); ?></h3>
        <p>
          <?php the_content() ?>
        </p>
      </article>
  <?php endwhile; ?>
 </section>
<?php endforeach; ?>
有帮助吗?

解决方案

哎呀,一旦你弄清楚,一个自定义分类的各项目被称为项(在WordPress文档的菜鸟不是很明显),其所有简单得多搜索。该解决方案更容易理解没有所有的自定义查询的东西。

<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
  <section class="category-<?php echo $category ?>">
    <h2><?php echo $category->name; // Print the cat title ?></h2>
    <p class="description"><?php echo $category->description ?></p>
    <div class="<?php echo $category->post_type ?>-list">
      <?php
      //select posts in this category (term), and of a specified content type (post type) 
      $posts = get_posts(array(
        'post_type' => 'custom_post_type_name',
        'taxonomy' => $category->taxonomy,
        'term' => $category->slug,
        'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
      ));
      foreach($posts as $post): // begin cycle through posts of this category
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
      ?>
        // Now you can do things with the post and display it, like so
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
          <h3><?php the_title(); ?></h3>
          <?php 
            // Getting custom field data example
            echo get_post_meta($post->ID, 'field_key', true); 
          ?>
          <?php the_content() ?>
        </article>
      <?php endforeach; ?>
    </div>
  </section>
<?php endforeach; ?>

和然后在理解的任何间隙可通过搜索上述在WordPress抄本的功能被填充。在上面的代码中,我的特定的应用程序,custom_post_type_name将视频和taxonomy_name将video_type(或video_types,我忘了)。

其他提示

您可以尝试另一种方法。尝试使用get_posts让自己的帖子整理您的自定义分类,建立一个变量,它最初是一个空字符串(称为$ current_cat或东西),并与结果的每个循环,检查分类,并与$ current_cat比较 - 如果不同,打印出的报头为新的类别,然后该条目,如果相同,打印出的条目如常。

你的代码(我相信)一个明显的问题是,你无法正常查询您的自定义分类。你应该为你的查询中使用简单taxonomy_name => 'value',一个自定义分类不会被cat在查询感动。

让我知道如果你需要更多的细节。

编辑:更多细节!

// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";

$categories = $wpdb->get_results($querystr, OBJECT);

foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
    echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
    echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description

    $posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
    foreach($posts as $post) : // begin cycle through posts of this category
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)

        [ ... ] // do things with your post (display it)

    endforeach;

endforeach;

这应该这样做。 - 和可以是用于使用get_posts有帮助

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top