WordPress.org

Ready to get started?Download WordPress

Forums

How to insert a post into a page using a shortcode? (4 posts)

  1. steveonz
    Member
    Posted 2 years ago #

    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.

  2. Chris
    Member
    Posted 2 years ago #

    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' );
  3. steveonz
    Member
    Posted 2 years ago #

    Thanks, but how do I use this shortcode?

  4. bcworkz
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic