• I’m trying to write a plugin, not rocket science: just prints out the contents of a post:

    function my_post_function($atts)
    {
       $post_object = get_post($atts['post']);
       return $post_object->post_content);
    }
    
    add_shortcode('mypostdisplay', 'my_post_function');

    No this work ‘almost’ OK ??
    If I use something like:

    [mypostdisplay post=45]

    Then this displays the post
    BUT there is no formatting at all AND there are no carriage returns ??

    If I display in the middle of small text, then the output is also small text – whereas I just want it to be how it would normally show if I were displaying the post on its own

    Where am I going wrong?

    ALSO, I’ve tried the following:

    '<p>'.format_to_post($post_object->post_content).'</p>'
    I saw this being done in another plugin – but this seems to have no effect ??

    Thanks

    Omar

Viewing 3 replies - 1 through 3 (of 3 total)
  • you could try this –
    https://codex.www.ads-software.com/Function_Reference/apply_filters

    function my_post_function($atts)
    {
       $post_object = get_post($atts['post']);
    $text = $post_object->post_content;
    $text = apply_filters('the_content',$text)
       return $text);
    }
    
    add_shortcode('mypostdisplay', 'my_post_function');

    be aware that your rendered html might not validate if you drop the shortcode in the middle of an open html tag.

    Thread Starter OM2

    (@om2)

    thanks for the reply
    i tried that code and am getting an error thrown
    i removed the extra bracket that u have on the last line of the function – but i’m still getting the error

    what’s going wrong?
    let me know

    thanks

    Thread Starter OM2

    (@om2)

    solved it
    it was just missing a semi colon on the line: $text = apply_filters(‘the_content’,$text)

    be aware that your rendered html might not validate if you drop the shortcode in the middle of an open html tag.

    not sure why this would ever come about?
    can say a little more?

    thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Trouble displaying post using shortcode’ is closed to new replies.