Home › Forums › Pro Support › Filtering category views to showpost pages
- This topic has 5 replies, 2 voices, and was last updated 4 years, 3 months ago by
Tom.
-
AuthorPosts
-
August 4, 2019 at 10:59 am #10599
Ian
ParticipantHi – This is a lovely filter to switch category views into a show posts page. It is a bit ‘fragile’ because there’s no error handling. I’ve inserted a conceptual query with an enquiry below. Forgive me for asking but I’m a wordpress baby – although not a php one – and I’m finding my way round the code with care…Apart from the case where the page has a very large number of pages which would present a performance overhead issue – would this be an useful strategy?
add_filter('request', function( array $query_vars ) { if ( is_admin() ) { return $query_vars; } if ( isset( $query_vars['category_name'] )) {
PSEUDOCODE: iterate through array of pagenames. If == found between pagename and $query_vars[‘category’] execute inner if loop ELSE RETURN
if ( isset( $query_vars['category_name'] ) ) { $pagename = $query_vars['category_name']; $query_vars = array( 'pagename' => "$pagename" ); } } return $query_vars; } );
August 6, 2019 at 5:19 pm #10622Tom
KeymasterHi there,
I’m not too sure what you mean – it looks the same as the code we have here: https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/
What about it did you change, exactly?
August 8, 2019 at 2:33 pm #10659Ian
ParticipantSorry. I didn’t change the code. I was suggesting that when I wrote ‘pseudocode’ iterate through an array of pagenames, there was a opportunity to put an error handling loop in which refuses to execute if there is no matching slug for the the filter to act on. I’m just thinking of the real world case where muggins who shouldnt changes the slug of a category replacement page. But I don’t know. The effect of being able to put show posts into the category page is awesome and your work is much appreciated
August 8, 2019 at 4:59 pm #10663Tom
KeymasterAh, good point.
I wonder if this would work?:
add_filter('request', function( array $query_vars ) { if ( is_admin() ) { return $query_vars; } if ( isset( $query_vars['category_name'] ) ) { $pagename = $query_vars['category_name']; if ( get_page_by_title( $pagename ) ) { $query_vars = array( 'pagename' => "$pagename" ); } } return $query_vars; } );
Let me know 🙂
August 11, 2019 at 3:58 am #10705Ian
ParticipantDear Tom,
Thank you for suggesting that nested IF code. I can confirm that it handles the error ‘silently’ and swiftly which is what one would want – unless I guess you are trying to error handle why the filtered ShowPosts category page doesn’t appear… But yes it works. You can check out what I’m up to here:
August 11, 2019 at 5:25 pm #10721Tom
KeymasterAwesome, glad it’s working 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.