Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1208155
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 2 years ago.
Improve this questionI am using the following code to access my categories.
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'exclude' => 18,
);
$context['categories'] = get_terms( 'product_cat', $cat_args );
$total_in_term = $context['categories']->count;
$context['TotalInTerm'] = $total_in_term;
And trying to display the category name following by number of posts that each term has in brackets after it:
{% for cat in categories %}
<a href="{{ site.link }}/{{ cat.slug }}">{{ cat.name }} ({{ TotalInTerm }})</a>
{% endfor %}
I am trying to get it to output like this: Category Name ( { number of posts in category } )
So far I have it outputting the category name and link only but not the number of posts it holds. Can someone help?
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 2 years ago.
Improve this questionI am using the following code to access my categories.
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'exclude' => 18,
);
$context['categories'] = get_terms( 'product_cat', $cat_args );
$total_in_term = $context['categories']->count;
$context['TotalInTerm'] = $total_in_term;
And trying to display the category name following by number of posts that each term has in brackets after it:
{% for cat in categories %}
<a href="{{ site.link }}/{{ cat.slug }}">{{ cat.name }} ({{ TotalInTerm }})</a>
{% endfor %}
I am trying to get it to output like this: Category Name ( { number of posts in category } )
So far I have it outputting the category name and link only but not the number of posts it holds. Can someone help?
Share Improve this question edited Jan 26, 2022 at 17:42 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jan 26, 2022 at 17:16 EJ_95EJ_95 33 bronze badges 3 |1 Answer
Reset to default 1You're assuming that get_terms()
returns an object with a count
property, but it doesn't.
From the docs:
Return
(WP_Term[]|int[]|string[]|string|WP_Error) Array of terms, a count thereof as a numeric string, or WP_Error if any of the taxonomies do not exist. See the function description for more information.
Also, the way you're calling get_terms()
is deprecated. There should be only one argument, an array that includes the taxonomy name.
So here's how I'd update the code:
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$cat_args = array(
'taxonomy' => 'product_cat',
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'exclude' => 18,
);
$context['categories'] = get_terms( $cat_args );
// Gets the count in a separate call.
$cat_args['fields'] = 'count';
$total_in_term = get_terms( $cat_args );
$context['TotalInTerm'] = $total_in_term;
References
get_terms()
fields
parameter
本文标签: categoriesHow can I display the number of post associated to a category in TwigTimber Wordpress
版权声明:本文标题:categories - How can I display the number of post associated to a category in TwigTimber Wordpress? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738757800a2110773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
TotalInTerm
in{{ ... }}
to get the variable? ie,({{TotalInTerm}})
? (nm, I see you've updated the question to reflect this) – Pat J Commented Jan 26, 2022 at 17:25()
at the end? What doesvar_dump( $context['TotalInTerm'] );
display? – Pat J Commented Jan 26, 2022 at 19:07()
at the end at it's not working.var_dump
displays NULL – EJ_95 Commented Jan 27, 2022 at 9:45