• Resolved ChrisL

    (@chrslcy)


    When I run the following code on a page with the slug ‘pta’, the catlist shortcode functions correctly:

    <?php if( is_page('pta')): ?><br><br><h2>PTA News</h2>[catlist name="pta" date="yes"]<?php endif;?>

    However, when I replace it with the following code, on the same page, the catlist shortcode does not provide a filtered list but instead outputs a list of ALL recent posts:

    <?php
    $categories = get_categories();
    global $post;
    $post_slug = $post->post_name;
    foreach ($categories as $category) {
        $category_slug = $category->slug;
        $category_name = $category->name;
        if ($category_slug == $post_slug) {
            echo ('<br><br><h2>' . $category_name . ' News</h2>');
            echo do_shortcode ('[catlist name="' . $category_slug . '" date="yes"]');
        }
    }
    ?>

    Is this a bug or something I’m doing wrong?
    Any and all assistance appreciated.

    Thanks!

    Chris

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor zymeth25

    (@zymeth25)

    Hi @chrslcy

    Try hardcoding the category slug in the shortcode and see what happenes, eg:
    echo do_shortcode ('[catlist name="mycategory" date="yes"]');

    Also, for debugging, would be good to try var_dump($category_slug); before you echo the shortcode.

    Thread Starter ChrisL

    (@chrslcy)

    Thanks very much for the reply.

    I’ve just tried hardcoding the category_slug, and it gives the correct output.

    Adding var_dump($category_slug); before the echo shortcode gives this:

    string(3) "pta"

    ?@#!

    Plugin Contributor zymeth25

    (@zymeth25)

    So var_dump gives expected result but somehow the result is different if you use string concatenation instead of hardcoding. That’s strange, I’ll see if I can reproduce the issue. For now you can try gettng the id from the category object ($category->cat_ID) and see if that works.

    Plugin Contributor zymeth25

    (@zymeth25)

    I ran a few tests but was unable to reproduce the issue, when I construct the shortcode string with a variable the result is correct. Your issue looks bizzare to me, the only explanation for the shortcode showing all posts regardless of categories is if the string you pass as $category_slug is either empty or wrong, but you have found out yourself that it is correct.

    Does this occur on every page? Try different pages, like single post pages, archive pages etc and see if there is any difference in behavior.

    • This reply was modified 7 years, 1 month ago by zymeth25.
    Thread Starter ChrisL

    (@chrslcy)

    This is odd indeed.

    I’ve tried it now on different pages, a single post and an archive page. The result is consistent: no problem with hard-coded value of slug in shortcode; but the shortcode won’t recognise value of PHP object, despite confirming that the object value is correct and not null. ??

    Oh well. I’ll keep fishing around.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[catlist] in PHP not working correctly’ is closed to new replies.