• Hello everyone,

    just got back into coding but it’s more like riding a unicycle than a bicycle.

    I’ve run into a bit of trouble with this piece of code:

    code`
    $pages = get_pages();
    foreach ($pages as $page){
    $navigation = ‘<li><a class=”‘ . $page->post_title . ‘ ‘ . if(wp_title(‘ ‘, false)){echo ‘current-page-item’} . ‘” href=”‘ . get_page_link($page->ID) . ‘”>’ . $page->post_title .'</a></li>’ . “\n”;
    echo $navigation;}`

    I’ve been trying to create custom classes in the wp_list_pages that relate to the page names but I can’t figure / find a post about it.

    Is there an easy way to do this with wp_list_pages?

    Thanx for looking,
    Sam

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Well, there is a ‘wp_list_pages’ filter, but you get all the HTML output for the entire list in one go, so it could be difficult to do what you want using the filter approach. You really are better off with get_pages() as you are attempting.

    Granted the forum parser may have scrambled some of your syntax, but your snippet is a bit of a mess, if I may be blunt. To be fair, most if it may be workable, but it’s certainly not readable. The one real problem is how you deal with the possibility of no title. Echoing a string at that point will send it straight to the browser immediately, not to inside the string you are trying to build.

    There are some nifty ways to deal with this, such as the ternary operator, but for readability, perhaps simply preassign the title to a variable in a separate if(){} structure and use that variable in building the output string.

    Another big help with readability is taking advantage of the fact PHP will automatically expand variables inside double quoted strings, and that HTML works equally well with single quoted parameters. This helps immensely with not getting lost in quotes and dots.

    I may seem really stuck on readability, but it really helps prevent hard to find errors and ensures your syntax is in order, especially when you put a project aside for a while then return with no recollection of where you were going with it.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom wp_list_pages’ is closed to new replies.