Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Excluding a few pages from a query to get all pages for links WordPress

Okay, in the first draft of the site (the prototype), I had a nav bar that looked like this:


<ul id="nav">
<li><a href="#" id="link_for_list_of_collections">COLLECTIONS</a>
<ul id="list_of_collections">
<?php
query_posts('category_name=collections');
if (have_posts()) :
while (have_posts()) : the_post();
echo "<li><a href=\"/?p=";
the_ID();
echo "\">";
the_title();
echo "</a></li>\n\n";
endwhile;
endif;
wp_reset_query();
?>
</ul>
</li>
<li><a href="/?page_id=15">SPECIAL PROJECTS</a></li>
<li><a href="/?page_id=26">ABOUT</a></li>
<li><a href="/?page_id=18">STOCKLISTS</a></li>
<li><a href="/?page_id=20">SHOP</a></li>
<li><a href="/?page_id=22">PRESS</a></li>
<li><a href="/?page_id=7">BLOG</a></li>
<li><a href="/?page_id=24">LINKS</a></li>
</ul> <!--end of Nav-->


"collections" is a category of blog posts.

But now "special_projects" is also going to be a category of blog posts, and "press" is going to be a category of blog posts.

So I need to do 2 more loops like that first one. Easy enough, yes?

However, the listing of the pages themselves now need to become dynamic. I was cheating at first by hardcoding them.

Assuming I hard-code the links that need to have loops (of category specific blog posts) underneath them, how could I do a loop for all the pages except for the pages that I've already posted? Can I get the pages while excluding certain pages from the query?

A really elegant solution would be to match page names to the name of the category used for the related blog posts, but I suspect that is more complicated than anything I currently want to attempt.

Answers (3)

2011-03-24

Maor Barazany answers:

Well, I'm not sure I really understood what you really need/want to do, but if you want to exclude pages/posts from the loop, you can just create an array of the ids you want to exclude, and then pass the array as a parameter to the query .

Suppose you have this array -

$excluded = array(15,26,18,20,22);

Then you could query posts as this :


$args = array('category_name' => 'collections', 'post__not_in' => $excluded);

query_posts($args);


Notice that I also changed the query paramters to be instead of GET style (param1=val&param2=val2 to be an associative array of the parameters)


You can find all paramters of [[LINK href="http://codex.wordpress.org/Function_Reference/WP_Query#Parameters"]]query posts here[[/LINK]]


Maor Barazany comments:

Also, if you want to use page by slugs and not by IDs, you can use this little function to pass a page slug and return the page ID, then you could add these IDs to the array of IDs needed to be excluded.


function get_ID_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
return $page->ID;
} else {
return null;
}
}

2011-03-24

AdamGold answers:

Try something like this:

$hardcore = array('collections', 'special_projects', 'press');

foreach( $hardcore as $name ) {
?>

<li><a href="#" id="link_for_list_of_collections"><?php echo $name; ?></a>

<ul id="list_of_collections">

<?php

query_posts('category_name=' . $name);

if (have_posts()) :

while (have_posts()) : the_post();

echo "<li><a href=\"/?p=";

the_ID();

echo "\">";

the_title();

echo "</a></li>\n\n";

endwhile;

endif;

wp_reset_query();

?>

</ul>

</li>
<?php
}


It will loop through all of your defined pages ($hardcore) and get the posts for each one of them.

2011-03-24

Ivaylo Draganov answers:

Hello,

I've read carefully through your question and I assume that you want to build a navigation with certain top-level items listing posts by category as a submenu and other top level items listing pages. Excluding and including pages is very easy with <em>wp_list_pages()</em> but for the posts I had to build a loop. OK, here's what I've got (tested and works):
[[LINK href="http://pastebin.com/N1qDMxCh"]]http://pastebin.com/N1qDMxCh[[/LINK]]

You could achieve a similar result by using the built-in navigation menus but then you'd have to manually add each post. Well, hope that this solution works for you.

Cheers!