从顶级类别获取帖子,并按子类别分组

时间:2013-09-18 作者:marxslope

我有一个名为“Membres”(英语成员)的CPT。然后我创建了一个名为“Membres”的类别和他的子类别。

Parent category "Membres"
 - Subcategory1 (3 post inside)
 - Subcategory2 (4 post inside)
 - Subcategory3 (1 post inside)
我需要的解决方案是生成一种循环(在循环之外),但生成我之前定义的类别的归档文件的输出(调用函数)。这将是静态页面模板的一部分。我想获得的方案是:

<h1>Title "Membres"(calls the title of the page, working)</h1>

<section>
 - <h2><a>Name of "Subcategory1"</a></h2>
  - <p>Description of "Subcategory1"</p>
   - <posts Subcategory1>
</section>

<section>
- <h2><a>Name of "Subcategory2"</a></h2>
  - <p>Description of "Subcategory2"</p>
   - <posts Subcategory2>
</section>

<section>
 - <h2><a>Name of "Subcategory3"</a></h2>
  - <p>Description of "Subcategory3"</p>
    - <posts Subcategory3>
</section>
元素有问题:<;h2><a>Name of "Subcategory#"</a></h2>. 我需要它作为子类别每个部分的标题(因此也可以作为类别本身的链接)。现在,我获得“类别名称”的次数与每个类别中的帖子数量一样多,但没有类别名称,只有一次。我试着到处寻找解决方案,但目前没有任何结果。我还想继续使用这个方法,不想把东西移到分类法。

wordpress中的CPT设置功能。php:

