0

I have several loops each of them built to display a certain category in different style, based on user's input (they're within shortcodes). Now I want to "merge" these (or just 1 of them) loops with the loop to display recent posts, regardless of the category.

Currently this is my code (to display post from a certain category):

$args = array(
  'category_name' => $category, 
  'showposts' => $numposts, 
  'order' => $order
);

$query = new WP_Query( $args );

while ($query->have_posts()) : $query->the_post();
// loop's usual stuff

As I said, they are inside shortcodes, so, $category, $numposts (number of posts) and $order are user's input (shortcode's atts)

Anyway, to display the recent posts, all I have to do is take away the category part. However, when I did that from the shortcode's "side" (by not filling the shortcode and not displaying it), the loop is broken. I suppose it is because I have declared "category" within the query parameters.

I've tried:

$args = array(
   if (!empty($category)) {
    'category_name' => $category,
  }
  'showposts' => $numposts, 
  'order' => $order
);

but of course, it is throwing an error :P. Well, I guess at least it is better explaining what I am trying to achieve.

My question: Is there anyway around this or should I build a new shortcode / loop, specific to just display the recent posts?

Thanks in advance.

EDIT:

Longer code:

function boxed_category($atts, $content) {
    extract(shortcode_atts(array(
      "category" => '',
      "numposts" => '-1',
      "order" => 'ASC'
    ), $atts));

    $output = '';

    $args = array(
      'category_name' => $category, 
      'showposts' => $numposts, 
      'order' => $order
    );

    $query = new WP_Query( $args );

    while ($query->have_posts()) : $query->the_post();

well, not many difference - function name because it is originally made to display a certain category (inside "boxed" - CSS) - I want to turned this shortcode to have a "Recent Posts" option. I'm going to need more atts, but first, need to figure out how to "get rid" of $category from the query should a user chose "Recent Posts" option.

  • Please post all of the relevant code. – s_ha_dum Jul 21 '13 at 22:48
  • Just edited to original post. – Mario88 Jul 21 '13 at 23:13
0

I would change

$args = array(
      'category_name' => $category, 
      'showposts' => $numposts, 
      'order' => $order
    );

to

$args = array(
      'showposts' => $numposts, 
      'order' => $order
    );

if ( $category )
     $args['category_name'] = $category;

This way you never pass an empty category_name parameter as a query arg.

  • Thanks Helga.. D**n, I really need more experience in PHP :P – Mario88 Jul 22 '13 at 0:09
0

...need to figure out how to "get rid" of $category from the query should a user chose "Recent Posts" option

This is barely a WordPress question, if at all, but just don't use category if you don't have one.

$args = array(
  'showposts' => $numposts, 
  'order' => $order
);
if (!empty($category)) {
  $args['category_name'] = $category;
}

The same for any of your other optional arguments.

  • Thanks s_ha_dum, I voted Helga's because hers is on the top, but I believe yours would work as well. – Mario88 Jul 22 '13 at 0:10
  • @Mario88 : Actually, answers aren't necessarily ordered oldest to newest but answers shouldn't be selected by that criteria anyway. Choose the best answer and upvote any helpful ones. – s_ha_dum Jul 22 '13 at 0:37
  • I said that because both of the answers are basically doing the same thing. Somehow, Helga's appear first (although, I remember that both you and Helga answered at the same minutes - both were "15 minutes ago" - at the time I checked). I don't know if, maybe there is an upside / downside for each of the answers, but I decided to go according the order. In anyway, I greatly appreciate your answer / responds :) EDIT: Tried to upvote your answer (as it is correct and helpful) but apparently I need 15 points. I'll remember it for the next time. – Mario88 Jul 22 '13 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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