WordPress.org

Ready to get started?Download WordPress

Forums

exclluding category from recent posts (5 posts)

  1. Web Hosting India
    Member
    Posted 4 years ago #

    hi,

    MY theme uses the following code to display recent posts on my home page ...

    <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

    i want to exclude a particular category posts from recent posts .. i mean i dont want to show posts of category id '5' in the recnt posts..

    how to do this..pls help me

  2. allstar
    Member
    Posted 4 years ago #

    The below when put in your active themes functions file will remove category '1234' from that URLs query even if there is already a category excluded or not.

    Also, if you want to remove it just from the homepage the is_home() conditional will only remove it from the homepage.

    function category_remove() {
    global $wp_query;
    $category_to_remove = 1234;
    if ( is_home() ) {
    $wp_query->query_vars['cat'] .= ( $wp_query->query_vars['cat'] == '' ) ? $category_to_remove : ','.$category_to_remove ;
    }
    }
    add_action('pre_get_posts', 'category_remove');
  3. Web Hosting India
    Member
    Posted 4 years ago #

    sorry i dint understand that ...

    my site is youngindiamovement.com

    i want to excude category 5 posts from thr recent posts of the blog on home page ..

    please add the code using category number '5'.. as i dnt understand tht much php//i am just a noob here..

    thanks again

  4. allstar
    Member
    Posted 4 years ago #

    Firstly, my virus scanner is picking up a trojan in your jQuery.js. I'm giving you the benefit of the doubt that this is unintentional.

    Secondly, there is only one '1234' shown in the code making it obvious what needs changing.

    Thirdly, thanks for showing me your site now I know you wanted to filter the recent posts in your sidebar on the homepage as opposed to the most recent posts on your homepage. Usually get_posts() is used for a sidebar post list which lead me to the wrong conclusion.

    Since it is a secondary loop perhaps you should try the codex page, it even has tutorials on your specific problem to help you.

    Good day.

  5. Web Hosting India
    Member
    Posted 4 years ago #

    Hi,

    firstly , when i scanned my webpages i dint find any thing..i have manually seen in my js files..but couldnt find any thing ..can u tell me what copy of antivirus are u using..

    secondly , i have understood only 1234 from the code is to be changed..can u please tell me whether i should replace my code with urs or shld i add it some where..please tell me the file name so that i can add it..

    Thirdly , i dont want it to display on sidebar ..but on home page u can find recent posts just below the featured section with the thumbnails beside them..i want to filter the code there..is the code u have given is same for that also?

    Thank you for ur help and support ..thanks again

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.