Forums

Set attribute to top level items using wp_list_pages (13 posts)

  1. daniello
    Member
    Posted 2 years ago #

    Hello.
    I've been trying to implement an opencube menu in wordpress 2.8 for a while now. I'm using the following code to get the desired output from wp_list_pages function:

    $my_pages = wp_list_pages('title_li=&echo=0');
    $my_pages = str_replace('<li class="page_item', '
    <li><span class="qmdivider qmdividery"></span></li>
    <li class="page_item', $my_pages);
    $my_pages = str_replace('<ul', '<ul class="wordp_listsubs_sec"', $my_pages);
    echo $my_pages;

    The problem is that I only want to assign the string
    <li><span class="qmdivider qmdividery"></span></li>
    to all top level pages (meaning the links that is displayed as main menu items when you visit http://blogger.steinberg.se/).
    The li string above works as a divider between the main menu items. But when this string is assigned to the submenus as well it fucks upp the spacing between the submenu items in older browsers ;-(

    Please help me with this one, I'm pulling my hair right now... I'm originally an ASP developer som I'm struggling a alittle bit with the PHP ;-)
    Thanks in advance! /Daniel

  2. daniello
    Member
    Posted 2 years ago #

    Hi again.
    One solution would be to assign an extra class to all the top level parents (as shown inside <strong> in the example output below):

    <li class="<strong>mainitem</strong> page-item page-item-30">
      <a href="javascript:void(0);" title="Pages">Pages</a>
      <ul>
        <li class="page-item page-item-40"><a href="link-to-submenu-item1" title="link-title1">Link-desription1</a></li>
        <li class="page-item page-item-41"><a href="link-to-submenu-item2" title="link-title2">Link-desription2</a></li>
        <li class="page-item page-item-42"><a href="link-to-submenu-item3" title="link-title3">Link-desription3</a></li>
      </ul>
    </li>
    <li class="<strong>mainitem</strong> page-item page-item-50">
      <a href="link-to-contact" title="title-contact">Contact</a>
    </li>
    <li class="<strong>mainitem</strong> page-item page-item-60">
      <a href="javascript:void(0);" title="Others">Others</a>
      <ul>
        <li class="page-item page-item-70"><a href="link-to-submenu-item1" title="link-title1">Link-desription1</a></li>
        <li class="page-item page-item-71"><a href="link-to-submenu-item2" title="link-title2">Link-desription2</a></li>
        <li class="page-item page-item-72"><a href="link-to-submenu-item3" title="link-title3">Link-desription3</a></li>
      </ul>
    </li>

    After that I could do a str_replace. But how do I assign that class only to the top level main menu items???
    If you take a look at the menu on http://blogger.steinberg.se/ I would like this mainitem class assigned to "HEM", "KATEGORIER", "LANKANNONSER", "OM DENNA BLOGG", "OM JOHN" and "KONTAKT"... /Daniel

  3. Technokinetics
    Member
    Posted 2 years ago #

    How about this:

    (1) Create a list of top-level page IDs by querying your wp_posts table based on post_parent (0), post_type (page), and post_status (publish).
    (2) For each ID, do a str_replace on the page-item-ID class.

    - Tim

  4. daniello
    Member
    Posted 2 years ago #

    Tim.
    Thank you for your response, but could you be a little bit more specific. I'm not that experienced of how to handle all the wordpress functions ;-(
    Could you give me an example? That would be really appreciated! Thank you... /Daniel

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    You'll need something a bit more specific to make sure you target only top level LI elements...

    I'm not a huge user of regex so i can't give you a good example, but i can tell you preg_replace would be better for your usage..

    http://uk3.php.net/preg_replace

  6. Technokinetics
    Member
    Posted 2 years ago #

    Hi Daniel,

    Not tested, but:

    (1) "SELECT ID FROM wp_posts WHERE post_parent = '0' AND post_type = 'page' AND post_status = 'publish'" should return an array containing the IDs of your top-level Pages.
    (2) In Page lists, every list item has a unique class "page-item-x", where x is the Page ID. With a list of all your top-level Page IDs you should therefore be able to target your str_replace to just your top-level Pages.

    Using the code in the OP as a starting point, you'll end up with something like this:

    $my_pages = wp_list_pages('title_li=&echo=0');
    $top_level_pages = mysql_query("SELECT ID FROM wp_posts WHERE post_parent = '0' AND post_type = 'page' AND post_status = 'publish'");
    while ($row = mysql_fetch_assoc($top_level_pages)) {
    	extract($row);
    	$my_pages = str_replace('<li class="page_item page-item-' . $ID, '
    <li><span class="qmdivider qmdividery"></span></li>
    <li class="page_item page-item-' . $ID, $my_pages);
    }
    $my_pages = str_replace('<ul', '<ul class="wordp_listsubs_sec"', $my_pages);
    echo $my_pages;

    - Tim

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

    Top notch idea techno...

    Might want to take a look at using get_col() for selecting the ID's.

    http://codex.wordpress.org/Function_Reference/wpdb_Class#SELECT_a_Column

  8. daniello
    Member
    Posted 2 years ago #

    Thanks guys!
    Technokinetics: it sort of works ;-) But for some strange reason it puts <li><span class="qmdivider qmdividery"></span></li> before "OM DENNA BLOGG", but not before "LANKANNONSER" AND "OM JOHN"???? Strange!? Your code is doing what I wanted but not between all the toplevel items????

    Any idea? Thanks! /Daniel

  9. Technokinetics
    Member
    Posted 2 years ago #

    Hi Daniello,

    It looks to me like the code above does what I was aiming for, i.e. it limits your str_replace to just top-level Pages (although I see now that class="page_item page-item-'.$ID might be better as class="page_item page-item-'.$ID.'"').

    However, I see now that your menu isn't just a Page list, and that the items that you want to modify aren't page items, so what you're asking for can't be done by modifying your wp_list_pages output.

    If you post the code that generates your menu, perhaps I or someone else will take another shot at this.

    - Tim

  10. daniello
    Member
    Posted 2 years ago #

    Ok Tim.
    Thanks again for your fast replies!!! But I think you understand what I want to acchieve, right???? But all menuitems from "LANKANNONSER" to "OM JOHN" are pages. The submenu items are connected to "Parent" below "Attributes" in the WP admin panel (administrating "Pages") ... So they all have to be pages..?!
    If you want Tim I can set you up for a temporary admin account so that you can see the listing for yourself in the WP admin interface!

    Here's the code:

    php code:

    <ul id="qm0" class="qmmc" style="">
    <li><a href="http://blogger.steinberg.se/" title="Hem">HEM</a></li>
    <li><span class="qmdivider qmdividery"></span></li>
    <li><a class="qmparent" href="javascript:void(0);" title="Kategorier">KATEGORIER</a>
    <ul class="wordp_listsubs">
    wp_list_cats(class=&category_order=ASC);
    </ul>
    </li>
    your code to list pages went here...
    <li><span class="qmdivider qmdividery"></span></li>
    <li><a href="http://www.steinberg.se/contact/" title="Kontakt">KONTAKT</a></li>
    <li class="qmclear">&nbsp;</li>
    </ul>

    (I had to erase your code from the post since it was causing problems when I posted... But yor code above should be placed where the text "your code to list pages went here..." is placed above...)

    js code can be downloaded from here:
    http://www.steinberg.se/inc/css_menu.js

    css code can be downloaded from here:
    http://www.steinberg.se/css/css_menu.css

  11. Technokinetics
    Member
    Posted 2 years ago #

    Hi Daniello,

    A temporary admin account so that I can see what's going would help.
    Email address is tim @ technokinetics.com.

    - Tim

  12. daniello
    Member
    Posted 2 years ago #

    Ok, Tim.
    I sent an email to you with the temp login! Please respond so that I know if you got the email ;-) /Daniel

  13. Technokinetics
    Member
    Posted 2 years ago #

    Now fixed. The code above is fine (with the closing double quote added), as long as the default "wp_" table prefix is used. In this case, the table prefix had been changed so it needed a global $wpdb; adding before the code, and the table name changing to $wpdb->prefix . "posts".

    - Tim

Topic Closed

This topic has been closed to new replies.

About this Topic