Forum

Thread tagged as: Question, Problem, Blog

How do I use a custom template for a blog post?

We use multiple blogs and one blog uses the default template (post.html) for my detail post page. Like:

<?php perch_blog_post(perch_get('s')); ?>

I like to use a custom template for the other blog. Something like:

<?php
perch_blog_custom(array(
'filter' => 'slug',
'blog' => 'news',
'template' => 'post_en.html',
'filter' => 'slug',
'match' => 'eq',
'value' => perch_get('s'),
'count' => 1,
));
?>

By using this code we manage to get the correct template but it doesn't show any content. How is this possible?

Thanks!

Stig Tafto

Stig Tafto 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's in your template? Did you start with a copy of post.html?

Yes, did start with a copy. Here is the customized template:

<article class="news">
    <img src="<perch:blog id="hovedbilde" width="1000" height="650" crop="true" type="image" output="url"/>" alt=""/>
    <h2><perch:blog id="postTitle" /></h2>
    <p><perch:blog id="ingress" type="textarea" words="20" append="..."/></p>
    <p><a href="/en<perch:blog id="postURL"/>">More</a></p>
</article>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, that looks ok.

Do you get any output at all?

Debug message shows two templates being used.

Debug Message - Perch Runway 3.0.2
[77] SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate, pr.routeOrder, s.settingValue AS siteOffline FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID LEFT JOIN perch2_settings s ON s.settingID='siteOffline' UNION SELECT NULL AS pagePath, pr2.routePattern, pr2.routeRegExp, pr2.templatePath AS pageTemplate, pr2.routeOrder, NULL AS siteOffline FROM perch2_page_routes pr2 WHERE templateID!=0 ORDER BY routeOrder ASC, pagePath ASC
Matched route: en/news/[slug:s]
Using master page: /templates/pages/news/post.php
Page arguments:
Array
(
    [0] => /en/news/17-04-2017-english
    [s] => 17-04-2017-english
    [1] => 17-04-2017-english
)
[4] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/en/news/news-detail' OR regionPage='*' ORDER BY regionPage DESC
[1] SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' AND postSlug='17-04-2017-english'
[28] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
[1] SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
[2] SELECT * FROM perch2_blogs ORDER BY blogTitle ASC
Using template: /templates/blog/post.html
[1] 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='_id' WHERE 1=1 AND ((idx.indexKey='postSlug' AND idx.indexValue='17-04-2017-english')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 10
[1] SELECT FOUND_ROWS() AS `count`
[1] Using template: /addons/apps/perch_blog/templates/blog/meta_head.html
[4] SELECT * FROM perch2_blog_authors ORDER BY authorFamilyName, authorGivenName ASC
[28] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Language link' AND (regionPage='/en/news/news-detail' OR regionPage='*')
[1] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=162 AND idx.itemRev=1)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC
[1] Using template: /templates/content/general-language.html
[1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/en' OR pageSortPath='/en' LIMIT 1
[7] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-002%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC
[1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/en/news/news-detail' LIMIT 1
[3] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-002-004-001', '000-002-004', '000-002', '000') ORDER BY pageTreePosition DESC
[6] Using template: /templates/navigation/navigasjon.html
Using template: /templates/search/search-form.html
[1] 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='_id' WHERE 1=1 AND ((idx.indexKey='postSlug' AND idx.indexValue='17-04-2017-english')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 10
[1] SELECT FOUND_ROWS() AS `count`
[1] Using template: /templates/blog/post.html
[1] SELECT blogID FROM perch2_blogs WHERE blogSlug='news' LIMIT 1
[nil] 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='_id' WHERE 1=1 AND ((idx.indexKey='slug' AND idx.indexValue='17-04-2017-english')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE ( blogID=4 AND postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 1
[1] SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post_en.html
Request time: 0.0521
Process time: 0.0518
Memory: 3.282
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you get any output at all?

Yes, now we manage to output the content, but it uses the default template (post.html), not the post_en.html that we like to use.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does your page function look like?

Do you mean this one?

<?php perch_blog_custom(array( 'filter' => 'slug', 'blog' => 'news', 'template' => 'post_en.html', 'filter' => 'slug', 'match' => 'eq', 'value' => perch_get('s'), 'count' => 1, )); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's the one. So you're saying that even though you're specifying post_en.html a different template is being used? Does that match what the debug output says?

Is the repeated 'filter' in the array doing something funny here?

Yes, that match what the debug output says. Removed the repeated «filter» without any new result.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show us the debug?

Yes, here:

Debug Message - Perch Runway 3.0.2 
[77] SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate, pr.routeOrder, s.settingValue AS siteOffline FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID LEFT JOIN perch2_settings s ON s.settingID='siteOffline' UNION SELECT NULL AS pagePath, pr2.routePattern, pr2.routeRegExp, pr2.templatePath AS pageTemplate, pr2.routeOrder, NULL AS siteOffline FROM perch2_page_routes pr2 WHERE templateID!=0 ORDER BY routeOrder ASC, pagePath ASC Matched route: en/news/
[slug:s] Using master page: /templates/pages/news/post.php Page arguments: Array ( 
[0] => /en/news/17-04-2017-english 
[s] => 17-04-2017-english 
[1] => 17-04-2017-english ) 
[4] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/en/news/news-detail' OR regionPage='*' ORDER BY regionPage DESC 
[1] SELECT * FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' AND postSlug='17-04-2017-english' 
[28] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 
[1] SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC 
[2] SELECT * FROM perch2_blogs ORDER BY blogTitle ASC Using template: /templates/blog/post.html 
[1] 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='_id' WHERE 1=1 AND ((idx.indexKey='postSlug' AND idx.indexValue='17-04-2017-english')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 10 
[1] SELECT FOUND_ROWS() AS `count` 
[1] Using template: /addons/apps/perch_blog/templates/blog/meta_head.html 
[4] SELECT * FROM perch2_blog_authors ORDER BY authorFamilyName, authorGivenName ASC 
[28] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0 
[1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Language link' AND (regionPage='/en/news/news-detail' OR regionPage='*') 
[1] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=162 AND idx.itemRev=1)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC 
[1] Using template: /templates/content/general-language.html 
[1] SELECT pageID, pageParentID, pageDepth, pageTreePosition FROM perch2_pages WHERE pagePath='/en' OR pageSortPath='/en' LIMIT 1 
[7] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageTreePosition LIKE '000-002%' AND pageDepth >=1 AND pageDepth<=2 ORDER BY pageTreePosition ASC 
[1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/en/news/news-detail' LIMIT 1 
[3] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-002-004-001', '000-002-004', '000-002', '000') ORDER BY pageTreePosition DESC 
[6] Using template: /templates/navigation/navigasjon.html Using template: /templates/search/search-form.html 
[1] 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='_id' WHERE 1=1 AND ((idx.indexKey='postSlug' AND idx.indexValue='17-04-2017-english')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 10 
[1] SELECT FOUND_ROWS() AS `count` 
[1] Using template: /templates/blog/post.html 
[1] SELECT blogID FROM perch2_blogs WHERE blogSlug='news' LIMIT 1 
[nil] 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='_id' WHERE 1=1 AND ((idx.indexKey='slug' AND idx.indexValue='17-04-2017-english')) AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE ( blogID=4 AND postStatus='Published' AND postDateTime<='2017-04-25 09:29:00' ) GROUP BY itemID, sortval ORDER BY sortval ASC LIMIT 0, 1 
[1] SELECT FOUND_ROWS() AS `count` Using template: /templates/blog/post_en.html Request time: 0.0521 Process time: 0.0518 Memory: 3.282
Drew McLellan

Drew McLellan 2638 points
Perch Support

And this is the exact code you're using?

perch_blog_custom(array( 
    'filter' => 'slug', 
    'blog' => 'news', 
    'template' => 'post_en.html', 
    'filter' => 'slug', 
    'match' => 'eq', 
    'value' => perch_get('s'), 
    'count' => 1, 
));

This is the correct code (removed the repeating filter):

<?php
perch_blog_custom(array(
    'filter' => 'slug',
    'blog' => 'news',
    'template' => 'post_en.html',
    'match' => 'eq',
    'value' => perch_get('s'),
    'count' => 1,
));
?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Is this the path to your template?

/perch/templates/blog/post_en.html

No, it´s not.

The right path is: /dv-cms/templates/blog/post_en.html

Changed the filter ID from «slug» to «postSlug» and now it works fine ;-)

<?php
perch_blog_custom(array(
    'filter' => 'postSlug',
    'blog' => 'news',
    'template' => 'post-en.html',
    'match' => 'eq',
    'value' => perch_get('s'),
    'count' => 1,
));
?>