// Register Custom Post Type
function custom_post_type() {

    $labels = array(
        \'name\'                => _x( \'Membres\', \'Post Type General Name\', \'mem\' ),
        \'singular_name\'       => _x( \'Membre\', \'Post Type Singular Name\', \'mem\' ),
        \'menu_name\'           => __( \'Membres\', \'mem\' ),
        \'parent_item_colon\'   => __( \'Membre principal\', \'mem\' ),
        \'all_items\'           => __( \'Tots els membres\', \'mem\' ),
        \'view_item\'           => __( \'Veure membre\', \'mem\' ),
        \'add_new_item\'        => __( \'Afegir membre\', \'mem\' ),
        \'add_new\'             => __( \'Nou membre\', \'mem\' ),
        \'edit_item\'           => __( \'Editar membre\', \'mem\' ),
        \'update_item\'         => __( \'Actualitzar membre\', \'mem\' ),
        \'search_items\'        => __( \'Cercar membres\', \'mem\' ),
        \'not_found\'           => __( \'No hi ha membres\', \'mem\' ),
        \'not_found_in_trash\'  => __( \'No hi ha membres a la paperera\', \'mem\' ),
    );
    $rewrite =<h2><?php arraysingle_cat_title(
        \'slug\'                => \'membre\',
        \'with_front\'          => true,
        \'pages\'               => true,
        \'feeds\'               => true,
    \'\');
    $args = array(
        \'label\'               => __( \'membres\', \'mem\' ),
        \'description\'         => __( \'Membres\', \'mem\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'trackbacks\', \'revisions\', \'custom-fields\', \'page-attributes\', \'post-formats\', ),
        \'taxonomies\'          => array( \'category\', \'post_tag\' ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'menu_icon\'           => get_bloginfo(\'template_directory\').\'/img/iconos?></users.png\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'post\',
    );h2>
  <p><?php echo register_post_typecategory_description( \'membres\', $args );

}

// Hook into the \'init\' action
add_actionget_category_by_slug( \'init\', \'custom_post_type\', 0 \'category_name_slug\');
我用于存档一个子类别的代码部分:

<?php query_posts(array( 
          \'post_type\' => \'membres\',
          \'category_name\' => \'investigadors\',
          \'showposts\' => 20,
         ->term_id )); ?>
    ></p>


      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<!-- PART TO SOLVE-->   
<h2><a>Name of "Subcategory3" Wordpress function PHP</a></h2>
<p>Description of "Subcategory3" PHP"category_description"</p>
<!-- PART TO SOLVE-->   

          <?php include (TEMPLATEPATH . \'/membresmem.php\'); ?>
           <?php endwhile; else: ?>
           <p>Sorry, no posts matched your criteria.</p>
           <?php endif; ?>
          ?php wp_reset_postdata();?>
感谢@CYBNET 因为提到“线索”

在查询之后和循环之前,为类别标题和类别描述添加了正确的函数:

 <?php query_posts(array( 
          \'post_type\' => \'posttype\',
          \'category_name\' => \'category\',
          \'showposts\' => 20,
          )); ?>

    <h2><?php single_cat_title(\'\'); ?></h2>
    <p><?php echo category_description( get_category_by_slug(\'category_name_slug\')->term_id ); ?></p>

          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

你得到“类别名称”的次数与每个类别中的帖子数量一样多,因为你有代码在循环中输出“类别名称”,所以“类别名称”会打印在每个循环行程中。您可以通过以下方式修复此部件:

<?php if ( have_posts() ) { ?>

    <h2><a>Name of "Subcategory"</a></h2>
    <p>Description of "Subcategory"</p>

    <?php
    //Now you can start the loop
    while (have_posts()) {
        the_post();
        //don\'t use include, use get_template_part() instead
        //include (TEMPLATEPATH . \'/membresgim.php\');
        get_template_part(\'membresgim\');
    }
    ?>

<?php } else { ?>

    <p>Sorry, no posts matched your criteria.</p>

<?pph } ?>
您还必须知道,建议避免使用query_posts() 功能有以下几个原因。在您的情况下,由于要运行几个辅助循环,我认为最好使用WP_Query 对于每个循环。一个简单的例子(我从你的问题和评论中了解到):

<?php
//Change by the ID of your top categoy
$root_category = 45;
$subcategories =  get_categories(\'child_of=\'.$root_category);  
foreach  ($subcategories as $cat) {
    $args = array(
                category__in => array($cat->cat_ID),
            );
    $the_query = new WP_Query($args);
?>
    <section>
        <h2><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->nicename; ?></a></h2>
    </section>
    <ul>
    <?php
    while($the_query->have_posts()) {
        $the_query->the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } ?>
    <?php wp_reset_postdata(); ?>
    </ul>
<?php } ?>

结束

相关推荐

Link categories to last post

我正在使用wp\\u list\\u categories()显示我的所有类别。但我希望子类别链接到该类别的最后一个帖子。例如: <ul> <li><a href=\"link-to-category-1\">CATEGORY 1</a> <ul> <li><a href=\"link-to-last-post-of-category-1-1\">CATE

从顶级类别获取帖子,并按子类别分组 - 小码农CODE - 行之有效找到问题解决它

从顶级类别获取帖子,并按子类别分组

时间:2013-09-18 作者:marxslope

我有一个名为“Membres”(英语成员)的CPT。然后我创建了一个名为“Membres”的类别和他的子类别。

Parent category "Membres"
 - Subcategory1 (3 post inside)
 - Subcategory2 (4 post inside)
 - Subcategory3 (1 post inside)
我需要的解决方案是生成一种循环(在循环之外),但生成我之前定义的类别的归档文件的输出(调用函数)。这将是静态页面模板的一部分。我想获得的方案是:

<h1>Title "Membres"(calls the title of the page, working)</h1>

<section>
 - <h2><a>Name of "Subcategory1"</a></h2>
  - <p>Description of "Subcategory1"</p>
   - <posts Subcategory1>
</section>

<section>
- <h2><a>Name of "Subcategory2"</a></h2>
  - <p>Description of "Subcategory2"</p>
   - <posts Subcategory2>
</section>

<section>
 - <h2><a>Name of "Subcategory3"</a></h2>
  - <p>Description of "Subcategory3"</p>
    - <posts Subcategory3>
</section>
元素有问题:<;h2><a>Name of "Subcategory#"</a></h2>. 我需要它作为子类别每个部分的标题(因此也可以作为类别本身的链接)。现在,我获得“类别名称”的次数与每个类别中的帖子数量一样多,但没有类别名称,只有一次。我试着到处寻找解决方案,但目前没有任何结果。我还想继续使用这个方法,不想把东西移到分类法。

wordpress中的CPT设置功能。php:

// Register Custom Post Type
function custom_post_type() {

    $labels = array(
        \'name\'                => _x( \'Membres\', \'Post Type General Name\', \'mem\' ),
        \'singular_name\'       => _x( \'Membre\', \'Post Type Singular Name\', \'mem\' ),
        \'menu_name\'           => __( \'Membres\', \'mem\' ),
        \'parent_item_colon\'   => __( \'Membre principal\', \'mem\' ),
        \'all_items\'           => __( \'Tots els membres\', \'mem\' ),
        \'view_item\'           => __( \'Veure membre\', \'mem\' ),
        \'add_new_item\'        => __( \'Afegir membre\', \'mem\' ),
        \'add_new\'             => __( \'Nou membre\', \'mem\' ),
        \'edit_item\'           => __( \'Editar membre\', \'mem\' ),
        \'update_item\'         => __( \'Actualitzar membre\', \'mem\' ),
        \'search_items\'        => __( \'Cercar membres\', \'mem\' ),
        \'not_found\'           => __( \'No hi ha membres\', \'mem\' ),
        \'not_found_in_trash\'  => __( \'No hi ha membres a la paperera\', \'mem\' ),
    );
    $rewrite =<h2><?php arraysingle_cat_title(
        \'slug\'                => \'membre\',
        \'with_front\'          => true,
        \'pages\'               => true,
        \'feeds\'               => true,
    \'\');
    $args = array(
        \'label\'               => __( \'membres\', \'mem\' ),
        \'description\'         => __( \'Membres\', \'mem\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'trackbacks\', \'revisions\', \'custom-fields\', \'page-attributes\', \'post-formats\', ),
        \'taxonomies\'          => array( \'category\', \'post_tag\' ),
        \'hierarchical\'        => true,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'menu_icon\'           => get_bloginfo(\'template_directory\').\'/img/iconos?></users.png\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'post\',
    );h2>
  <p><?php echo register_post_typecategory_description( \'membres\', $args );

}

// Hook into the \'init\' action
add_actionget_category_by_slug( \'init\', \'custom_post_type\', 0 \'category_name_slug\');
我用于存档一个子类别的代码部分:

<?php query_posts(array( 
          \'post_type\' => \'membres\',
          \'category_name\' => \'investigadors\',
          \'showposts\' => 20,
         ->term_id )); ?>
    ></p>


      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<!-- PART TO SOLVE-->   
<h2><a>Name of "Subcategory3" Wordpress function PHP</a></h2>
<p>Description of "Subcategory3" PHP"category_description"</p>
<!-- PART TO SOLVE-->   

          <?php include (TEMPLATEPATH . \'/membresmem.php\'); ?>
           <?php endwhile; else: ?>
           <p>Sorry, no posts matched your criteria.</p>
           <?php endif; ?>
          ?php wp_reset_postdata();?>
感谢@CYBNET 因为提到“线索”

在查询之后和循环之前,为类别标题和类别描述添加了正确的函数:

 <?php query_posts(array( 
          \'post_type\' => \'posttype\',
          \'category_name\' => \'category\',
          \'showposts\' => 20,
          )); ?>

    <h2><?php single_cat_title(\'\'); ?></h2>
    <p><?php echo category_description( get_category_by_slug(\'category_name_slug\')->term_id ); ?></p>

          <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

你得到“类别名称”的次数与每个类别中的帖子数量一样多,因为你有代码在循环中输出“类别名称”,所以“类别名称”会打印在每个循环行程中。您可以通过以下方式修复此部件:

<?php if ( have_posts() ) { ?>

    <h2><a>Name of "Subcategory"</a></h2>
    <p>Description of "Subcategory"</p>

    <?php
    //Now you can start the loop
    while (have_posts()) {
        the_post();
        //don\'t use include, use get_template_part() instead
        //include (TEMPLATEPATH . \'/membresgim.php\');
        get_template_part(\'membresgim\');
    }
    ?>

<?php } else { ?>

    <p>Sorry, no posts matched your criteria.</p>

<?pph } ?>
您还必须知道,建议避免使用query_posts() 功能有以下几个原因。在您的情况下,由于要运行几个辅助循环,我认为最好使用WP_Query 对于每个循环。一个简单的例子(我从你的问题和评论中了解到):

<?php
//Change by the ID of your top categoy
$root_category = 45;
$subcategories =  get_categories(\'child_of=\'.$root_category);  
foreach  ($subcategories as $cat) {
    $args = array(
                category__in => array($cat->cat_ID),
            );
    $the_query = new WP_Query($args);
?>
    <section>
        <h2><a href="<?php echo get_category_link($cat->cat_ID); ?>"><?php echo $cat->nicename; ?></a></h2>
    </section>
    <ul>
    <?php
    while($the_query->have_posts()) {
        $the_query->the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } ?>
    <?php wp_reset_postdata(); ?>
    </ul>
<?php } ?>

相关推荐

Pagination in Archives

你好,我是wordpress的新手,我很烂。我试图在wordpress中为我的博客页面添加编号分页。我下载了插件“page navi”,进入编辑器并更改了索引中的一个文件。php收件人: <?php if ( $wp_query->max_num_pages > 1 ) : ?> <div class=\"post-nav archive-nav\"> <?php wp_pagenavi();