The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] get_post_class (3 posts)

  1. jasonsweb
    Posted 3 years ago #


    I've read the WordPress codex and Googled for hours, but I can't get the "get_post_class" to work in my shortcode.

    I would like to convert this code into a shortcode valid code:
    <div id="post-1027" <?php post_class('custom-class'); ?>>

    At the moment I have this:
    <div id="post-1027" ' . get_post_class('custom-class', $post->ID) . '>

    But unfortunately this returns an empty class, instead of the complete post classes.

    What am I doing wrong here?
    Thanks in advance!

  2. Michael
    Forum Moderator
    Posted 3 years ago #


    the function returns an array of post_classes; you need to convert them to a string and add the 'class=" "' part;

    example (untested):

    <div id="post-1027" class="' . implode(' ',get_post_class('custom-class', $post->ID)) . '">


    (if you have problem with getting the right post_classes, you might also need to add global $post; at the start of the shortcode function)

  3. jasonsweb
    Posted 3 years ago #

    Tested your code and it works perfect!

Topic Closed

This topic has been closed to new replies.

About this Topic