Forum

Thread tagged as: Question, Problem, Configuration

Perch Blog — related posts from same category

Hi there,

I've followed a series of posts from the forums on getting related posts working.

I have a set called 'news' for which there are two categories: Athlete Spotlight and Articles. If I run the following I get some errors.

//Get the categories for the current post 
$categories = perch_blog_post_categories(perch_get('s'), array(
       'skip-template'=>true, ));
print_r($categories);

//The post may be in more than one category, so we need to loop through these and get the category slugs
if (count($categories)) {
 $cat_slugs = array();

 foreach ($categories as $cat) {
    $cat_slugs[] = $cat['catSlug'];
    print_r($cat);
 }
}

The printed output it gives me is:

Array ( [0] => Array ( [desc] => Array ( [_flang] => markdown [raw] => Latest News [processed] =>

Latest News
) [catID] => 1 [setID] => 4 [catParentID] => 0 [catTitle] => Articles [catSlug] => articles [catPath] => news/articles/ [catDisplayPath] => Articles [catOrder] => 1 [catTreePosition] => 004-001 [catDynamicFields] => {"desc":{"_flang":"markdown","raw":"Latest News","processed":"

Latest News<\/p>"}} [perch_desc] => Array ( [_flang] => markdown [raw] => Latest News [processed] =>

Latest News
) [catDepth] => 1 ) ) Array ( [_flang] => markdown [raw] => Latest News [processed] =>

Latest News
) 140Articlesarticlesnews/articles/Articles1004-001{"desc":{"_flang":"markdown","raw":"Latest News","processed":"

Latest News<\/p>"}}Array ( [_flang] => markdown [raw] => Latest News [processed] =>

Latest News
) 1 

and in the perch debug I get this error:

Array
(
    [type] => 8
    [message] => Undefined index: catSlug
    [file] => /var/www/sitename/news/post.php
    [line] => 49
)

But the catSlug is the right reference to that information so I'm confused as how to its undefined. The posts are definiately all assigned to either articles or athlete spotlight.

Mathew Doidge

Mathew Doidge 2 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you edit each of your categories in turn? Just click save on them.

Hi Drew,

Just done that, although still appear to be getting the same error. I've also just tried deleting all my posts and re-adding some new ones to each category

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is that code above verbatim, or have you edited stuff out?

The array print_r are exactly as they come out.

My post page in it's entirety looks like:

<?php include('../perch/runtime.php'); ?>
    <?php 
        # get the post
        $post = perch_blog_custom(array(
              'filter'        => 'postSlug',
              'match'         => 'eq',
              'template'   => 'post.html',
              'value'         => perch_get('s'),
              'skip-template' => 'true',
              'return-html'   => 'true',
          ));

        # set up the variables
        $title       = $post['0']['postTitle'];
        $description = strip_tags($post['0']['excerpt']);

        # use the variables in the array value 
        perch_page_attributes_extend(array(
            'description'    => $description,
            'og_description' => $description,
            'og_title'       => $title,
            'og_type'        => 'article',
            'og_author'      => 'https://www.facebook.com/myfbpagename',
        ));

        perch_layout('header.blog', array(
            'title'           => perch_blog_post_field(perch_get('s'), 'postTitle', true) ,
            'blog' => true,
        )); 
    ?>

    <div class="post">

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

        <div class="post__right">
            <?php perch_layout('sidebar.blog'); ?>
            <?php
                //Get the categories for the current post 
                $categories = perch_blog_post_categories(perch_get('s'), array(
                       'skip-template'=>true, ));
                print_r($categories);

                //The post may be in more than one category, so we need to loop through these and get the category slugs
                if (count($categories)) {
                 $cat_slugs = array();

                 foreach ($categories as $cat) {
                    $cat_slugs[] = $cat['catSlug'];
                    print_r($cat);
                 }
                }

            ?>
        </div>

        </article>

        </div>

  </div> 


<?php perch_layout('footer'); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

I can't marry up what's happening between the first print_r and the second. What you have there should be correct as far as I can see.

Yeh me too. I will keep working away. Like you said though I've been through this a million time and everything looks fine - like it should work.

The only way I managed to get

perch_blog_custom(array( 
     'template'=> 'blog/post_in_list_related.html',
     'count'=> 2,
     'section'=> 'news',
     'filter' => 'postSlug',
     'match' => 'neq',
     'value' => perch_get('s'),
     'category' => $cat_slugs,
     'sort-order'=> 'RAND'
  ));

Is to change

$categories = perch_blog_post_categories(perch_get('s'), array('skip-template'=>true));

to

$categories = perch_blog_post_categories(perch_get('s'), true);

and I then see two stories from either category. Switch is back to the previous and I see nothing being output again. But causes the error:

Array
(
    [type] => 2
    [message] => Invalid argument supplied for foreach()
    [file] => /var/www/sitename/news/post.php
    [line] => 47
)

Should I able to to see catSlug in the showall output for the post?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You need to return an array (with skip-template) in order to be able to iterate over them.

When you loop through you should have each category, but somehow you end up with the description field.

Ill try make a fresh project this weekend in order to test this on a project where everything is set back to default and try track down what's happening from there. It must be something I've done within the project files for the blog that is causing this.

Just quick one - would using a set of categories under a set called 'news' rathern than 'blog' be a problem?

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, that's fine. If you want to use the blog functionality around categories you'd need to use the dedicated set, but you can do whatever you want to with it.