WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. idkdesign
    Member
    Posted 4 years ago #

    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

    http://leadershipsports.com/wordpress/?page_id=2

    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.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 4 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
    Member
    Posted 4 years ago #

  4. idkdesign
    Member
    Posted 4 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
    Member
    Posted 4 years ago #

    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().
    ?>
  6. idkdesign
    Member
    Posted 4 years ago #

    Thanks Michael! That did the trick.

  7. Jack Reichert
    Member
    Posted 3 years ago #

    You can also use the do_shortcode() function

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

  8. radiofranky
    Member
    Posted 3 years ago #

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

    thanks

  9. radiofranky
    Member
    Posted 3 years ago #


  10. maxemil
    Member
    Posted 3 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic