Forum

Thread tagged as: Question, Blog

Template for Blog postTag Template Tag

I have modified post_in_list.html to display the post tags below each post by including

<perch:blog id="postTags" />

however, this is not what I want as it just spits out an unstyled list of the tags for each post. Is there a template for Template Tags (https://docs.grabaperch.com/addons/blog/template-tags/), or am I missing the point? What I would like is a list of the tags that link to the tag archive pages (/archive.php?tag=mytag) - how can I insert that in post_in_list.html?

Thanks, Mike.

Mike Harrison

Mike Harrison 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I can't use php in a template though, so would I need to include a layout file to do this?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can generate the HTML outside of the template and then pass it in.

https://docs.grabaperch.com/docs/templates/passing-variables-into-templates/

I'm still not getting it I'm afraid. This is what I have done:

/blog/index.php

<?php
  $tags = perch_blog_post_tags(perch_get('s'));
  PerchSystem::set_var('tags', $tags);
  perch_blog_recent_posts(10);
?>

post_in_list.html

<perch:blog id="tags" />

This doesn't output anything :(

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to return the value, so try:

 $tags = perch_blog_post_tags(perch_get('s'), false, true);

Still getting nothing I'm afraid Drew. I think the problem is that it needs to be called from within the template else it won't work.

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's not the case, no.

Can you turn on debug and let me know what it outputs?

Debug Message
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/calcon/blog/index.php' OR regionPage='*' ORDER BY regionPage DESC
SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 ORDER BY pageTreePosition ASC
SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/calcon/blog/index.php' LIMIT 1
SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-008', '000') ORDER BY pageTreePosition DESC
Using template: /templates/navigation/nav.html
Using template: /templates/navigation/subnav.html
Fetching from cache: perch_blog_post_tagsc80dc268194400d188e11933da8c5ee2
Cache file not found: perch_blog_post_tagsc80dc268194400d188e11933da8c5ee2
SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2016-01-28 14:17:00' AND postSlug=''
SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postDateTime' WHERE 1=1 AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue ) as tbl WHERE (postStatus='Published' AND postDateTime<='2016-01-28 14:17:00' ) GROUP BY itemID, sortval ORDER BY sortval DESC LIMIT 0, 10
SELECT FOUND_ROWS() AS `count`
SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Using template: /templates/blog/post_in_list.html
SELECT * FROM perch2_blog_authors ORDER BY authorFamilyName, authorGivenName ASC
SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
SELECT * FROM perch2_blogs ORDER BY blogTitle ASC
Fetching from cache: perch_blog_categories589c7a634eb12f2ec225cfedc7dc78ae
Fetching from cache: perch_blog_tagse0b2ff59ed95ac2a35516ee8013c2dd2
Fetching from cache: perch_blog_date_archive_monthsf2e7d49784704a5b24102a78c349d57c
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's the value of ?s= in your query string?

Do you mean in a post URL? As in "post.php?s=2015-12-17-our-first-blog-post"

Drew McLellan

Drew McLellan 2638 points
Perch Support

Right - that looks ok, but for some reason it's not coming through:

SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2016-01-28 14:17:00' AND postSlug=''

Any ideas why? Just to clarify I am calling this from the main blog index.php page rather than the post page itself so would the post slug not become available until cycling through perch_blog_recent_posts(10)?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show me the code you have on that page?

Sure Drew, see below:

/blog/index.php:

<?php
$tags = perch_blog_post_tags(perch_get('s'), false, true);
PerchSystem::set_var('tags', $tags);

perch_blog_recent_posts(10);
?>

post_in_list.html:

<perch:blog id="tags" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

If this is /blog/index.php then you don't have a specific post to show, do you?

Yeah, that's what I was trying to say. Hence why I asked you if I could call this from within the post_in_list.html template file? It seems clear to me that it should be called from within the loop, not outside as you previously seemed to suggest.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to use an each callback for that.

'each' => function($item) {
    $item['tags_html'] => perch_blog_post_tags($item['postSlug'], false, true);
    return $item;
},

Drew McLellan said:

You'd need to use an each callback for that.

'each' => function($item) {
   $item['tags_html'] => perch_blog_post_tags($item['postSlug'], false, true);
   return $item;
},

Sorry Drew, I'm even more confused now, where am I putting this code in my files? Plus, am I supposed to replace 'each' as this doesn't look like valid PHP? In fact if I put this in my /blog/index.php page I get a syntax warning (Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)). And what is 'tags_html'?

I'm afraid you are going to have to explain this in dumb terms and break it down as to how and where to use this - sorry!

Thanks, Mike.

Drew McLellan

Drew McLellan 2638 points
Perch Support

each is an option you can add to your perch_blog_custom() options array.

Replace this:

  $tags = perch_blog_post_tags(perch_get('s'));
  PerchSystem::set_var('tags', $tags);
  perch_blog_recent_posts(10);

with

perch_blog_custom([
    'count' => 10,
    'sort' => 'postDateTime', 
    'sort-order' => 'DESC',
    'each' => function($item) {
        $item['tags_html'] => perch_blog_post_tags($item['postSlug'], false, true);
        return $item;
    },
]);

Then in your template you can use:

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

I'm getting a syntax error with this code: Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) on this line:

$item['tags_html'] => perch_blog_post_tags($item['postSlug'], false, true);

I assume this should be

$item['tags_html'] = perch_blog_post_tags($item['postSlug'], false, true);

It still doesn't work anyway and shows this error in diagnostics now:

Template file not found: .../perch/addons/apps/perch_blog/templates/blog/.html