How to echo terms count in MBView?

Support MB Views How to echo terms count in MBView?Resolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #36122
    indatext.comindatext.com
    Participant

    I 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 %}
    #36130
    indatext.comindatext.com
    Participant

    I tried {% set models_count = mb.wp_count_terms('collection', array('hide_empty' => true)) %}
    and echo {{models_count}} but it did not worked

    #36131
    indatext.comindatext.com
    Participant

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

    #36138
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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/

    #36142
    indatext.comindatext.com
    Participant

    Thank you so much, Long, MBViews is a godlike tool.

    #37472
    indatext.comindatext.com
    Participant

    This 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?

    #37479
    indatext.comindatext.com
    Participant

    Solved. 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.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.