Why does this shortcode not render at all?

Why doesn’t this shortcode not render at all?

Is it because I have incorrectly nested the get_modified_date_by_id function inside the shortcode function? I grabbed the get_modified_date_by_id from Get date of last update outside of loop

No errors in the log; the shortcode is simply not rendered in pages.

Will this work for both posts and pages? Or for pages, do I need to change the $post = &get_post?

 // Page Last Modifed Date

    function last_modified_shortcode() {

           function get_modified_date_by_id( $post_id = 0, $format = "F j, Y" ){
            global $post;
            $post = &get_post( $post_id );
            setup_postdata( $post );

            $modified_time = get_the_modified_time( $format );

         wp_reset_postdata( $post );

            return $modified_time;
            }
        }
      add_shortcode( 'last_modifed', 'last_modifed_shortcode' );

Update 12/05/16:

This works:

// Page/post Last Modifed Date

function get_modified_date_by_id(){

        global $post;
        $post = &get_post( $post_id );
        setup_postdata( $post );

        $format = "F j, Y";
        $modified_time = get_the_modified_time( $format );

        wp_reset_postdata( $post );

        return $modified_time;
    }

  add_shortcode( 'last_modifed', 'get_modified_date_by_id' );

1

Answers


  1. Mark Kaplun
    1 Votes

    Your function do not return anything, it just defines another function and exists. Your code is confused, there should not be many reasons to define a function inside of another function, and you should use a closure in most places it seems to make sense to do such a thing.

Please signup or login to answer this question.

Signup for an account and start participating in our site today!




Social Signup

You can also login with your Facebook, Twitter or Linkedin account

Log in with Facebook