WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Featured post in current category page by specific tag (5 posts)

  1. jumust
    Member
    Posted 2 years ago #

    Hi,
    I want to build a featured section for each category page that will show one post of the current category page by a specific tag (featured).

    I ended up using this but it shows the same post with the "featured" tag in all categories....

    How can I use the variable category to get the featured post from the current category?

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    untested - try to add the current category into the query:

    $args = array(
            'category__in' => array(get_query_var('cat')),
            'posts_per_page' => 1,
            'tag' => 'featured',
        );

    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

  3. jumust
    Member
    Posted 2 years ago #

    Great it worked! Thanks
    what should I add to not duplicate the featured post with posts the loop below the featured section?

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    what should I add to not duplicate the featured post with posts the loop below the featured section?

    check:
    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action
    particular the section after the headline 'Note for Multiple Posts in the First Category'
    i.e. using an array for $do_not_duplicate and the 'post__not_in' parameter in a query for the rest of the posts.

    example - assuming that you have the post ID of the 'featured' post in the $do_not_duplicate array;

    global $wp_query;
    $args = array_merge( $wp_query->query, array( 'post__not_in' => $do_not_duplicate ) );
    query_posts( $args );

    http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

    you could also paste the full code of your category archive template (into a pastebin ...) and someone might have a look at your code.

  5. jumust
    Member
    Posted 2 years ago #

    Awesome I figured that out
    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic