WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [help] how to show a REAL single category ? (4 posts)

  1. c_a_k_r_a
    Member
    Posted 6 years ago #

    Case :
    post 1, category "banana"
    post 2, category "banana" and "apple"
    post 3, category "banana" and "manggo"
    post 4, category "banana"
    post 5, category "banana", "apple" and "manggo"

    Default result in "banana" category page ( http://my_site/category/banana/ ) will show post 1,2,3,4 and 5.

    Primary Question :
    How do I show only post 1 and 4 (it's mean the post which have only single category) in "banana" category page ?
    Since,
    1) condition is_category() didn't work for this case because is_category('banana') will always show post 1 - 5.
    2) query query_posts("cat=1"); or query_posts('category_name=banana'); has the same result too.

    Secondary Question :
    May be my primary question will solved if somebody can tell me how do I check if post have only pure one (single) category or more in query before push it to the loop ?

    I appreciate U'r help and thx before..

  2. c_a_k_r_a
    Member
    Posted 6 years ago #

    Somebody help... :(

  3. arickmann
    Member
    Posted 6 years ago #

    Try something like this inside the loop but before any of the post information:

    <?php
    $category_array = get_the_category();
    
    if ( count( $category_array ) > 1 ) { continue; }
    ?>
  4. arickmann
    Member
    Posted 6 years ago #

    Another option is to exclude all the other categories using query posts, so before the loop:

    //get the requested category
    $cat_id = array( get_query_var('cat') );
    
    //get all the category ids
    $cat_ids = get_all_category_ids();
    
    //remove the one we have from the one we want
    $cat_result_array = array_diff( $cat_ids, $cat_id );
    
    //create a new query to remove what we don't need
    query_posts( $query_string . "&cat=" . implode( ',-' , $cat_result_array ) );

    There is probably a much easier way to do it, but these are the ones that spring to mind.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags