Forum

Thread tagged as: Problem, Addons, Blog

Blog if else statement for no tags returned

I thought I was just trying to achieve something fairly simple, but for whatever reason, I cannot get it to work.

On a blog app installation, I am using categories and tags to help aid in blog entry archiving, searching etc....as most people do.

At the bottom of each blog entry I am listing the categories and tags that are associated with that individual entry.

What I would like to do is return a value/message when, for example, the entry doesnot have any tags.

Currently, my thought was to use the 'if exists' route for the perch_item_index, with the theory being, if there is no index value, then there are no tags and as such, display a message. The code I have is:

<perch:before>
</perch:before>
    <perch:if exists="perch_item_index">    
        <perch:if exists="perch_item_last">
            <a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag"><perch:blog id="tagTitle" /></a>
        <perch:else />
            <a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag"><perch:blog id="tagTitle" /></a> | 
        </perch:if>
    <perch:else />
        <p>No Tags</p>
    </perch:if>         
<perch:after>
</perch:after>

The first if, I thought would have just been the trigger to pick up that either tags existed, or didn't for that entry. The sub perch:if is purely for styling.

Any thoughts as to why my 'No Tags' message is not displaying.

Works fine, if tags are present.......

Andrew Kennedy

Andrew Kennedy 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Templates aren't normally rendered if they don't have any content, unless they use a noresults section.

Have you tried that?

Just tried adding a 'noresults' element to the current solution, as such:

<perch:before>
</perch:before>
<perch:if exists="perch_item_last">
    <a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag"><perch:blog id="tagTitle" /></a>
<perch:else />
    <a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag"><perch:blog id="tagTitle" /></a> | 
</perch:if>
<perch:after>
</perch:after>
<perch:noresults>
    Sorry, there are no items currently available.
</perch:noresults>

No joy. Will look at trying some other things.

Thanks

Ok, got it working. I should have read / googled this more before I posted, so my apologies. I was in a rush/stressed.

For anyone who is interested, to get the 'noresults' element working, you need to use a custom function. So, in my example, from the Blog post.php page I originally called the tags with:

<?php perch_blog_post_tags(perch_get('s'), array(
    'template' => 'tag_in_blog.html',
)); ?>

Now, I use a custom call:

<?php perch_blog_custom(array(
    'tag' => perch_get('tag'),
    'template' => 'tag_in_blog.html'
)); ?>          

This, with the changes to my template (as shown in above reply) result in a working solution.

Thanks Drew for putting me on the correct path !!

Ooops, sorry everyone. The above does not work.

Back to the drawing board. I will edit things, as soon as I can figure out a solution to this.