• Resolved fran330

    (@fran330)


    Hi,
    The shortcode containing PHP works on pages/posts but when I write
    [category-name]
    in a text widget or in a Custom HTML widget, it displays nothing, since version 3.0.2

    If I do the same with a shortcode containing HTML (no php), it works fine though.

    I am sure my php code is valid because it used to work.
    Here it is:

    $current_cat_id = the_category_ID(false);
    echo '<span>' . get_cat_name($current_cat_id) . '</span>';

    “Shrotcode” and “PHP code” are ticked.

    I hope you can help,
    Thanks in advance,
    Fran

    • This topic was modified 8 years, 3 months ago by fran330.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi Fran

    When reviewing your support request, I noticed that the_category_ID() is deprecated:
    https://developer.wordpress.org/reference/functions/the_category_id/

    Tested this on 2.5.4 and 3.0.0, it only worked when the shortcode was added in the Text widget and when the shortcode was shown on a post page. It does not work in other widgets or when not on a page/post in either version.

    Shortcodes in Text widget seem to be supported since WordPress 4.9. You might have been using another plugin or code snippet to allow shortcodes in widgets before that. Please check that, maybe they no longer work or cause conflicts because it is now in core.

    Widget Improvements in WordPress 4.9

    It doesn’t work on other pages then pages/posts because the function needs the post/page ID to get the category ID.

    You can download that old version from this page, install it and confirm if it does or does not work for you:
    https://wordpress.org/plugins/post-snippets/advanced/

    Kind regards
    David

    Thread Starter fran330

    (@fran330)

    OMG! I am so stupid and so sorry…
    I am building a new website and as I was testing stuff, I totally forgot that the concerned code was meant to appear on a post page only (and disply its own category). Of course, that’s the purpose of it…
    In fact, it works prefectly!

    Thank you for pointing out the deprecated code. I’ll look into that.

    Again, I am sorry for having taken your attention for nothing..
    Thank you for your response though 🙂
    Fran

    Hi Fran

    Not a problem!

    If you like Post Snippets and the support, please feel free to let us know in the reviews:
    https://wordpress.org/support/plugin/post-snippets/reviews/#new-post

    Kind reagrds
    David

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘PHP in shortcode doesn’t display on widgets’ is closed to new replies.