• Resolved wilfswann

    (@wilfswann)


    This should be fairly simple but I’ve searched for hours in vein.

    I’m trying to do something similar to this https://nettuts.s3.amazonaws.com/196_jquery/index.htm so that I can hide or display groups of pages in a list. To do that I need to assign classes to each of my pages, I’ve tried using the Classy wp_list_pages plugin but that assigns every page with a different class, I need to be able to assign the same class to a group of the links. I also need to be able to assign multiple classes to each list item.

    I could hard code it but then I would need to manually change either the CSS or the markup every time I added a new page. Anyone have any ideas to fix this conundrum?

Viewing 15 replies - 16 through 30 (of 35 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Ok that is strange. On my server it is working. Let’s do some testing.
    in stead of the code I gave you paste this code:

    <?php
    	$my_pages = get_pages('meta_key=class&meta_value=print');
    	echo '<pre>';
            print_r($my_pages);
            echo '</pre>';
    ?>

    If you don’t see a big array with your pages, get_pages is not working or you don’t have a page with a custom field with key = class and value = print.
    If you don’t see a big array paste this code:

    <?php
    	$my_pages = get_pages();
    	echo '<pre>';
            print_r($my_pages);
            echo '</pre>';
    ?>

    If you don’t see a big array with all your pages get_pages is not working.

    Moderator keesiemeijer

    (@keesiemeijer)

    If you go to a page (with the custom field) in your dashboard does it look like this.

    Thread Starter wilfswann

    (@wilfswann)

    thank you again for your help kees.

    I got an array from that second code but none from the first one so I must have done the custom field wrong. Here’s a screenshot of the custom field, where am I going wrong?

    https://img684.imageshack.us/img684/964/screenshot20100617at230n.png

    Thread Starter wilfswann

    (@wilfswann)

    aha I didn’t see your latest post before I posted but they look the same to me?

    Moderator keesiemeijer

    (@keesiemeijer)

    ok get_pages is working. They look the same, yes. Now try this:

    <?php
    	$my_pages = get_pages('meta_key=class');
    	echo '<pre>';
            print_r($my_pages);
            echo '</pre>';
    ?>

    Thread Starter wilfswann

    (@wilfswann)

    still no results, just says

    Array
    (
    )

    Moderator keesiemeijer

    (@keesiemeijer)

    did you paste “class” and “print” in the custom field. sometimes you paste in a space (class[space] or [space]print) and it doesn’t work. We keep on testing.
    make a new custum field. name: myclass, value: print. (dont paste or be sure of no spaces)
    try this one again:

    <?php
    	$my_pages = get_pages('meta_key=myclass&meta_value=print');
    	echo '<pre>';
            print_r($my_pages);
            echo '</pre>';
    ?>

    otherwise I don’t know what is causing this. (keeps on thinking)

    Thread Starter wilfswann

    (@wilfswann)

    hmmm still getting nothing, how annoying. does it matter if the pages are a child of another page? also does it matter if there are other custom fields?

    Moderator keesiemeijer

    (@keesiemeijer)

    We could test for that to. (it is probably the child page that stumps us) but first if you know the page ID of a page with the custom field (myclass)do this:

    <?php
            $key_1_value = get_post_meta(65, 'myclass', true);
            echo 'myclass value =' . $key_1_value ;
    ?>

    substitute 65 for your ID

    Thread Starter wilfswann

    (@wilfswann)

    myclass value =print

    is what i get

    Moderator keesiemeijer

    (@keesiemeijer)

    That’s good. I got it. It was the child category that did it.
    now try:

    <?php
    	$my_pages = get_pages('child_of=65&meta_key=myclass&meta_value=print');
    	echo '<pre>';
            print_r($my_pages);
            echo '</pre>';
    ?>

    substitute 65 for the parent ID where the child page with custom field (myclass) belongs to. And see if you get an array with all the pages with meta_key =myclass.

    Moderator keesiemeijer

    (@keesiemeijer)

    (posted twice) sorry

    Thread Starter wilfswann

    (@wilfswann)

    bingo that worked ??

    Moderator keesiemeijer

    (@keesiemeijer)

    Ok I will make the function for yor 3 classes now

    Moderator keesiemeijer

    (@keesiemeijer)

    Ok put this in your functions.php

    function get_my_pages($id, $class='', $value='') {
    	if($class !='' && $value != '' ) {
    		$parentID = ($id == '') ? '' : '&child_of='.$id;
    		$my_pages = get_pages('meta_key=' . $class . '&meta_value=' . $value . $parentID);
    		$html= '';
                    if(!empty($my_pages)){
    		$html .= '<ul>';
                      foreach ($my_pages as $pagg) {
      	    	  $html .=  '<li class="' . $pagg->meta_value . '"><a href="' . get_page_link($pagg->ID) . '">';
      	    	  $html .=  $pagg->post_title . '</a></li>';
                      }
                   $html .= '</ul>';
                   return $html;
                   }
            }
    }

    You can call this function in your template with:
    <?php echo get_my_pages(65, 'myclass', 'print'); ?>
    The two second values, “myclass” and “print” stand for the meta_key and the meta_value. you have to fill these in. The first value (65) stands for the parent page ID. You don’t have to fill this in, so you can use this function on parent pages as well. call the function like so: <?php echo get_my_pages('', 'myclass', 'print'); ?>
    The li gets its class from the meta_value (third value in the function)

Viewing 15 replies - 16 through 30 (of 35 total)
  • The topic ‘wp_list_pages Classes’ is closed to new replies.