4

I'm new at this.

I need a shortcode that returns the post id of the post in which the shortcode is inserted.

If someone could provide that it would open the doors of understanding for me :)

Much appreciated!

2
  • Look at the left side of my answer, there is a "right" sign. Please click on that. @James
    – CodeMascot
    Dec 12, 2016 at 15:57
  • I assume that is the check mark :) consider it clicked and thanks :)
    – James
    Dec 12, 2016 at 19:07

2 Answers 2

7

Place the below code to your themes functions.php or inside your plugin and the [return_post_id] shortcode will print the post ID.

add_shortcode( 'return_post_id', 'the_dramatist_return_post_id' );

function the_dramatist_return_post_id() {
    return get_the_ID();
}

Hope that helps.

2
0

Shortcode: Embeddable Post Excerpts

After installing the shortcode (follow steps 1 + 2 below) you can execute it anywhere WordPress accepts shortcodes. To connect to a post, you simply need to enter the post ID you wish to display:

https://marketersdelight.net/snippets/embeddable-excerpts/

1
  • Link is ok, but you should also provide some code in case that someday the link will stop working and your answer becomes useless.
    – Picard
    Jul 14, 2017 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.