Forum

Thread tagged as: Problem, Blog

How can I use separate templates for different blog sections

Hi, I'm trying to use blog sections to create 2 different post types (posts and special offers) that I want to present differently, i.e. I want to use different templates for each section - is this possible or am I barking up the wrong tree?

I'm using blog/index.php to list the posts section: <?php perch_blog_custom([ 'count'=>4, 'section'=>'posts', 'sort' => 'postDateTime', 'sort-order' => 'DESC', 'template' => 'post_in_list.html' ]); ?>
This works fine. I am using special-offers/index.php to list the special offers section: <?php perch_blog_custom([ 'count' => 4, 'section'=>'special-offers', 'sort' => 'postDateTime', 'sort-order' => 'DESC', 'template' => 'section_list.html' ]); ?>

Ideally the postSlug urls generated by the section_list.html template would output something like: special-offers/post.php?s=my-post-slug rather than blog/post.php?s=my-post-slug. That way I could use a different post.php to display the special offers using a different template.

This is the button link from section_list.html template <button><a href="<perch:blog id="postURL" />">Read More</a></button>

Is there some way to modify the postURL to get this to work? Or is there a simpler and better approach?

Adam Green

Adam Green 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Thanks for that Drew, just what I was looking for.

I have another, hopefully very simple question: I need to use some simple logic in post.php to output some sidebar content depending on what section the post is in - how do I get the section name of the post in post.php?

<?php  
    $section = perch_blog_post_field(perch_get('s'), 'sectionSlug', true);
    if($section=='special-offers') { //....do something different }
?>

Unfortunately, this doesn't work! Any ideas?

Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_blog_post_field does need some improvements. Either use perch_blog_custom() with skip-template or test for sectionID instead.

Thanks Drew, testing for sectionID does the job. Looking at the perch_blog_custom option with skip-template - how would that work?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The skip-template option returns the content as a PHP array, but that would include the section slug.

Adam - take a look at https://forum.grabaperch.com/forum/07-14-2014-finding-what-section-a-specific-blog-post-is-in

I had the same requirement and its probably solved the same way? ... Graham

Thanks for that Graham and Drew. It looks like a lot of heavy-lifting to use the perch_blog_custom / skip-template option, and it appears only to dump the sectionID and not the sectionSlug, so would it not be a lot simpler to test for sectionID using:

$sectionID = perch_blog_post_field(perch_get('s'), 'sectionID', true);