wordpress - How to list post of certain category (only one category lets say cat id = 5 ) only on blog listing page? -


i need list posts of category on blog listing page , let's of category id 5. need plugins or function.php file. not want change template files, think blog listing on index.php.

i used parse_query hook following. affect other places . menu bar gone. please me. thank you.

 add_filter( 'parse_query', 'pp_posts_filter' );    function pp_posts_filter( $query ){      $query->query_vars['cat'] = 5;   } 

to make query changes specific main query , not secondary ones menus or sidebars etc. use is_main_query function i.e.

 add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' ); function foo_modify_query_exclude_category( $query ) {     if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )         $query->set( 'cat', '-5' ); } 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -