• Resolved henriemedia

    (@henriemedia)


    I currently use some search criteria with wp_list_pages to return a menu for my users. This works great until wp_list_pages doesn’t return anything. So I am trying to determine if wp_list_pages is blank so that I can tell the customer to “try again” but I can’t seem to get it to work. This is what I have but it does’t work –

    $children = wp_list_pages(‘title_li=&child_of=1700&meta_key=location&meta_value=vegas&sort_column=post_title’);

    if ($children) {
    echo ‘

      ‘.$children.’

    ‘;
    } else {
    echo ‘

    • No results match your criteria.
    • Please try again or use our SEARCH.

    ‘;
    }

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi, can you edit your post to put the code inside code tags, it is much more readable. ??

    Try a more specific test such as:

    if ($children != null) {

    or:

    if ($children != '') {

    If this doesn’t work then echo out the value of $children when it returns something and when it doesn’t. Echo out the return inside some characters so spaces can be clearly seen:

    echo 'xyz'.$children.'xyz';

    Is the value of $children consistent for any scenario when you think it returns nothing? This is important so you can do a consistent test for it. Try the above and let me know how you get on..

    Thread Starter henriemedia

    (@henriemedia)

    I tried all of this and nothing seems to be working. Specifically I tried to echo 'xyz'.$children.'xyz'; and it didn’t change anything. This leads me to believe that it is printing earlier in the script, except I cannot figure out where. Here is the code (sorry for before) –

    <?php
    		  $exclude_these_pages = get_custom_field('excludepages');
    		  $view_location = get_custom_field('location');
    		  $view_meta_key = "location";
    		  $list_by_type_page_id = $_GET["type"];
    		  $list_by_location = $_GET["loc"];
    		  $list_current_post_id = $post->ID;
    		  if ($list_by_type_page_id != "" && $list_by_location != "") { // list by LOCATION and TYPE
    		  	echo '<div class="title">Updated Results</div>';
    			$children = wp_list_pages('title_li=&child_of='.$list_by_type_page_id.'&meta_key='.$view_meta_key.'&meta_value='.$list_by_location.'&sort_column=post_title');
    		  } elseif ($list_by_type_page_id != "" && $list_by_location == "") { // list by TYPE
    		  	echo '<div class="title">Updated Results</div>';
    			$children = wp_list_pages('title_li=&child_of='.$list_by_type_page_id.'&sort_column=post_title');
    		  } elseif ($list_by_type_page_id == "" && $list_by_location != "") { // list by LOCATION - Requires TYPE as well
    		  	echo '<div class="title" style="color:#ff0000; font-size:16px;">Please select a TYPE to continue.</div>';
    		  } else { // list COMPLETE
    		  	echo '<div class="title">Results</div>';
    		  	$children = wp_list_pages('title_li=&child_of='.$list_current_post_id.'&exclude='.$exclude_these_pages.'&sort_column=menu_order');
    		  }
    
    		  if ($children != '') {
    		  	echo '<ul>123'.$children.'321</ul>';
    		  } else {
    		  	echo '<ul><li>No results match your criteria.</li><li>Please try again or use our SEARCH.</ul>';
    		  }
    		 ?>

    Echo out the code:

    echo '<ul>123'.$children.'321</ul>';

    before the if statement so that it will ALWAYS print, so you can see it’s value for when it returns something, and ’empty’ returns.

    Thread Starter henriemedia

    (@henriemedia)

    If I understand you correctly, I moved the code echo '<ul>123'.$children.'321</ul>'; to right after the $list_current_post_id = $post->ID; and before if ($list_by_type_page_id != "" && $list_by_location != "") { // list by LOCATION and TYPE.

    The result is “123321”. This tells me it is blank, but when I look for ” or null nothing happens. I think this is because the $children is printing during the IF statements instead of waiting until I echo it. This crazy! Any ideas? Thanks for your responses!

    No, if you moved it there then it won’t show any value as it has not been assigned yet.

    Change this block of code:

    if ($children != '') {
                  echo '<ul>123'.$children.'321</ul>';
              } else {
                  echo '<ul><li>No results match your criteria.</li><li>Please try again or use our SEARCH.</ul>';
              }
             ?>

    to be:

    echo '<ul>123'.$children.'321</ul>';
    if ($children != '') {
      echo 'Got something!';
    } else {
      echo '<ul><li>No results match your criteria.</li><li>Please try again or use our SEARCH.</ul>';
    }
    ?>
    Thread Starter henriemedia

    (@henriemedia)

    Gotcha. I did this and my results were –

    123321
    * No results match your criteria.
    * Please try again or use our SEARCH.

    Ideas?

    Well if that is the case then it seems to be working! $children is obviously null so you get the try again message. Put back the echo statement in place of the ‘got something’ line.

    Thread Starter henriemedia

    (@henriemedia)

    I put the code to `if ($children != ”) {
    echo ‘<ul>123′.$children.’321</ul>’;
    } else {
    echo ‘<ul><li>No results match your criteria.</li><li>Please try again or use our SEARCH.</ul>’;
    }` and it is not printing the 123′.$children.’321 portion of the code. You can see that it is still printing the pages before it gets to the code – https://www.carefreelasvegas.com/entertainment/

    I don’t understand why it is printing the pages in the IF statement when I don’t tell it to print it until the end???

    Oooooops! Sorry, just remembered, you will need to turn the echo parameter off otherwise it will output the wp_list_pages the first time it encounters it. You want it to ‘return’ the list and print it later.

    Change all your wp_list_pages functions to include ‘echo=0’ (that’s a zero). See the echo parameter here for more details:

    https://codex.www.ads-software.com/Function_Reference/wp_list_pages

    Thread Starter henriemedia

    (@henriemedia)

    YOU NAILED IT!!! That was it! Thank you so, so much!!! It works perfectly now!

    You are most welcome.. ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to tell if wp_list_pages returns no pages’ is closed to new replies.