The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] Getting Short Code to with get_page function (10 posts)

  1. idkdesign
    Posted 6 years ago #


    I created a page called "About Us" that is also used for the home page intro text with post Loop.The reason for this is so the intro content can be edited via the WP admin.

    I used the following code to include it on the home page:

    $page_id = 2;
    $page_data = get_page( $page_id );
    $content = $page_data->post_content;
    echo $page_data->post_content;

    Everything works as intended except for the gallery short code:

    [gallery link="file"]

    On the "About Us" page it renders the gallery as intended. See


    However on the home page, it does not render the gallery but renders the short code text "[gallery link="file"]" See


    Any suggestions on how to get WP to render it properly in the home page?


  2. esmi
    Forum Moderator
    Posted 6 years ago #

    It's because you're using $page_data->post_content which is the raw, unfiltered page content. You might be better off using get_posts() to grab the page data and then using the_content().

  3. MichaelH
    Posted 6 years ago #

  4. idkdesign
    Posted 6 years ago #

    Not sure if I am doing it right but would the code be:

    <?php get_posts('2');
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;

  5. MichaelH
    Posted 6 years ago #

    Version of what esmi recommended...

      'page_id' => 2,
      'post_type' => 'page',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    wp_reset_query();  // Restore global post data stomped by the_post().
  6. idkdesign
    Posted 6 years ago #

    Thanks Michael! That did the trick.

  7. Jack Reichert
    Posted 6 years ago #

    You can also use the do_shortcode() function


  8. radiofranky
    Posted 6 years ago #

    I was wondering how do you limiting the page to display?
    also, limiting display no child or grandchild?


  9. radiofranky
    Posted 6 years ago #

  10. maxemil
    Posted 6 years ago #

    Thanks for this snippet!

    Im using it to generate content inside a sub-page list, like

    $pages = get_pages('child_of='.$post->ID.'&parent='.$post->ID); $count = 0;
    foreach($pages as $page)
      // the above code

    After having tried other fancy "list subpages" variations, this seems to be the right one.
    Previously I had a filter around the_content, which dident leave me any meta data inside the_content();
    In return shortcodes could run, but if I did a the_title in the shortcode enabled function, it would return the parent title.

    The MichaelH written piece works a charm :)

Topic Closed

This topic has been closed to new replies.

About this Topic