Forum

Thread tagged as: Blog

Finding what section a specific blog post is in?

I have some working code that checks if a blog post is in a specific category so I can then do something extra within the page, like this:

post.php:

$var = trim(perch_blog_post_categories(perch_get('s'), '_category_check1.html', true));

_category_check1.html:

<perch:if id="categoryTitle" match="eq" value="Specific Heading">true</perch:if>

I want to do something similar, but instead I need to check if a blog post is in a specific section.

I've created _section_check1.html ...

<perch:if id="sectionTitle" match="eq" value="Specific Heading">true</perch:if>

I can't seem to get the right working code for 'post.php' so I can set the variable if that heading's found. Anyone advise please? ... Graham

Graham Street

Graham Street 17 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

$var = perch_blog_custom(array(
    'filter' => 'postSlug',
    'match' => 'eq',
    'value' => perch_get('s'),
    'skip-template' => true,
));
if (count($var) && $var[0]['sectionSlug']== 'specific-heading') {
    ... all good in the hood ...
}

I'm unable to get this working and have emailed info to hello@ - because the date/code I need to supply was quite lengthy and contained info I didn't really want public. Hope you can tie up my email with this and put me straight. I must be missing something really obvious I think.

I've now set this up in a local hosting setup so I can get better diagnostics.

This code ...

 if (count($var) && $var[0]['sectionSlug']== 'specific-heading') {
     ... all good in the hood ...
 }

... generates this error ...

 Array
 (
     [type] => 8
     [message] => Undefined index: sectionSlug
     [file] => /Users/xxxxx/Documents/websites/xxxxx.com/xxxxx/post.php
     [line] => 25
 )
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this give you?

print_r($var);

The posting I was looking at was huge so set up a test/short one instead ...

Array ( [0] => Array ( [excerpt] => Array ( [raw] => Some text for the excerpt. [processed] =>
Some text for the excerpt.

) [metaTitle] => Test Text [postID] => 74 [postTitle] => Test posting [postSlug] => 20140718-test-posting [postDateTime] => 2014-07-18 13:28:00 [postDescRaw] => Some test text for the posting [postDescHTML] =>
Some test text for the posting

[postDynamicFields] => {"excerpt":{"raw":"Some text for the excerpt.","processed":"
Some text for the excerpt.<\/p>"},"metaTitle":"Test Text"} [postTags] => [postStatus] => Published [authorID] => 1 [sectionID] => 2 [postCommentCount] => 0 [postImportID] => [postLegacyURL] => [postAllowComments] => 1 [postTemplate] => post.html [perch_excerpt] => Array ( [raw] => Some text for the excerpt. [processed] =>

Some text for the excerpt.

) [perch_metaTitle] => Test Text ) )

Looking at that output, picking up the 'sectionID' would probably be all I need as its now not likely to change. So, is it ...

 if (count($var) && $var[0]['sectionID']== '2') {
     ... all good in the hood ...
 }
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that should work. Not working?

Yes, that got it. Many thanks. The blog is now switched over to sections, leaving categories for the use intended.