Support » Fixing WordPress » Getting Short Code to with get_page function

  • Resolved idkdesign



    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?


Viewing 9 replies - 1 through 9 (of 9 total)
  • esmi


    Forum Moderator

    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().

    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;

    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().

    Thanks Michael! That did the trick.

    You can also use the do_shortcode() function

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


    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 🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Getting Short Code to with get_page function’ is closed to new replies.