Using PHP to display one category on page.
-
Hi,
I have come across this piece of code from this website.
function my_home_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '2'); } } add_action( 'pre_get_posts', 'my_home_category' );
So how it works is my posts index page is called ‘work’, i also have a category for posts called ‘work’ with an id of ‘2’. The above code is placed into the functions.php file. It then only displays posts of category id ‘2’ on the posts index page. It works great, I have it running perfectly and its all fine.
My question is if i create a second page called ‘blog’ and have a category called ‘blog’ setup with an id of ‘3’, could i manipulate this piece of code to only display category ‘3’ on the ‘blog’ page?
Being fairly new to PHP and looking at the code the only reference I can see to the index page is
$query->is_home()
.Anyway, any help would be greatly appreciated.
- The topic ‘Using PHP to display one category on page.’ is closed to new replies.