For over 3 days I am trying to filter posts in certain blog pages by certain categories on my wordpress theme: "Grafika".For example: I create a blog page called "Friends", after that I create a category named "friends" and after that I create 5 posts and I assign the "friends" category to those 5 posts.How can I do that on the "Friends" page to display only the posts from the category "friends".Currently the page shows all my blog posts from all pages.
I have tried many many plugins, query_posts, query_args, shortcut codes in page, modifying template. Actually I have only 1 plugin wich almost fixed my problem.The plugin it's called "wp posts filter".But it doesn't fully work.The problem with this plugin is that I am applying a filter for Home Page and that filter goes to all my pages no matter that for other pages I applyied different filters.This is the plugin link: here
Can somebody give me a REALLY WORKING solution to filter posts display by category on pages ? Thank you very much for reading !
In your functions.php
file of your theme, we can use the pre_get_posts
function to alter the query before the page load.
function my_friends_category( $query ) {
if ( $query->is_page('friends')):
$query->set( 'cat', 'friends' );
endif;
}
add_action( 'pre_get_posts', 'my_friends_category' );
This is assume the name of your page is friends
otherwise replace it with the id
of the page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.