WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Getting Short Code to with get_page function

[Resolved] Getting Short Code to with get_page function

  • idkdesign
    Member

    @idkdesign

    Hello,

    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:

    <?php
    $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

    Home

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

    http://leadershipsports.com/wordpress/

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

    Thanks.

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

    @esmi

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

    idkdesign
    Member

    @idkdesign

    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…

    <?php
    $args=array(
      '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>
        <?php
        the_content();
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    idkdesign
    Member

    @idkdesign

    Thanks Michael! That did the trick.

    Jack Reichert
    Member

    @jackreichert

    You can also use the do_shortcode() function

    http://codex.wordpress.org/Function_Reference/do_shortcode

    radiofranky
    Member

    @radiofranky

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

    thanks

    radiofranky
    Member

    @radiofranky

    Thanks for this snippet!

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

    <?php
    $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 ‘[Resolved] Getting Short Code to with get_page function’ is closed to new replies.