Forums

[resolved] Displaying wp_list_pages AND Custom Fields (12 posts)

  1. bbellx
    Member
    Posted 2 years ago #

    I am using this code in a template to generate a list of pages that are children of the parent page:

    <?php
    if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    I got it from this page in the codex:
    http://codex.wordpress.org/Template_Tags/wp_list_pages
    This script will display the list on the parent page, and on the child pages, which is what I want.

    I modified the code to display a title for the list like this:

    <?php
    if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
    else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>
    <ul>
    
    <li id="parent-page">About</li>
    
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    My first, minor, question: is there is a dynamic way to do this?

    Now for why I'm writing this post: I would like to add in a custom field from each child page to display under their respective outputs in the list that is created. Here is where I am after using the above modified code:
    http://www.backlitcreative.com/images/start.png
    Here is where I would like to be:
    http://www.backlitcreative.com/images/end.png
    I'm not sure how to add in a call to grab the custom field (the gray text in the end.png image) for each of these pages and have it display in this order. Any suggestions?

  2. MichaelH
    Volunteer
    Posted 2 years ago #

    You'll probably be better off using Function_Reference/get_pages and Function_Reference/get_post_meta to do that.

  3. bbellx
    Member
    Posted 2 years ago #

    I've looked at those references before, but I'm not sure how to combine them. Any chance someone could explain how to combine the two?

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    Did you try the Displaying Child pages of the current page in post format example in the get_pages article? Just add echo 'A book by:'. get_post_meta($page->ID,'whateveryourkeyis'); to that foreach loop and you will likely want to display of the content.

  5. bbellx
    Member
    Posted 2 years ago #

    The solution thanks to the guiding hand of MichaelH:

    <div id="sidebar-left">
    <ul>
    <li id="parent-page">About</li>
    <?php
    if($post->post_parent)
    $pages = get_pages('title_li=&child_of='.$post->post_parent.'&sort_column=post_date&sort_order=desc&echo=0');
    else
    $pages = get_pages('title_li=&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&echo=0');
    $count = 0;
    foreach($pages as $page)
    {
    $content = $page->post_content;
    if(!$content)
    continue;
    if($count >= 2)
    break;
    $count++;
    $content = apply_filters('the_content', $content);
    ?>
    <li id="pages"><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></li>
    <li class="sub-list"><?php echo get_post_meta($page->ID,'listdesc',true); ?></li>
    <?php
    }
    ?>
    </ul>
    </div>

    This will display the child pages and the custom field on both the parent page and all child pages. Nice! Much appreciated! You guys are great.

  6. shanegriff
    Member
    Posted 2 years ago #

    Hi all, Wandered if anyone can help.

    I am trying to do a similar thing as above, but cant get my head around how to do it.

    I have a few pages in my site and im using the wp_list_pages function to list them all.

    What i would like to do next is have one or more labels underneath that are driven by custom fields in that page. see here for example:
    http://twitpic.com/86yd2

    If possible it would also be interesting if the labels would be become anchor links to varoius parts of the page.

    Any ideas would be great....i just dont get the cold.

    Many thanks

  7. kevingoldman
    Member
    Posted 2 years ago #

    Hi bbellx & MichaelH,

    Great solution; Thanks! Its just what I needed.

    One note however, have you noticed that 'the_content();' does not work when placed after this solution? It works if placed before it, but when placed after it, it returns the text "Object" without any content.

    If you close "the loop" and re-open a second loop that the_content() lives in it will work.

  8. Vayu
    Member
    Posted 2 years ago #

    Hi Shanegriff.

    Dont know if I have understood you quite right, but this might be what your looking for.

    echo "<ul>";
    echo "<li><a href="/photos" title="Photos">Photos</a></li>";
    wp_list_pages("title_li=&child_of=".$post->ID."&meta_value=Photos");
    
    echo "<li><a href="/video" title="Video">Video</a></li>";
    wp_list_pages("title_li=&child_of=".$post->ID."&meta_value=Video");
    echo "</ul>";

    Cordially
    Vayu

  9. MohammedAlaa
    Member
    Posted 2 years ago #

    Hello all,

    I'm having a similar problem.

    I want to load the list of pages and custom field value defined in this page the final code should look like this:

    <ul>
    <li><a href="" title=""><strong>PAGE NAME</strong> CUSTOM FIELD CALLED UNIVERSITY</a></li>
    </ul>

    any clue how to get this result? any help would be highly appreciated :)

  10. canofpopcom
    Member
    Posted 2 years ago #

    I'm also trying to do a similar thing replacing my text links with custom images.. ummz

  11. mrauc941
    Member
    Posted 2 years ago #

    @bebelix (and MichaelH) Wow! Thanks a million! You just helped me solve the problem I've been working on for about 3 hours. Much appreciated.

  12. ebben
    Member
    Posted 2 years ago #

    I've added the fields "listetittel" and "listetekst" to ever page in my WordPress installation, and have tried several modified versions of the scripts above; but nothing works.

    This is what I do:

    <ul>
    <?php
    $pages = get_pages('title_li=Verda&child_of=33&echo=0');
    $count = 0;
    foreach($pages as $page)
    {
    $content = $page->post_content;
    if(!$content)
    continue;
    if($count >= 2)
    break;
    $count++;
    $content = apply_filters('the_content', $content);
    ?>
    <li id="pages"><a>ID) ?>"><?php echo $page->post_title ?></a>
    <li class="sub-list"><?php echo get_post_meta($page->ID,'listetittel',true); ?>
    <?php
    }
    ?>
    </ul>

    What's wrong? Help! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic