• bramdesign

    (@bramdesign)


    <?php if(is_page(6) || is_page(12) || is_page(13) || is_page(14) || is_page(15) || is_page(16) || is_page(17) || is_page(18) || is_page(19) || is_page(20) || is_page(21) || is_page(22) || is_page(28) || is_page(29)) {?>
    								 <?php wp_list_pages('child_of=28&sort_column=menu_order&depth=2&title_li='); ?>
    							<?php }?>

    I’m creating very a specific list of links on my subnav. All the pages below are children of page 28. Is there a more efficient way to write this code then list page after page?

Viewing 10 replies - 1 through 10 (of 10 total)
  • i have a similar inquiry… a menu will appear in subpages. is there a way to make the code above shorter?

    i would like to add in connection with the code above, could it be written in ->
    <?php if( is_page(6) || is_page(XXX){?>

    wherein XXX are subpages of page(6). what would be the value/code of XXX?

    Thank You.

    PHP’s in_array() function might be what you are looking for. You could use something like this:

    <?php
    // create array of page ID’s we want to check for
    $page_ids = array(6,12,13,14);

    // see if the current page ID is in the array
    if(in_array(the_ID(),$page_ids)){
    // the_ID() was in the array!
    // your code here
    }
    ?>

    Also, as at least one of you mentioned, if the $page_ids array is going to contain the children of a specific page ID, you may want to read about WordPress’ get_children() function. I haven’t used that function, and there isn’t much documentation on it, but it may enable you to automatically populate the $page_ids array from the WordPress database.

    ^it didn;t work T_T

    Is your code executing inside The Loop? If not, I think I know why it didn’t work.

    ‘the_ID()’ function only works if you are inside The Loop. If you are outside The Loop, you’ll have to get the post ID another way.

    Try this:

    <?php
    // get the current page ID
    $the_page_id = $post->ID;

    // create array of page ID’s we want to check for
    $page_ids = array(6,12,13,14);

    // see if the current page ID is in the array
    if(in_array($the_page_id,$page_ids)){
    echo ‘hooray! this is a special id!’;
    }
    ?>

    I tested this code on my site and it works.

    ^ it works. thank you very much. appreciate it ??
    one more question. how can i write a range value?
    for example:

    $page_ids = array(115,123,124,125,126,127,128)

    can i write it as

    $page_ids = array(115,123:128)

    but it didnt work? thanks.

    For ranges, use PHP’s built in range() function:

    $page_ids = range(123,128);

    For a mix of individual ID’s and ranges of ID’s, you just need to figure out the best way to build the $page_ids array (see these PHP functions: array_merge, array_push). For example:

    $page_ids = array_merge(range(123,128),array(115,119,309));

    Or:

    $page_ids = range(123,128);
    array_push($page_ids,115,119,309);

    THANK YOU! that helps! ??

    KS

    (@karl19)

    Great, I’ve been looking around for something similar to the in_categories() call, but for pages. I have a site where I want to call something on a few main pages and their sub-pages. Using the code above, I now just have to define the parent IDs and all sub-pages get included too – fantastic! This really helps when using WP as a CMS and having lots of pages rather than posts.

    Thank you.

    Maxaud

    (@maxaud)

    This is a code that I use to show subpages when on a parent page AND while on a child page. It could easily be changed to echo something else if the page is a child.

    <?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; ?>
    		<?php } ?>
    	</ul>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘IS there a more efficient way?’ is closed to new replies.