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

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]' ); ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>