I have a function below whose purpose is to create a listing of posts where the category assigned to each post is “top-menu”. However, I first want to exclude any posts where the category is “hidden”…
How can I edit this code to do that?
global $post;
$cat = get_cat_ID('top-menu');
if ($cat > 1)
$myposts = get_posts('numberposts=5&category='.$cat);
else
$myposts = get_posts('numberposts=10');
foreach ($myposts as $post) :
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endforeach; ?>
This should do what you want. Not sure why you had the
ifstatement, so feel free to incorporate again if you need it. You can always just usecategoryand give it an id instead ofcategory_nameas I am using here as a shortcut.EDIT:
category_namecannot be combined withcategory__not_in, so I changed the code slightly so both filters would be run.I have tested this and it works in WordPress 2.8, just be sure to supply the actual category name in the
get_cat_IDcalls and not theslugby accident.