WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_post_class (3 posts)

  1. jasonsweb
    Member
    Posted 1 year ago #

    Hi,

    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. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/get_post_class

    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)) . '">

    http://php.net/manual/en/function.implode.php

    (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
    Member
    Posted 1 year ago #

    Thanks!
    Tested your code and it works perfect!

Topic Closed

This topic has been closed to new replies.

About this Topic