为子类别创建存档页面

时间:2011-07-29 作者:Fatih Toprak

Example:

视频cat id=4视频cat儿童类别id=7,8,9新闻cat id=5新闻cat儿童类别id=11,12,13

我想在不同的档案中显示选定的类别(如视频和新闻)。php?

可以吗?谢谢。

(抱歉我的英语不好)

谢谢Chris S,但这不是我想做的。

  <?php
$catPosts = new WP_Query();
$catPosts->query( array( \'category__and\' => array(5,11,12,13), \'posts_per_page\' => 5, \'orderby\' => \'date\', \'order\' => \'DESC\' ) );
  while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
  <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <div class="meta">
   By <?php the_author() ?>
  </div>
<div class="content">
  <?php the_excerpt(); ?>
</div>
当我写入父类别id时,我不想写入其子类别id。当我选择父类别id时,系统应将其子类别id保存在同一存档页中。

3 个回复
最合适的回答,由SO网友:Mridul Aggarwal 整理而成

创建2页类别新闻。php&;类别视频。php&;将此代码放入其中。然后根据需要自定义两者的标记

<?php
$children = get_categories(\'child_of\'=>get_query_var(\'cat\'));
$cat = array(get_query_var(\'cat\'));
foreach($children as $child)
    $cat[] = $child->term_id;
$catPosts = new WP_Query( array( \'category__in\' => $cat, \'posts_per_page\' => 5, \'orderby\' => \'date\', \'order\' => \'DESC\' ) );
  while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
  <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <div class="meta">
   By <?php the_author() ?>
  </div>
<div class="content">
  <?php the_excerpt(); ?>
</div>

SO网友:its_me

查看Template Hierarchy 在WordPress中可能很有用。WordPress允许您根据类别slug为不同的类别使用不同的模板(category-{slug}.php) 或类别ID(category-{id}.php).

至于不同的帖子类型,你可以archive-{post_type}.php.

基本上,您可以简单地复制archive.php 并将其重命名为category-{slug}.php, category-{id}.phparchive-{post_type}.php (取决于最适合您的内容),然后根据您的需要修改模板中的现有代码。只是一个想法。

SO网友:chris_s

因此,您已经知道需要创建归档类别。每个类别的php模板。在每个模板中,您将为每个类别及其子类别调用自定义查询。例如:

<?php
$catPosts = new WP_Query();
$catPosts->query( array( \'category__in\' => array(5), \'posts_per_page\' => 5, \'orderby\' => \'date\', \'order\' => \'DESC\' ) );
  while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
  <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
  <div class="meta">
   By <?php the_author() ?>
  </div>
<div class="content">
  <?php the_excerpt(); ?>
</div>

这一个显示了“新闻”类别及其子类。请注意,您可以放置任何自己的标记来输出所需的HTML。

结束