Support » Plugins » Hacks » How to insert a post into a page using a shortcode?

  • Hi,

    I am using wordpress as our CMS for our companies website.
    We have about 5-10 pages which we want to insert the same call to action content in.

    How can I create a shortcode that will allow me to embed the content from a post into a page?

    Thanks
    Steven.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Inside your shortcode handling function or method (http://codex.wordpress.org/Shortcode_API) you would have to create a custom WP_Query object (http://codex.wordpress.org/Class_Reference/WP_Query) like this

    function myCustomShortcodeHandler( $atts ){
    
      $CustomQuery  = new WP_Query( array( 'post__in' => array( 1, 2, 3 ) ) ); // Insert YOUR post IDs here
    
      wp_reset_query();
    
      foreach( $CustomQuery->posts as $Post ) {
    
        /**
         * $Post->ID
         * $Post->post_title
         * $Post->post_excerpt
         * $Post->post_content
         * ...
         */
      }
    
      return;
    
    }
    
    add_shortcode( 'myCustomShortcode', 'myCustomShortcodeHandler' );

    Thanks, but how do I use this shortcode?

    Moderator bcworkz

    (@bcworkz)

    To use shortcodes, using the page or post editor, enter the tag name inside square brackets wherever you want the shortcode output to occur. Specifically for your situation using Chris’ example, using the page editor, type [myCustomShortcode] where your want the posts 1, 2, & 3 to appear on your page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to insert a post into a page using a shortcode?’ is closed to new replies.