Aller au contenu
FR

Creating a Shortcode to Obtain a Post’s ID

Version audio de cet article

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