NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Easy
Last modified : Oct 01 2022
Estimated reading time : 0 minutes, 46 seconds - 82 words
Share the post "Creating a Shortcode to Obtain a Post’s ID"
With this tutorial, we will create a shortcode that will allow us to fetch the ID of the current post. This shortcode is often referred to as current post id . For example, this can be used to fetch a param and get the value in order to stash it in a form.
We will need to add this shortcode into our functions.php file and will return the current post id.
//[thepostid] function thepostidfunc( $atts ){ global $post; return $post->ID; } add_shortcode( 'thepostid', 'thepostidfunc' );
Puis vous pouvez l’appeler de cette façon :
<?php echo do_shortcode( '[thepostid]' ); ?>