• Resolved mikelacroix

    (@mikelacroix)


    Hello!

    I’ve implemented the Page of Posts example from the Codex’s “Pages” page, and like it a great deal. However, I was wondering how can one make it so that multiple categories can show up, instead of just the one.

    Here’s the code:

    <?php
    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }
    if ($category) {
      $cat = get_cat_ID($category);
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $post_per_page = 4; // -1 shows all posts
      $do_not_show_stickies = 1; // 0 to show stickies
      $args=array(
        'category__in' => array($cat),
        'orderby' => 'date',
        'order' => 'DESC',
        'paged' => $paged,
        'posts_per_page' => $post_per_page,
        'caller_get_posts' => $do_not_show_stickies
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
      if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    I’m thinking there’s probably a better way of doing it instead of using a custom field, and having very rudimentary php knowledge I’m not sure how to figure it out. I also don’t mind hard-coding the Categories if need be, as the cats are few and there won’t be many additions.

    Any thoughts?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    give the value of your custom field “categorie” multiple categorie ID’s seperated by commas.
    and try it with this code (not tested):

    <?php
    if (is_page() ) {
    $category = get_post_meta($posts[0]->ID, 'category', true);
    }
    if ($category) {
      $multicats = explode(',', $category);
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $post_per_page = 4; // -1 shows all posts
      $do_not_show_stickies = 1; // 0 to show stickies
      $args=array(
        'category__in' => $multicats,
        'orderby' => 'date',
        'order' => 'DESC',
        'paged' => $paged,
        'posts_per_page' => $post_per_page,
        'caller_get_posts' => $do_not_show_stickies
      );
      $temp = $wp_query;  // assign orginal query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query($args);
      if( have_posts() ) :
    		while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    Thread Starter mikelacroix

    (@mikelacroix)

    It worked MAGNIFICENTLY. Thank you so much!!!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Page of Posts using multiple categories’ is closed to new replies.