How to echo terms count in MBView?
- This topic has 6 replies, 2 voices, and was last updated 2 years, 8 months ago by
indatext.com.
-
AuthorPosts
-
May 22, 2022 at 3:59 PM #36122
indatext.com
ParticipantI have custom taxonomy 'collections'. For each collection in requested array of collections I want to display a number of custom posts associated with that collection. I searched through docs and found no clue. Can I directly employ PHP code in MBView, or I need to stick to Twig?
I created an MBView with
{% set args = {taxonomy: 'collection',hide_empty: false,include:9,10,109,111} %} //only for certain collections array of IDs {% set collections = mb.get_terms( args ) %} {% for collection in collections %} <div class="background-collection"> <div class="collection_name"> <div>Collection</div><br> <h2>{{ collection.name }}</h2> <div class="button">View: _COUNT SHOULD BE HERE SOMEHOW_ models </div> </div> </div><!-- end of background-collection --> {% endfor %}
May 23, 2022 at 12:29 AM #36130indatext.com
ParticipantI tried
{% set models_count = mb.wp_count_terms('collection', array('hide_empty' => true)) %}
and echo {{models_count}} but it did not workedMay 23, 2022 at 1:35 AM #36131indatext.com
ParticipantI also tried this :
`{% for collection in collections %}
{% set models_count = mb.get_queried_object() %}'
since I am in a Collection loop, custom taxonomy object should be in query, but
{{models_count.count}} does not return count number.May 23, 2022 at 11:01 AM #36138Long Nguyen
ModeratorHi,
You can try to use the property
count
of the term object to show posts that are associated with the term.<div class="button">View: _COUNT SHOULD BE HERE SOMEHOW_ {{ collection.count }} models </div>
Read more on the documentation https://developer.wordpress.org/reference/classes/wp_term/
May 23, 2022 at 1:21 PM #36142indatext.com
ParticipantThank you so much, Long, MBViews is a godlike tool.
August 13, 2022 at 7:16 PM #37472indatext.com
ParticipantThis is interesting, but it seems this method does not return the correct count if used with WPML.
For original language it returns some larger count, for translated languages it returns ok.Is there a way to somehow filter this count through some WPML object to get the correct count on original language?
Example:
{% set args = {taxonomy: 'collection',hide_empty: false, include:'132, 133, 135, 136, 120, 449, 128, 130, 131, 141, 119'} %} {% set collections = mb.get_terms( args ) %} {% for collection in collections %} {% set image_upload = mb.get_term_meta( collection.term_id, 'image_advanced_collection_ru', true ) %} {% set image_upload_link = mb.wp_get_attachment_image_src( image_upload, large) %} <div class="background-collection" style="background:url({{ image_upload_link [0] }})"> <div class="collection_name"> <div class="collection-inner-tag">КОЛЛЕКЦИЯ</div> <h2>{{ collection.name }}</h2> <div class="collection-button-wrapper"><a class="ct-link collection-count" href="{{ mb.get_category_link( collection.term_id ) }}" target="_blank" rel="noopener">Показать: {{ collection.count }} моделей</a></div> </div><!-- end of collection_name --> </div><!-- end of background-collection --> {% endfor %}
returns incorrect count on original language at https://dev2.belwooddoors.com/collections/#div_block-111-3838 , but the very same code works correct on translated version (English)- https://dev2.belwooddoors.com/en/collections/#div_block-111-3838
It seems that MBView gets sum of all translations plus original, looking at count values returned. How to enforce count function stick to current language for terms?
August 13, 2022 at 11:00 PM #37479indatext.com
ParticipantSolved. It was terms attribution problem. WPML allows to assign translated AND original terms to the same post. It doubles the count. Keeping terms in discipline to the current language helped.
-
AuthorPosts
- You must be logged in to reply to this topic.