David S.
David S.

Reputation: 27

Shortcode with current page id

I've hit a wall with my little of knowledge of php.

Is there any way how to "put current page id" instead of particular id number in do_shortcode function in wordpress? Or is it matter of files inside of the plugin?

<?php echo do_shortcode('[plugin-review id="2"]'); ?>

I dont want to display content from page with id=2 on every page on my website when I put this code into a sidebar.

Thank you for a possible solution.

edit: I have just also found this function (there is more functions with same content) which might be maybe helpful?

public function do_shortcode_ratings( $atts ) { extract( shortcode_atts( array( 'id' => '', 'template' => '', 'post' => get_the_ID() ), $atts ) );

    $shortcode = '[plugin-review id="'. $id .'" branch="ratings" post="'. $post .'" template="'. $template .'"]';

    return do_shortcode( $shortcode );

}

Upvotes: 2

Views: 6654

Answers (2)

Samyappa
Samyappa

Reputation: 531

You can follow, this one

global $post;
$current_id = $post->ID;
echo do_shortcode('[plugin-review id="'.$current_id.'"]');

Upvotes: 0

prettyfly
prettyfly

Reputation: 3130

Current post/page ID is available via the global $post variable, i.e.

global $post;
$current_id = $post->ID;

Now you can use the $current_id in your shortcode :)

Upvotes: 2

Related Questions