WordPress.org

Ready to get started?Download WordPress

Forums

customising wp_list_pages (15 posts)

  1. dtsn
    Member
    Posted 7 years ago #

    Hi,

    I'm really new to wordpress and php (only installed it yesturday). However i have been trying to create a theme for my site, it's all easy enough except i want to use the 'sliding doors' principle for my navigation, for this to work i need to enter a <span> in between my list items e.g.

  2. <span></span>
  3. . However wp_list_pages doesn't allow this. Could anyone help me performing a SQL Query on the list of pages and passing them out in a list.

    You can look at my awful attempt at http://www.dtsn.co.uk

    Thanks

    Daniel

  • Root
    Member
    Posted 7 years ago #

    Forgive me for saying so but IMHO this is an exceptionally interesting question and one which is of wide application. It is also similar to one I asked a while ago and received no answer to. Without hijacking the thread but to put more meat on it I would love to know where the widgets are defined, including get pages, and where the functions in the widgets are set up. Where I am really headed is that I would like functions (a) to return plain unadorned links and (b) to have the option of adding my markup like the OP. In my case I would like to add an IE conditional at the critical point. I hope someone can point us in the right direction.

  • MichaelH
    Member
    Posted 7 years ago #

    @dstn
    Try using wp_list_pages to get your list of pages. This example will assign all page titles to the $pages array.

    $pages = wp_list_pages('depth=-1&echo=1&hierarchical=0&sort_column=post_title&title_li=');
    `

    @Root
    The widget code is in wp-includes/widgets.php and does make use of the template tag, wp_list_pages()

    If fact, dstn could use the code from widgets.php as an example also.

  • dtsn
    Member
    Posted 7 years ago #

    Hi MichealH,

    Thanks for replying, how do i get the link to the pages. I know that i could just use <?php echo $pages ?> to give me the list of pages, but how do i get the link to those pages.

    Thanks

    Daniel

  • MichaelH
    Member
    Posted 7 years ago #

    <?php
    $pages = wp_list_pages('depth=-1&echo=1&hierarchical=0&sort_column=post_title&title_li=');
    if ( ! empty($pages) ) {
    foreach ($pages as $page) {
    echo $page;
    }
    }
    ?>

    is one possibility.

  • dtsn
    Member
    Posted 7 years ago #

    Sorry i may be a bit stupid, but i just don't see how this would work. This would just produce the same thing as wp_list_pages, how do i get the links and page titles from it?

    e.g. i need to have <li><a href="link"><span>page name</span></a></li>

    Could i use echo $page['title']?

    Thanks

    Daniel

  • MichaelH
    Member
    Posted 7 years ago #

    There's this also:

    <?php
    $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'page' ORDER BY $wpdb->posts.post_title ASC";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    ?>
    <?php if ($pageposts): ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></p>
    <?php endforeach; ?>
    <?php else : ?>
    Not Found
    <?php endif; ?>
  • dtsn
    Member
    Posted 7 years ago #

    Thanks great thanks! :D

    You wouldn't happen to know how i could tell which page i am on so i could change the class accordingly?

    Thanks

    Daniel

  • MichaelH
    Member
    Posted 7 years ago #

    Try using a Conditional Tag

  • Root
    Member
    Posted 7 years ago #

    Mh. Thanks a lot this is really useful. But are you sure they are still in widgets.php? I can't find them. I thought maybe widgets.php called in the actual widgets from another file. Maybe?

  • MichaelH
    Member
    Posted 7 years ago #

    Yes, wp-includes/widgets.php is where the built-in widgets are defined. Just look for the wp_list_pages text in that file for example.

    Also, there is some widget 'data' kept in wp_options with an option_name like widget_pages.

  • Root
    Member
    Posted 7 years ago #

    That is great thanks. I am going to get right on this.

  • analogpanda
    Member
    Posted 7 years ago #

    I wanted to do this too, and couldn't find any information on it.

    In the end, I had to edit the start_el() function in wp-includes/classes.php

    Change the following (from about line 517):
    $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('the_title', $page->post_title)) . '">' . apply_filters('the_title', $page->post_title) . '</a>';

    To this:
    $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_page_link($page->ID) . '" title="' . attribute_escape(apply_filters('the_title', $page->post_title)) . '"><span>' . apply_filters('the_title', $page->post_title) . '</span></a>';

    Of course, this will always add span tags any time wp_list_pages is called. And you'll have to remember that you've done this hack the next time you upgrade...

  • Root
    Member
    Posted 7 years ago #

    Eureka. The Holy Grail. A big up to analogpanda for his discovery of where that dang thing is edited. Now we are rocking. Is this the end for Son of Suckerfish? :)

  • analogpanda
    Member
    Posted 7 years ago #

    Just for completeness, my above hack was done with WP 2.2.1

    Let's hope that 2.3 (or even 2.2.2?) has a little more flexibility in this regard.

    I'm actually a little surprised they wouldn't just use "the loop" - would probably end up with a more elegant solution than my hacky method... But I'm not a real php dev - maybe they have their reasons!

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags