Support » Themes and Templates » Exclude Pages from Nav Bar Issue

  • So I’m having an issue excluding some pages from my nav bar. Unfortunately my theme has <?php dp_list_pages();?> instead of the normal <?php wp_list_pages();?> .

    I’ve been told that dp_list_pages doesn’t allow any exclusions as the wp_list_pages normally would.

    What would I need to do in order to exclude a few pages from my nav? Here’s the function code for the dp_list_pages.

    # Displays a list of pages
    function dp_list_pages() {
    	global $wpdb;
    	$querystr = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'page' ORDER BY $wpdb->posts.post_title ASC";
    	$pageposts = $wpdb->get_results($querystr, OBJECT);
    	if ($pageposts) {
    		foreach ($pageposts as $post) {
    			?><li><a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a></li><?php
    		}
    	}
    }

    Thanks for any help. This is getting to be frustrating little problem.

Viewing 10 replies - 1 through 10 (of 10 total)
  • I don’t see any advantage in using <?php dp_list_pages();?> instead of <?php wp_list_pages();?> so you can use the normal wp_list_pages().

    I’m not quite following the function. Why wouldn’t you just replace it with <?php wp_list_pages();?>?

    Tried that. It destroys the entire template.

    Could you add WHERE $wpdb->posts.ID != '17' to exclude a post? (Post with ID #17 in this case)

    If you don’t want to hard-code page IDs in the query string, you might be able to do something like this.

    First modify the dp_list_pages in the foreach loop to check for a Custom field called ‘hide-nav’ like this (UNTESTED!!):

    if ($pageposts) {
       foreach ($pageposts as $post) {
          $meta_value = get_post_meta($post_id, 'hide-nav', TRUE);
          if ($meta_value != 'true') {
             ?><li><a href="<?php echo get_permalink($post->ID); ?>"><?php  echo $post->post_title; ?></a></li><?php
          }
       }
    }

    Second, add the Custom field ‘hide-nav’ to the appropriate posts and give it a value of ‘true’.

    This will allow you to add or remove pages from the nav without having to modify the code.

    vtxyzzy, i tried your method and every time I save the new function.php page I get sitewide error.

    Parse error: syntax error, unexpected $end in wp-content/themes/meganews/functions.php on line 139

    The funny thing is that when have the entire functions.php open in an editor..there is no line 139. It only goes to 137. Any ideas.

    This can be caused by a missing curly bracket, or the use of ‘Short Tags’, eg <? instead of <?php.

    Make sure all your curly brackets match up and replace all short tags.

    If that doesn’t solve the problem, post your full functions.php (assuming it is fairly short) and I will have a look.

    I just figured out how to do it. This will allow you to exclude them, while ordering them however you want if that is your preference.

    Open up the “functions.php” file for the theme and replace the portion under
    “# Displays a list of pages”. It is located in your theme (/wp-content/themes/{name of your theme’s folder}/functions.php

    Replace:

    $querystr = “SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘page’ ORDER BY $wpdb->posts.post_title ASC”;

    With:

    $querystr = “SELECT $wpdb->posts.ID, $wpdb->posts.post_title FROM $wpdb->posts WHERE $wpdb->posts.post_status = ‘publish’ AND $wpdb->posts.post_type = ‘page’ ORDER BY $wpdb->posts.menu_order ASC”;

    It took me three days to find this, so I figure I’d share. WordPress does not have a great support system, so if you can pass this on, please do. Please copy and paste the link to this post to other inquiries.

    On second thought, if you want to exclude pages from the Nav Bar, you can really just use wp_list_pages ( ); and just put the ids of the pages you want included and voila. It will not kill, your nav. It’s native to wp, so it should work. If you don’t want the details, just put ‘title_li=’ inside the parentheses and it should work. I just did it using a theme that had the same code as yours for the display of pages (dp_list_pages).

    Great riffard,

    I just ran into the same problem with dp_list_pages and your solution works 🙂

    Thanks.
    O

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Exclude Pages from Nav Bar Issue’ is closed to new replies.