Site logo

Archived topic

GP Hooks and List Category Posts shortcode

3 replies · Started by Chris on July 18, 2019

Viewing posts 1–4 of 4

The List Category Posts shortcode works just fine in GP Hooks like this:

[catlist name="cat1" date="yes"]

However, when the name is passed as a PHP variable via the 'After Content' GP Hook, the shortcode does not work. Here's my code:

<?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) {
        if ($category_slug == 'cat1') {
            echo ('<br>');
            echo do_shortcode ('[catlist name="' . $category_slug . '" date="yes"]');
            echo('(Category slug = ' . $category_slug . ', Post slug = ' . $post_slug . ')');
        } else {
        echo ('<br><br><h2>' . $category_name . ' News</h2>');
        echo do_shortcode ('[catlist name="' . $category_slug . '" date="yes"]');
        }
    }
}
?>

You can see an example of the correct and incorrect outputs (provided in the Website URL field below)

I've switched off the cache too to exclude that as the issue. And, I've sought help from the plugin developer, but they were unable to help: https://wordpress.org/support/topic/catlist-in-php-not-working-correctly/

Your help would be greatly appreciated.

Thanks,

Chris

Hi there,

That looks like it should work.

Are we sure $category_slug is outputting the correct text?

Hi Tom,

Thanks for the reply.

As it happens, I fixed the problem late last night by running the exact same code in a PHP snippet plugin and then placing the snippet shortcode in the GP Hook.

However, when running the PHP directly in the GP Hook, it does not work. And yes, $category_slug is outputting the correct value, as can be seen from the additional echo I added for debug purposes after the shortcode. I also tried var_dump($category_slug) and that gave the correct value.

So, unresolved anomaly but a workaround has been found.

Thanks again for the reply.

Chris

Very strange - it's possible that GP Hooks is having a tough time running the variable inside do_shortcode(). Have you tried a Hook Element?

This archived topic is closed to new replies.