Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Goodmorning everyone,

I'm quite new to Couchcms, and I'm encountering a problem of wich I can't find a solution.
I have a Blog page, which contains blog articles, all handled dynamically with Couchcms. In the sidebar of the Blog article page I have "Related articles" and "Related products" widgets, both handled with <cms:related_pages> tag (I post here only one of the two widgets, as they are basically the same).

Here is what I have at the top of the page
Code: Select all
<cms:editable name="articoli_correlati" label="Articoli correlati" type='relation' masterpage='blog.php' />
<cms:editable name="prodotti_correlati" label="Prodotti correlati" type='relation' masterpage='prodotti.php' />


And here is what I have in the widget area
Code: Select all
<div class="recent-post-widget">
   <div class="widget-heading">
      <h3>Related articles</h3>
   </div>
   <div class="widget-content">
       <cms:related_pages 'articoli_correlati' limit='4'>
             <div class="recent-post-item">
                 <div class="recent-post-item-image">
                      <img src="<cms:show foto_articolo/>" alt="<cms:show k_page_title/>" />
                 </div>
                 <div class="recent-post-item-content">
                      <div class="recent-post-item-title">
                           <a href="<cms:show k_page_link/>"><cms:show k_page_title/></a>
                      </div>
                      <div class="recent-post-item-date">
                            <p><cms:date k_page_date format='%e/%m/%Y' locale='italian'/></p>
                       </div>
                  </div>
              </div>
          </cms:related_pages>
     </div>
</div>


What I'm trying to achieve is to hide the entire widget section if there are no related articles checked in backend (basically if there are no related articles, the title is still showing, but with no content, which is not quite good). I tried to wrap the entire widget with a <cms:if prodotti_correlati> tag, but instead of hiding only the empty widgets, it hides also the sections where the related articles are checked.

I hope I well explained the problem, as I'm not really into Couchcms programming.

Thanks everyone! :)
Hi,

We can use the 'count_only' parameter with all page listing tags (e.g. cms:pages, cms:related_pages etc.) to return only the number of pages found for the given specifications.

This then can be used to first query if any pages would be found and them do the listing only if that is so.
Your code would now become like this -
Code: Select all
<cms:if "<cms:related_pages 'articoli_correlati' count_only='1' />">
    <div class="recent-post-widget">
       <div class="widget-heading">
          <h3>Related articles</h3>
       </div>
       <div class="widget-content">
           <cms:related_pages 'articoli_correlati' limit='4'>
                 <div class="recent-post-item">
                     <div class="recent-post-item-image">
                          <img src="<cms:show foto_articolo/>" alt="<cms:show k_page_title/>" />
                     </div>
                     <div class="recent-post-item-content">
                          <div class="recent-post-item-title">
                               <a href="<cms:show k_page_link/>"><cms:show k_page_title/></a>
                          </div>
                          <div class="recent-post-item-date">
                                <p><cms:date k_page_date format='%e/%m/%Y' locale='italian'/></p>
                           </div>
                      </div>
                  </div>
              </cms:related_pages>
         </div>
    </div>
</cms:if>

Notice how I have wrapped your original code in a <cms:if> clause testing the count of pages found.

Hope this helps.
Thank you so much, that was really what I was looking for!

Regards :)
Thanks for the answer (and actually the question also), helped me out good. Just FYI and future reference, I was dealing with reverse relation (a lecturer teaching multiple courses) and the code then would be

Code: Select all
<cms:if "<cms:reverse_related_pages 'lecturer' masterpage='courses.php' count_only='1' />">
   <div>
      <h3><cms:show lecturer_name /> also leads courses:</h3>
      <cms:reverse_related_pages 'lecturer' masterpage='courses.php' >
         <ul>
            <li><a href="<cms:show k_page_link />"><em><cms:show k_page_title /></em></a></li>
         </ul>
      </cms:reverse_related_pages>
   </div>
</cms:if>


...so you need to ho with "cms:reverse_related_pages" and state the masterpage.

Just in case someone needs it at some time.

@KK can you please possibly direct me to a place where things like count_only are discussed? I do not think I have seen this before.
Alternative choice is with k_paginated_top, k_paginated_bottom variables. Does not need an extra query.
5 posts Page 1 of 1
cron