- This topic has 7 replies, 2 voices, and was last updated 5 months ago by
David.
-
AuthorPosts
-
October 18, 2022 at 7:19 am #2377477
Dan
I have created a query loop which lists all the posts of a CPT in a single CPT template.
Similar to WP core class ‘current_page_item’ which is added to menu items for example, how can GP query loop add that class to an item when the user is on that same page. I would like to utilize that class for styling the current item in the list.Image:
https://www.dropbox.com/s/w7uoyp2lzrp8qe1/query-loop-current.jpg?dl=0Thanks,
DanOctober 18, 2022 at 8:02 am #2377703David
StaffCustomer SupportHi there,
not sure on this, its not as simple as hooking into a PHP loop and checking IDs of current object and current post.
Is it possible to see what you have so far ? Might help getting the grey matter workingDocumentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 18, 2022 at 9:19 am #2377816Dan
Thanks David for your response.
The site is currently on local dev, I’ll share once it’s online.
But I think it should be a basic functionality similar to the https://developer.wordpress.org/reference/functions/wp_list_pages/ functionality which adds classes to current page/post.Dan
October 19, 2022 at 6:31 am #2378668David
StaffCustomer SupportMy first thoughts would to use the
post_class
filter like so:function db_current_post_class( $classes, $class, $post_id ) { global $wp_query; $current_id = $wp_query->get_queried_object_id(); if( $current_id == $post_id ) { $classes[] = 'current-page-item'; } return $classes; } add_filter( 'post_class', 'db_current_post_class', 10, 3 );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 19, 2022 at 6:37 am #2378673Dan
Thanks David, that worked perfectly.
Question – does this run on all the queries of the site? Should this be a built-in feature of GP’s query loop block?
https://www.dropbox.com/s/uhgvyr47r2ib4z1/current-page-item.jpg?dl=0
DanOctober 19, 2022 at 6:57 am #2378691David
StaffCustomer SupportSo the
post_class
hook will fire anywhere that uses theget_post_class
function.
You should be able to do this and check the post type first:function db_current_post_class( $classes, $class, $post_id ) { if ( 'your-post-type' == get_post_type() ) { global $wp_query; $current_id = $wp_query->get_queried_object_id(); if( $current_id == $post_id ) { $classes[] = 'current-page-item'; } } return $classes; } add_filter( 'post_class', 'db_current_post_class', 10, 3 );
Adding it to the Query Loop, could be something we could add. I’ll raise an issue on it
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 19, 2022 at 7:16 am #2378710Dan
Great, thanks for your support!
Dan
October 19, 2022 at 8:52 am #2378976David
StaffCustomer SupportYou’re welcome
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.