Forum

Thread tagged as: Question, Problem, Blog

Blog App Tag Links

Hi, In the Blog App is there a template tag to automatically get the link to the specific tag category. I know you can pull the tag name automatically, but how do you get the tag name to bring them to a page where they can see all the posts with that tag? Thanks

Greg Stone

Greg Stone 0 points

  • 6 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

You would filter on tag, I believe the files that you download contain an example of doing that on the archive listing, but otherwise it's all in the docs:

https://docs.grabaperch.com/addons/blog/page-functions/custom/

You just pass in a tag or tags as an array.

Hi Rachel,

Thanks, but I don't think that's quite what I'm looking for. In my blog templates I've adjusted the "post_in_list.html" to my liking, but I'd like the tag category title to also be a link to the tag category listing automatically. What I'm looking for is the automated link association. I'll show you my code. I put in code brackets what I'm trying to adjust.

<li class="hentry">
        <a href="<perch:blog id="postURL" />" rel="bookmark" class="entry-title"><perch:if exists="image"><img src="<perch:blog id="image" type="image" />" alt="<perch:blog id="postTitle" />" /></a></perch:if>
        <span><perch:blog id="postTags" type="text" /></span> <!-- I want this post tag to be linked to the tag category. Is there a template tag for this? -->
        <h2><a href="<perch:blog id="postURL" />" rel="bookmark" class="entry-title"><perch:blog id="postTitle" /></a></h2>
        <div class="description entry-summary">
        <perch:blog id="excerpt" type="textarea" textile="true" />
        </div>
    </li>

I hope this makes sense.

Hi Greg,

I think your template code would need something like this to list out the tags:

<perch:before><ul></perch:before>
    <li><a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag"><perch:blog id="tagTitle" /></a></li>
<perch:after></ul></perch:after>

This should (as an example) direct the query to an archive page using the slug for the tag.

If you check the "archive.php" within the blog download files, the code to display a list of posts by tag looks like this:

/* --------------------------- POSTS BY TAG --------------------------- */
if (perch_get('tag')) {
    echo '<h1>Archive of: '.perch_blog_tag(perch_get('tag'), true).'</h1>';

    perch_blog_custom(array(
    'tag'        => perch_get('tag'),
    'template'   => $template,
    'count'      => $posts_per_page,
    'sort'       => $sort_by,
    'sort-order' => $sort_order,
            ));

    $posts_displayed = true;
}

You could alternatively create a "tags.php" page to use rather than "archive.php" and loose the IF statement, etc…

Thank you for your reply. This looks like what I need. I'll give it a try! Thanks!

I'm not getting anywhere. When I click on the tag, it takes me to the full archive of posts rather than the filtered tag. This is what I have.

<span>
         <a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag">
                <perch:blog id="postTags" type="text" />
         </a>
  </span> 

I'm looking to get the tag link from the post tag title it's pulling.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Does the tag appear on the URL?

No it doesn't. It ends like this: mysite.com/archive.php?tag=

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, that's why the archive page isn't filtering - it's got nothing to filter with

Check your template that is creating the link.

Not sure what to check for in the template, that's my problem. Here's my code for the post_in_list.html. Thanks.

<perch:before>
<ul class="hfeed listing"></perch:before>
    <li class="hentry">
        <a href="<perch:blog id="postURL" />" rel="bookmark" class="entry-title">
            <perch:if exists="image"><img src="<perch:blog id="image" type="image" />" alt="<perch:blog id="postTitle" />" />
        </a></perch:if>


<!--THIS IS WHERE MY PROBLEM IS-->
        <span>
            <a href="archive.php?tag=<perch:blog id="tagSlug" />" rel="tag">
                <perch:blog id="postTags" type="text" />
            </a>
        </span> 

<!--END PROBLEM-->



        <h2>
            <a href="<perch:blog id="postURL" />" rel="bookmark" class="entry-title">
                <perch:blog id="postTitle" />
            </a>
        </h2>

        <!--<p class="entry-published date"><perch:blog id="postDateTime" format="%d %B %Y" /></p>-->
        <div class="description entry-summary">
        <perch:blog id="excerpt" type="textarea" textile="true" />
        <!--<p>Comments: <perch:blog id="postCommentCount"/></p>-->
        </div>
    </li>
<perch:after>
    </ul>
    <perch:if exists="paging">
        <div class="paging">
            Page <perch:blog id="current_page" /> of <perch:blog id="number_of_pages" />
            <perch:blog id="page_links" encode="false" />
            <perch:if exists="not_first_page">
                <a href="<perch:blog id="prev_url" encode="false" />">Previous</a>
            </perch:if>
            <perch:if exists="not_last_page">
                <a href="<perch:blog id="next_url" encode="false" />">Next</a>
            </perch:if>
        </div>
    </perch:if>
</perch:after>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're using tagSlug, but in that context there could be lots of tags - so which slug is it?

To link the individual tags in a listing you'll need to process them before the template.

perch_blog_custom([
    'each' => function($item){
        $item['tag_links'] = perch_blog_post_tags(perch_get('s'), 'post_tag_link.html', true);
        return $item;
     }
]);

and then in your template

<perch:blog id="tag_links" encode="false" />

Thanks Drew. Would I add the php code to the archive.php or as a new php file?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Add the each option to your existing code where you're creating the listing.