0

I have my categories set to sort alphabetically:

<?php if (is_category()) { 
     $posts = query_posts($query_string . '&orderby=title&order=asc'); 
  } 
?>

I want to exclude one category so that it sorts by date. I tried this but it made all the cats come up blank:

<?php if (is_category()) { 
    $posts = query_posts($query_string . '&orderby=title&order=asc&exclude=15'); 
   }
?>

Any ideas?

4
  • 2
    query_posts should not be used. you should be using WP_Query or pre_get_posts. In which template are you using this code Commented Jun 10, 2014 at 11:23
  • ThemeMeme's Cassie
    – lkl
    Commented Jun 10, 2014 at 14:09
  • What I meant is is it index.php, category.php etc Commented Jun 10, 2014 at 14:35
  • Sorry, in archive.php
    – lkl
    Commented Jun 10, 2014 at 14:43

1 Answer 1

0

Add an else to the condition along with adding the excluded category id in the is_category() call. You will also need to include/exclude that particular category depending on the condition that is true:

NOTE: you probably want to avoid using query_posts and use WP_Query() Reference here

 <?php 
    if (is_category(15)) {
        $posts = query_posts($query_string . '&orderby=date&order=asc&cat=15'); 
    }

   else {
     if(is_category()) { 
        $posts = query_posts($query_string . '&orderby=title&order=asc&cat=-15'); 
       } 
   }
 ?>
4
  • query_posts should not be used Commented Jun 14, 2014 at 6:12
  • @PieterGoosen: he already had it in his code, I used it to clarify his own code as all he needed/wanted was the condition.
    – leromt
    Commented Jun 14, 2014 at 13:03
  • 1
    I just always think that someone must be made aware of something this wrong. I always make the OP aware of his problem and rectify it in my answer. The big thing here is, even the codex clearly states hat query_posts should not be used. Commented Jun 14, 2014 at 13:11
  • @PieterGoosen: Pieter, I don't disagree but sometimes people just need help to solve a specific problem they have with functioning code. I think it's great that you point it out and you should continue to do so. As a matter of fact, based on all the times I've seen you comment about it, I've stopped using it in my own code. Thanks for that ! :-)
    – leromt
    Commented Jun 14, 2014 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.