Support » Fixing WordPress » Display only one category on home page

  • Resolved Akasashasha

    (@akasashasha)


    Dears,

    I’m using the theme Esplanade by one designs.
    I’d like to display only one posts category on the home page (category n° 15) … I found a few old post on the forum but I did not manage to make them work.
    I assume I need to add some code either in my index.php or in the function.php ? can someone help me please ? thanks …
    for your infos : the only modification I made is in the function.php file, I added this lines

    function my_post_queries( $query ) {
    // not an admin page and is the main query
    if (!is_admin() && $query->is_main_query()){
    if(is_home()){
    $query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
    }
    }
    }
    add_action( ‘pre_get_posts’, ‘my_post_queries’ );

    (the posts marked as sticky appear only in the slideshow but not below on home page)

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi, you can try this

    function my_post_queries( $query ) {
      // only homepage and is the main query
      if ($query->is_home() && $query->is_main_query()){
        // display only posts in category with id=32
        $query->set('cat', 32);
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
    Thread Starter Akasashasha

    (@akasashasha)

    I tried your suggestion but get an error message probably because of the above bit of codding, can you advise me how to write properly the following or maybe how to merge this two in one :

    [ Moderator Note: Please post code or markup snippets between backticks or use the code button not blockqoute. ]

    function my_post_queries( $query ) {
    // not an admin page and is the main query
    if (!is_admin() && $query->is_main_query()){
    if(is_home()){
    $query->set('post__not_in', get_option( 'sticky_posts' ) );
    }
    }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );
    
    function my_post_queries( $query ) {
      // only homepage and is the main query
      if ($query->is_home() && $query->is_main_query()){
        // display only posts in category with id=32
        $query->set('cat', 32);
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    as mentioned above the first part is to avoid sticky post appearing on home page (only appear in slider) and the second part is your suggestion … thanks for your help …

    Hi, you are using two functions with the same name – hence you get an error.

    use this instead:

    function my_post_queries( $query ) {
      // only homepage and is the main query
      if ($query->is_home() && $query->is_main_query()){
         // display only posts in category with id=32
         $query->set('cat', 32);
         // avoid sticky posts
         $query->set('post__not_in', get_option( 'sticky_posts' ) );
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    where you have to change the value 32 to match your category id.

    Thread Starter Akasashasha

    (@akasashasha)

    works perfectly, thanks … I have another question but will make a new post for that … see you, and thanks again …

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display only one category on home page’ is closed to new replies.