Forum

Thread tagged as: Question, Problem, Blog

Blog Category Custom Field

Hi guys,

I've added:

<perch:blog id="categoryColour" type="text" label="Colour" required="true" help="Format: #HEX" />

to the category.html blog template and now need to get that value within the blog post, i tried:

<perch:blog id="categoryColour" type="hidden" />

but that doesn't do anything, where am i going wrong!? or is that not possible?!

Thanks

ewe agency

ewe agency 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's a field belonging to the category, not the post.

If you want to show it along with the post you'll need to pass it into the template.

ok cool, gave it a try and managed to nail it! For anyone that comes across this:

<?php $cat_colour = (perch_blog_post_categories(perch_get('s'), 'category_colour.html', true)); PerchSystem::set_var('categoryColour', $cat_colour); perch_blog_post(perch_get('s')); ?>

The above doesn't work on the blog list (post_in_list.html) do i just need to tweak the code or something entirely different?!

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to handle it in the each callback

'each' => function($item) {
    $cat_colour = (perch_blog_post_categories(perch_get('s'), 'category_colour.html', true);
    $item['categoryColour'] = $cat_colour;
    return $item;
},

something like this?

               perch_blog_custom(array(
                'count' => 10,
                'template' => 'post_in_list.html',
                'sort' => 'postDateTime',
                'sort-order' => 'DESC',
                'each' => function($item) {
                $cat_colour = (perch_blog_post_categories(perch_get('s'), 'category_colour.html', true);
                $item['categoryColour'] = $cat_colour;
                return $item;
                }
            ));

The above doesn't work and just kills the page and nothing loads!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Check your PHP error log for the message.

ahh there was a missing ')' - page loads as normal but the colour doesn't show:

updated code:

perch_blog_custom(array(
                    'count' => 10,
                    'template' => 'post_in_list.html',
                    'sort' => 'postDateTime',
                    'sort-order' => 'DESC',
                    'each' => function($item) {
                     $cat_colour = (perch_blog_post_categories(perch_get('s'), 'category_colour.html', true));
                     $item['categoryColour'] = $cat_colour;
                     return $item;
                 }
                ));
Drew McLellan

Drew McLellan 2638 points
Perch Support

Which version of Blog are you on?

blog version 4.1

Drew McLellan

Drew McLellan 2638 points
Perch Support

That should work then. Hmm.

You're using categoryColour in post_in_list.html ?

yep

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

Drew McLellan 2638 points
Perch Support

Oh, hold on. This:

$cat_colour = (perch_blog_post_categories(perch_get('s'), 'category_colour.html', true));

should be:

$cat_colour = (perch_blog_post_categories($item['postSlug'], 'category_colour.html', true));

brilliant all working! Thanks :)