admin管理员组

文章数量:1208155

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 question

I 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 question

I 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
  • Wouldn't you have to wrap 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
  • What does display inside the () at the end? What does var_dump( $context['TotalInTerm'] ); display? – Pat J Commented Jan 26, 2022 at 19:07
  • @Pat J Nothing is displaying inside () at the end at it's not working. var_dump displays NULL – EJ_95 Commented Jan 27, 2022 at 9:45
Add a comment  | 

1 Answer 1

Reset to default 1

You'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