Search code examples
phpwordpressshortcode

Simple shortcode $id+$title with small html


I have a simple need to put a shortcode at the end of my articles on wordpress with the post id and post title. I want to add a (ref:) in front of $id and enclose it all in h4

the desired result look like this

<h4 ref: 123 this is my first post <h4>

I'm having a hard time getting the logic of how to do it

my shortcode

function post_id_title( ){
    $id = get_the_ID();
    $title = get_the_title();
    $result = $id . $title ;
   return $result;
}
add_shortcode( 'page_title', 'post_id_title' );

Solution

  • Try this since you got the hardest part right.

    function post_id_title( ){
        $id = get_the_ID();
        $title = get_the_title();
        $result = "<h4>ref: $id $title</h4>";
       return $result;
    }
    add_shortcode( 'page_title', 'post_id_title' );