WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting read more of child pages to show on static homepage (8 posts)

  1. Calicosun
    Member
    Posted 4 years ago #

    I would be very grateful if someone could help me out with the following question:

    What code would I use to get the 'read more' portion of a child page (not post) to display on my home page?

    I have a page called 'About' and two child pages under the 'About'. I want the title and the first few lines of each of the child pages to display in a small area on my home page with a 'read more' link back to their full pages. I have used the read more insert within the page to split my copy and have included the:

    <?php global $more; $more = 0; ?> but have only been able to get the read more excerpt to show with posts - not pages, and not child pages.

    I have read the read more pages within codex but as I am still learning php and wordpress I don't find them helpful at all - just confusing. I never understand how to incorporate the suggested code and what to use before and after as they don't give full real life examples.

    I am working locally so am unable to provide a link to the site - not that there is anything to see as, in spite of spending over 8 hours trying to solve this problem, I have not even been able to come up with a 'sort of works' solution.

    I would really appreciate someone helping me with this. I am not having much luck getting any responses when posting on this forum which is becoming quite frustrating as that was one of the top reasons for choosing to use wordpress. I always try the best I can to find the answer before coming here (often several days or more) so I don't waste anyone's time. Please, please, please can someone help me?

  2. Calicosun
    Member
    Posted 4 years ago #

    Ok. I have managed to get the child page to display on my homepage (in a php file called home-features.php) using the following code:

    <?php
    global $more;
    $more = 0;
    ?>

    <?php
    $pageChildren = get_pages('child_of=2');
    if ( $pageChildren ) ;
    foreach ( $pageChildren as $pageChild ) ;

    echo '<h2>ID) . '">'. $pageChild->post_title.'</h2>';

    echo '<p>'. $pageChild->post_content.'</p>';

    ?>

    I am not sure if this is the right way to go about it and I can't for the life of me figure out how to get just the copy above the 'read more' insert within the page to display.

    Here is a link to the static draft site: http://gridmaster.slicetest.co.nz/ The read more excerpts need to go where the orange box is, bottom right, just above footer.

    Can anyone help me with this please?

    Thanks.

  3. Calicosun
    Member
    Posted 4 years ago #

    Another update to above, only the first child page is displaying so there is still something wrong with my code.

    I know there are other ways to achieve what I want to achieve using custom fields (although don't know how to do a 'read more' with custom fields, and using posts instead of pages but, without going into details, I need to use pages for this.

    I am exhausted, been sitting at my computer trying to work this out for over 15 solid hours. This website needs to get finished urgently but I just don't have the energy to keep looking tonight. I am hoping with all my heart a magic little fairy will have posted some helpful suggestions when I wake up tomorrow... otherwise I am stuffed.

    I am not even sure if this has been posted in the right page... just my luck it isn't. If someone comes across it and knows it is in the wrong place I would appreciate if you could let me know so I don't sit staring at my computer all day tomorrow waiting for an answer that is never going to come.

  4. MichaelH
    Member
    Posted 4 years ago #

    Using the WordPress Default theme, put this at the top of a Page Template and it worked just fine:

    <?php
        $args=array(
          'post_type' => 'page',
          'post_parent' => 93,
          '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() ) {
          echo 'List of Child Pages for Page ID 93';
          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
            global $more; $more = false;
            the_content('Read on....');
          endwhile;
        }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  5. Calicosun
    Member
    Posted 4 years ago #

    Oh thank you so much! Magic fairies do exist! It works perfectly. I can't tell you how grateful I am for your help on this. Bleary eyed I stumbled down to my computer this morning, with my heart pounding, I went to check my RSS feeds - even closed my eyes at first as was too scared to look! I am still learning php (very early stages) hence my ignorance. I am slightly past the stage of when looking at code my eyes glaze over and blocks of code turn into one big fuzzy blob, forcing me to constantly re-focus. Seeing this has helped me to understand things a bit more. Once thing I think I was doing was, when looking at example code that read like this: post-type=>ID I would put exactly that, didn't realise I needed to put the page id in. That was answered by another kind soul somewhere in cyber space.

    Thank you so much.

  6. Calicosun
    Member
    Posted 4 years ago #

    Hi Michael I do have two other quick questions relating to this if you have time to answer:

    1. I know (well I think I know) the global $more; $more = false; and wp_reset_query(); // Restore global post data stomped by the_post() relate to the fact I want to post a read more on a page not a post. I just don't really understand what exactly they do. I read in the codex you were suppose to put global $more; $more = 0; somewhere in the code (didn't understand where excactly or how to use it ie. which the php or without etc) which is similar to what you have done. Could you give me brief run down on what this does?

    2. How do we enter the code in the separate frame as you have done above? Or is that just something moderators can do? Yes, this is a 'newbie' question! I will probably cringe in years to come if I come across it.

    Thanks!

  7. MichaelH
    Member
    Posted 4 years ago #

    1. Putting that global $more;$more=false; 'fakes' the query out to allow the more to work--putting it right before the_content(); makes sure nothing else corrupts it.

    2. It's not a frame, it's code. Just click on the code 'quicktag' at the top of the text box. Need to do a code and end code. Back ticks (on my tilde ~ key), at the begin and end of a code block, does the same thing.

  8. Calicosun
    Member
    Posted 4 years ago #

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic