WordPress.org

Ready to get started?Download WordPress

Forums

show child pages of parent page on homepage (12 posts)

  1. nyckidd
    Member
    Posted 2 years ago #

    hey guys,

    i have like 10 parent pages and each parent page has 15 child pages, i want to show the child pages of each parent page dynamically, so that when i a user selects say parent page A the page shows the child pages if they select parent page B the page will show the child pages from Page B....the page template will be the same just the content will be changing dynamically....i had this code but it is pulling all the child pages...

    <?php
    query_posts('post_type=page&posts_per_page=1&post_parent='.$parent);
    ?>   
    
    	<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    my content
    
    		<?php endwhile; ?>
    
    				<?php wp_reset_query(); ?>
  2. Jackson
    Member
    Posted 2 years ago #

    Where are you setting the $parent variable?

    If you wanted children of the current page, you could replace $parent with $post->ID

    You should be using a new WP_Query object instead of query_posts I am willing to bet.

    http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts

  3. nyckidd
    Member
    Posted 2 years ago #

    jackson, thanks that worked out pretty well....let me have a look at the WP_Query object see if it will help out...actually let me just rea the article you just linked to...thanks

  4. nyckidd
    Member
    Posted 2 years ago #

    got another bummer situation...

    my code looks like this below...what i would like is to show the parent page content on the top then the child page content at the bottom what do i need to tweak in the parent page query to make this happen coz as it is now its grabbing the latest parent page that i create which is not what i want...

    <?php
    query_posts('post_type=page&posts_per_page=1&post_parent='.$parent);
    ?>
    	<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    i want my parent page content here
    				<?php endwhile; ?>
    				<?php wp_reset_query(); ?>
    
    <?php
    query_posts('post_type=page&posts_per_page=5&post_parent='.$post->ID);
    ?>
    			<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    my content for the child pages
    				<?php endwhile; ?>
    				<?php wp_reset_query(); ?>

    and the other under

  5. Jackson
    Member
    Posted 2 years ago #

  6. nyckidd
    Member
    Posted 2 years ago #

    Hey jackson really appreciate your help am not a php novice so how and where exactly do i change/add/edit the code ?

    my home.php file looks like this..

    http://pastebin.com/v2R9WMPf

  7. Jackson
    Member
    Posted 2 years ago #

    What I pasted was really just a basic loop you'd have to build your HTML structure around.

    The home.php file you posted makes my head spin, I would start by straightening it up and then trying to fix the rest.

    I think where you're having trouble is by modifying the main query here:

    query_posts('post_type=page&posts_per_page=1&post_parent='.$parent)

    If the main query is already a single page view, then this line is not needed.

    In a nutshell, try removing lines 11-13 here - http://pastebin.com/v2R9WMPf

  8. nyckidd
    Member
    Posted 2 years ago #

    dude you roc bro!! that worked hope i dont come back here again brah!!

  9. nyckidd
    Member
    Posted 2 years ago #

    am back but not for the same thing...rather the orderby seems not to be working..

    i have it as

    <?php query_posts('post_type=page&orderby=asc&posts_per_page=5&post_parent='.$post->ID); ?>
    
    			<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

    but when i manually assign order id's to the magic field entries ie the child pages they dont adapt the order .....

  10. Jackson
    Member
    Posted 2 years ago #

    Probably because you have no arguments set for what to use for ordering, just the direction to order in.

    Do you mean custom fields when you say - "magic field entries"? Or are you referring to the page order field?

  11. nyckidd
    Member
    Posted 2 years ago #

    yeah when i say custom fields i mean the magic field entries...they are setup as pages so when i try to edit the page order field it does not apply on the pages..

  12. Jackson
    Member
    Posted 2 years ago #

    I'm not familiar with magic fields, but you need to specify the name of the magic field (which I believe is just a fancy custom field) you want to sort by.

    query_posts('post_type=page&order=ASC&orderby=meta_value_num&meta_key=YOURKEYNAME&posts_per_page=5&post_parent='.$post->ID);

    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    I can't stress enough how much you should be using WP_Query, this is _doing_it_wrong...

Topic Closed

This topic has been closed to new replies.

About this Topic