Wordpress自定义帖子type_show帖子计数

时间:2016-08-23 07:37:26

标签: php wordpress wordpress-theming custom-post-type taxonomy

我正在使用自定义WP主题。我需要在各个类别下显示每个帖子,这些工作正常。

我将类别更改为taxonomy.Now,我想在每个类别名称下显示更多信息,但是,我无法理解,我应该将我的代码放在循环中。 特别是每个类别下的帖子计数。

 <?php
    /*
    Template Name: Home Page
    */
    get_header();
     global $redux_demo;

    ?>
    <div class="sroll"><div class="container">
    <marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
    </div></div>
    <div class="container">
     <div class="row">
      <div class="col-sm-9"> </br>
      <div class="content mCustomScrollbar" style="height: 690px;">

        <?php
    $terms = get_terms( array(
        'taxonomy' => 'category',
        'hide_empty' => false,
    ) );
    foreach($terms as $cat){
        $cata_name = $cat->name; 
        $term_id = $cat->term_id; 

    ?>
    <div class="col-sm-6 col-md-4 col-lg-3 p10">
    <div class="box">
         <?php 
    //echo '<h3>'.$catname[0]->cat_name.'</h3>';

    ?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
        <?php echo $cata_name; ?></a></h3> <?php

    $catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' ); 
    while($catqueryy->have_posts()) : $catqueryy->the_post();
    ?>

        <p class="post_title"><?php echo '<a  href="'.home_url('index.php/category/'.$cata_name).'">'.__(get_the_title(),'rockon').'</a>'; ?></p>
        <p class="post_cont"><?php echo get_the_excerpt(); ?></p>
        <?php
    endwhile; 
    ?>
    </div>

        </div>

    <?php } ?>
    </div></br>
      </div> 

      <div class="col-sm-3">
      <h1></h1>
       <?php get_sidebar(); ?>
      <h1></h1>
      </div>

     </div>
    </div>

    <?php
    get_footer();
    ?>

4 个答案:

答案 0 :(得分:0)

您可以查看此链接enter link description here

如果有帮助..我想,有人会在这里编写这段代码,因为我还需要了解在传递查询后它是如何工作的。

答案 1 :(得分:0)

自定义分类尝试:

$the_query = new WP_Query( array(
    'post_type' => 'CUSTOM_POST_TYPE',
    'tax_query' => array(
        array(
            'taxonomy' => 'CUSTOM_TAXONOMY',
            'field' => 'id',
            'terms' => TERM_ID
        )
    )
) );
$count = $the_query->found_posts;

https://wordpress.org/support/topic/counting-posts-within-categories

例如:

<?php
    /*
    Template Name: Home Page
    */
    get_header();
     global $redux_demo;

    ?>
    <div class="sroll"><div class="container">
    <marquee><p> <?php echo $redux_demo['main-option-marquee']; ?></p></marquee>
    </div></div>
    <div class="container">
     <div class="row">
      <div class="col-sm-9"> </br>
      <div class="content mCustomScrollbar" style="height: 690px;">

        <?php
    $terms = get_terms( array(
        'taxonomy' => 'category',
        'hide_empty' => false,
    ) );
    foreach($terms as $cat){
        $cata_name = $cat->name; 
        $term_id = $cat->term_id; 

    ?>
    <div class="col-sm-6 col-md-4 col-lg-3 p10">
    <div class="box">
         <?php 
    //echo '<h3>'.$catname[0]->cat_name.'</h3>';

    ?><h3><a href="<?php echo home_url('index.php/category/'.$cata_name) ?>">
        <?php echo $cata_name; ?></a></h3> 
    <?php

    $catqueryy = new WP_Query( 'cat='.$term_id.'&posts_per_page=4' ); 
    $count = $catqueryy->found_posts;
    ?>
    <h3><?php echo "Post Count : ".$count; ?></h3> 
    <?php
    while($catqueryy->have_posts()) : $catqueryy->the_post();
    ?>

        <p class="post_title"><?php echo '<a  href="'.home_url('index.php/category/'.$cata_name).'">'.__(get_the_title(),'rockon').'</a>'; ?></p>
        <p class="post_cont"><?php echo get_the_excerpt(); ?></p>
        <?php
    endwhile; 
    ?>
    </div>

        </div>

    <?php } ?>
    </div></br>
      </div> 

      <div class="col-sm-3">
      <h1></h1>
       <?php get_sidebar(); ?>
      <h1></h1>
      </div>

     </div>
    </div>

    <?php
    get_footer();
    ?>

答案 2 :(得分:0)

实际上,您不需要通过手动编写代码来计算任何内容。如果您查看get_terms()描述,您可以看到WP为您计算(如果您将'pad_counts'设置为true(或1))。启用此选项后,每个类别的响应数组中都会有一个“count”键和一个数字值。

你可以在你想要的地方“回声”它。

这样你的

$terms = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false,
) );

应如下所示:

$terms = get_terms( array(
    'taxonomy'   => 'category',
    'hide_empty' => false,
    'pad_counts' => true,
) );

请注意,我添加了'pad_counts'=&gt;对查询是真的,,这样你就可以得到你想要的数字 - 而无需编写太多的代码。

如果你想手动完成,我建议你创建一个用'category =&gt;填充数组的循环编号'元素,并在写出HTML的循环中查找所需的键值对。

答案 3 :(得分:-2)

我也在寻找类似这个问题的东西。我想,你需要一个代码来显示每个类别的帖子数。