Forums

shortcode attribute default value post->ID (5 posts)

  1. ryanve
    Member
    Posted 12 months ago #

    I have a shortcode [user-profile] where the author ID is the attribute. I'd like to make it so that if no ID is specified [user-profile] defaults to post author, and when an ID is specified, like [user-profile id="47"], it uses that ID. The shortcode function right now is like this:

    function user_profile( $atts, $content = null ) {
    	extract(shortcode_atts(array('id' => ''), $atts));
    	include ('user-profile.php');
    	return $user_hcard;
    	}

    ...which works only when an ID is specified.
    (user-profile.php code is here)
    How can I set the default to be the $post->ID ?

  2. alchymyth
    The Sweeper
    Posted 12 months ago #

    have you read the codex: http://codex.wordpress.org/Shortcode_API

  3. ryanve
    Member
    Posted 12 months ago #

    Of course :)
    I already tried

    extract(shortcode_atts(array('id' => $post->ID), $atts));

    but no dice.

  4. alchymyth
    The Sweeper
    Posted 12 months ago #

    as this is in functions.php, you may need to use global $post right at the beginning of the function.

  5. ryanve
    Member
    Posted 11 months ago #

    This works—killer! Thanks =)

    function user_profile( $atts, $content = null ) {
    	global $post;
    	extract(shortcode_atts(array('id' => ''), $atts));
    	include ('user-profile.php');
    	return $user_hcard;
    	}

Reply

You must log in to post.

About this Topic