Forum

Thread tagged as: Question, Meta, Blog

Display the blog post category through variable

Hey, so iv got my blog post tags working as so:

$tag         = ($post['0']['postTags']);

How do I get my categories showing also?

This code is currently in my post.php in header.

Thankyou

Kieran Hunter

Kieran Hunter 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What's in $post ?

It's the standard blog post stuff, as in your open graph meta tutorial.

Will this help?

My post

    $post = perch_blog_custom(array(
    'filter'        => 'postSlug',
    'match'         => 'eq',
    'value'         => perch_get('s'),
    'skip-template' => 'true',
    'return-html'   => 'true',
));

my portfolio

    $product_details = perch_content_custom('Portfolio', array(
     'template' => 'portfolio_item.html',
     'filter' => 'slug',
     'match' => 'eq',
     'value'         => perch_get('s'),
     'skip-template' => 'true',
     'return-html'   => 'true',
));
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this output?

print_r($post);
Array ( [0] => Array ( [image] => /perch/resources/perch-logo-1.png [description] => My main reason that I want to move my site from WordPress to Perch is that im hooked on making my site the best it can be. In terms of speed and efficiency. [categories] => Array ( [0] => blog/news/ ) [itemID] => 29 [postID] => 29 [postTitle] => Migrating from WordPress to Perch CMS [postSlug] => migrating-from-wordpress-to-perch-cms [postDateTime] => 2015-03-08 00:26:00 [postDescRaw] =>

Ok, so by that ouput I used [categories] like so:

$cat         = ($post['0']['categories']);

and I get:

<meta property="article:section" content="blog/news/" />

It spits out the URL not the name, so we're nearly there

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can get all of the fields for a category from its path using perch_category()

perch_category('blog/news/');

How do I implement that in my code, as above?

I have this as my variables, they all work except the og_cat

    perch_page_attributes_extend(array(
    'og_description' => $description,
    'og_title'       => $title,
    'og_type'        => 'article',
    'og_image'       => $fbimage,
    'og_author'      => 'https://www.facebook.com/kieranfivestars',
    'og_tag'         => $tag,
    'og_cat'         => $cat,
));

then my $cat field is

    $cat         = ($post['0']['categories']);

Its for the meta data in my head.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this give you?

foreach($cat as $categoryPath) {
    perch_category($categoryPath);
}

Do I just drop it in the post.php? it doesn't change anything.

Drew McLellan

Drew McLellan 2638 points
Perch Support

It needs to go somewhere after you've defined and populated $cat.

It gives me the correct category wrapped inside h4 tag and a ul

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, great. If you add skip-template ?

I can't work out where I would put that in the code given?

Cheers

Drew McLellan

Drew McLellan 2638 points
Perch Support

In the options for perch_category.

So like this?

foreach($cat as $categoryPath) {
 perch_category($categoryPath, array(
 'skip-template' => 'true',
));
}
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, but assign the result to something:

$categories = array();
foreach($cat as $categoryPath) {
$categories[] = perch_category($categoryPath, array(
 'skip-template' => 'true',
));
}

I've given up on this, it's not worth the effort for something not that important, its just holding me back.