Wordpress 自定义 post type_show post 计数
Wordpress Custom post type_show post counts
我在自定义 WP 中工作 theme.I 需要在各个类别下显示每个 post,这工作正常。
我将类别更改为 taxonomy.Now,我想在每个类别名称下显示更多信息,但是,我不明白,我应该将我的代码放在循环中的什么位置。
特别是每个类别下的 post 计数。
<?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();
?>
我也在搜索类似这个问题的东西。我认为,您需要一个代码来显示每个类别的 post 计数。
你可以查看这个Link
如果有帮助..我想,有人会在这里写这段代码,因为我还需要了解它在您通过查询后如何工作。
自定义分类尝试:
$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();
?>
实际上,您不需要通过手动编写代码来计算任何东西。如果您查看 get_terms() 描述,您可以看到 WP 会为您计算这个(如果您将 'pad_counts' 设置为真(或 1))。启用此功能后,每个类别的响应数组中都会有一个 "count" 键和一个数字值。
你可以"echo"它在你想要的地方。
这边你的
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
应该是这样的:
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'pad_counts' => true,
) );
请注意,我在查询中添加了 'pad_counts' => true,,因此您将获得所需的号码 - 无需编写太多代码。
如果你想手动完成,我建议你创建一个循环,用 'category => number' 元素填充一个数组,并在写出 [= 的循环中查找所需的键值对32=].
我在自定义 WP 中工作 theme.I 需要在各个类别下显示每个 post,这工作正常。
我将类别更改为 taxonomy.Now,我想在每个类别名称下显示更多信息,但是,我不明白,我应该将我的代码放在循环中的什么位置。 特别是每个类别下的 post 计数。
<?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();
?>
我也在搜索类似这个问题的东西。我认为,您需要一个代码来显示每个类别的 post 计数。
你可以查看这个Link
如果有帮助..我想,有人会在这里写这段代码,因为我还需要了解它在您通过查询后如何工作。
自定义分类尝试:
$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();
?>
实际上,您不需要通过手动编写代码来计算任何东西。如果您查看 get_terms() 描述,您可以看到 WP 会为您计算这个(如果您将 'pad_counts' 设置为真(或 1))。启用此功能后,每个类别的响应数组中都会有一个 "count" 键和一个数字值。
你可以"echo"它在你想要的地方。
这边你的
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
) );
应该是这样的:
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
'pad_counts' => true,
) );
请注意,我在查询中添加了 'pad_counts' => true,,因此您将获得所需的号码 - 无需编写太多代码。
如果你想手动完成,我建议你创建一个循环,用 'category => number' 元素填充一个数组,并在写出 [= 的循环中查找所需的键值对32=].