Wordpress -

Hi,

I’m creating quite a complex site based on the Wordpress framework and I have run into a problem that I cannot seem to solve…

My site http://icomprintmanagement.co.uk.s88828.gridserver.com/healthcare/ has a section toward the bottom which holds lots of Parent page categories, (i.e accident & emergency is the parent page and there are several children within it) and nested within them are product pages, what I’m trying to do is specify the page and pull all the product pages into a list. Currently I have tried to do it on the first product Category “medical Records” using the following PHP:

<div class="grid_2 productColumn">
			<h2><a href="/healthcare/medical-records/" title="medical records">Medical Records</a></h2>
			<?php
			  $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=1');
			  if ($children) { ?>
			  <ul>
			  <?php echo $children; ?>
			  </ul>
			 <?php } ?>
			


		   </div><!-- End of product Column 1Div -->

Unfortunately as you can see on my site, it is pulling the whole list of product categories and product pages,

How do I isolate the list to just a specific product category parent page and then list its children below it?

Any help would be great with this, I have tried reading the codex but I can’t seem to find the right information.

Thank you

O

Hi Chris,

I knew thats what I needed to do, I just didnt know how I would present that value…

So am I right in saying that if I had a page with the id of 43 and I wanted to display it children then the code would be:

<?php

              $children = wp_list_pages('title_li=&child_of='.$post->43.'&echo=1');

              if ($children) { ?>

              <ul>

              <?php echo $children; ?>

              </ul>

             <?php } ?>

            

or is it $post->ID43 ?

Thanks for your help Chris

Can anyone help me with this?

I think your code is correct, but you are using this code: $post->ID which will grab the current post id.

If you want to display all of the children under a certain page, you have to use the correct page id. You can get the page id from the admin by editing the page and it should be the number at the end of the url. That is what you need to put in place of the $post->ID value.

No, that is not correct. You do not want to use a php variable.

It should look like this:

$children = wp_list_pages('title_li=&child_of=43&echo=1'); 

From the source you showed I take it you manually create the Parent link.
I’m assuming the title of the Medical Records page is named “Medical Records”.


<?php 
$parent = get_page_by_title('Medical Records');
$children = wp_list_pages('title_li=&child_of='.$parent->ID.'&echo=1'); 
if ($children) { ?> 
<ul> 
    <?php echo $children; ?> 
</ul> 
<?php } ?> 

That should do it.

You don’t have to create the main links by hand. It could all be wrapped in a loop, but I would need a bit more info to show you an example :slight_smile:

Peter van der Does

I thought he wanted to always pull the same children. He definitely will want to use your code if he wants the children of the current page displayed.

It’s not the children of the current page that are being pulled.
I request the information for a particular page, in this case the page with the title “Medical Records”.

The only difference between my code and yours is that you don’t have to hard code the page ID. I think it would make the code a bit more readable and you can create a loop to build the overview.

For example:


// The parents titles you want to show
$page_title = array ('Medical Records', 'Finance' );
foreach ( $page_title as $title ) {
	$page = get_page_by_title( $title );
	$url = get_permalink( $page->ID );
	echo '<div class="grid_2 productColumn">';
	echo '<h2>';
	echo '<a href=' . $url . ' title=' . $page->post_title . '">' . $page->post_title . '</a>';
	echo '</h2>';
	$children = wp_list_pages( 'title_li=&child_of=' . $parent->ID . '&echo=1' );
	if ( $children ) {
		echo '<ul>';
		echo $children;
		echo '</ul>';	
	}
	echo '</div><!-- End of product Column 1Div -->';
}

Oh duh. I didn’t take a close enough look at the code you provided. :slight_smile:

But wouldn’t it be better to use the page id, since the page id is not going to change? My concern would be if the page title changed, then your code wouldn’t work.

With this idea, this is how I would code it (using your code):

// The parents titles you want to show
$page_id = array (15, 23); // the page ids of Medical Records and Finances
foreach ( $page_id as $current_pageid ) {
    $page = get_page( $title );
    $url = get_permalink( $current_pageid );
    echo '<div class="grid_2 productColumn">';
    echo '<h2>';
    echo '<a href=' . $url . ' title=' . $page->post_title . '">' . $page->post_title . '</a>';
    echo '</h2>';
    $children = wp_list_pages( 'title_li=&child_of=' . $current_pageid . '&echo=1' );
    if ( $children ) {
        echo '<ul>';
        echo $children;
        echo '</ul>';   
    }
    echo '</div><!-- End of product Column 1Div -->';
}

I’m not 100% sure this would work because I haven’t tested the code. Does…thanks for the improved code. :slight_smile:

yeah you’re right it makes sense to use the ID’s instead of title.
And like you did, make sure you do a comment on which pages you’re pulling.

Hi Both thanks for your help,

@does: yes ideally what I would like to do is pull the whole overview via a loop so that that section is automated depending on what categories I set up in the control panel. Then within each category the children would also be automated as your solution above is trying to do.

So to recap, The area at the bottom of the page has 12 main category pages, i.e “medical records” being one of the 12. Within that category (and every other) I would like it to display the children in a list related to that category. I would like the whole process to be automated so that If I decided to change the name of one of the parent category pages from say “medical records” to “medical Files” in the back end then it would change in the area at the bottom of the page. And like wise for the children pages within each Main parent.

Does this make sense?

Correct.

And go with the solution of using the ID’s to pull the pages. That way when you do change the name it will all still work.

Chris’s code had one little bug, so here’s the new code:

// The parents ID's you want to show 
$page_id = array (15, 23); // the page ids of Medical Records and Finances 
foreach ( $page_id as $current_pageid ) { 
    $page = get_page( $current_pageid); 
    $url = get_permalink( $current_pageid ); 
    echo '<div class="grid_2 productColumn">'; 
    echo '<h2>'; 
    echo '<a href=' . $url . ' title=' . $page->post_title . '">' . $page->post_title . '</a>'; 
    echo '</h2>'; 
    $children = wp_list_pages( 'title_li=&child_of=' . $current_pageid . '&echo=1' ); 
    if ( $children ) { 
        echo '<ul>'; 
        echo $children; 
        echo '</ul>';    
    } 
    echo '</div><!-- End of product Column 1Div -->'; 
} 

Thanks for your help Does,

I’m no php expert, merely a designer trying to get to grips with wordpress. this code will be a great help…

I’l let u know how I get on with it…

thank you

O

Cheers Guys, this worked a treat,

thanks again

Out of curiosity, has anyone had the 404 page not found error when submitting comments on their blog?

The comment is added to the site but it gets redirected to a 404 page…

You can see this in action here

Hi Both, Sorry to revisit this, but I have noticed that the code you gave me works great but it is not validating.

According to W3c Validator the lists that are being created are being displayed without the <ul></ul> tags wrapped around the list items…

the error I get is:

#  Error  Line 352, Column 36: document type does not allow element "li" here; missing one of "ul", "ol", "menu", "dir" start-tag

&lt;li class="page_item page-item-179"&gt;&lt;a href="http://icomprintmanagement.co.uk.s&#8230;

The mentioned element is not allowed to appear in the context in which you’ve placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you’ve forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as “<p>” or “<table>”) inside an inline element (such as “<a>”, “<span>”, or “<font>”).

The code echo’s out the <ul> tags before and after the li items but for some reason they do not show up in the page when it is brought from the server…

if you view the source of the following page you will see what I mean,

the php code in question is this part:-

if ( $children ) {
echo ‘<ul>’;
echo $children;
echo ‘</ul>’;

Any idea why they are not displaying?

regards

Owain