• ryanve

    (@ryanve)


    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 ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    (@alchymyth)

    have you read the codex: http://codex.ww.wp.xz.cn/Shortcode_API

    Thread Starter ryanve

    (@ryanve)

    Of course 🙂
    I already tried

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

    but no dice.

    Michael

    (@alchymyth)

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

    Thread Starter ryanve

    (@ryanve)

    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;
    	}
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘shortcode attribute default value post->ID’ is closed to new replies.