Search code examples
phpwordpressposts

Get Wordpress posts from two categories


I am trying to display posts from two categories on one page (6 & 7). It should include posts from either category as well as the ones matching both. Unfortunately I have so far only found solutions for the latter which is why I am asking you for help now. This is my current code:

<div class="sponsoren_container_wrapper">
<?php $args = array ( 'posts_per_page' => -1, 'category' => 6 );
    $myposts = get_posts ( $args );
    foreach ($myposts as $post) : setup_postdata( $post ); ?>
    <div class="gallery_image_container">
        <a href="<?php
$content = get_the_content();
print $content;
?>" title="<?php the_title_attribute(); ?>">
            <div class="gallery_image_thumb">
                <?php the_post_thumbnail('thumbnail'); ?>
            </div>
            <div class="gallery_title">
                <h2>
                    <?php the_title(); ?>
                </h2>
            </div>
        </a>
    </div>
<?php endforeach; wp_reset_postdata(); ?>

</div> 

How would I be adding the category ID "7" to that code to make everything work as explained above? Thanks in advance!


Solution

  • use a comma seperated string:

    $args = array ( 'posts_per_page' => -1, 'category' => '6,7' );
    

    http://codex.wordpress.org/Template_Tags/get_posts