• Hello, I am using this to list posts in a given category.

    <?php
    $cats = get_categories('include=11');
    foreach ($cats as $cat)
    {
        echo "<ul>";
        $custom_loop = new WP_Query('posts_per_page=-1&cat=' . $cat->cat_ID);
        while ($custom_loop->have_posts())
        {
            $custom_loop->the_post();
            $category = get_the_category();
            // Only display a post link once, even if it's in multiple categories
            if ($category[0]->cat_ID == $cat->cat_ID)
            {
                echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
            }
        }
        echo "</ul>";
    }
    wp_reset_query();

    How can I add a tag to this?

    I’d like to list posts in a given category that also have a given tag.

    Thanks

Viewing 1 replies (of 1 total)
  • You use get_categories('include=11') and loop, as if there would be more than one.
    In the post loop, you use get_the_category() which is only for categories. You have to use get_the_terms() if you want to handle tags instead of categories.

    But you should read about tax_query parameters for WP_Query.
    This is the object WP_Tax_Query. And it is constructed when you use tax_query on WP_Query.

Viewing 1 replies (of 1 total)
  • The topic ‘Join category and tag’ is closed to new replies.