Archived topic
[GPP 2.0 beta] Content template not working with static archives
9 replies · Started by George on April 6, 2021
I am using this coode to set static pages as category archives:
//Use Static Pages as Category Archives
add_filter('request', function( array $query_vars ) {
if ( is_admin() ) {
return $query_vars;
}
if ( isset( $query_vars['category_name'] ) ) {
$pagename = $query_vars['category_name'];
$query_vars = array( 'pagename' => "$pagename" );
}
return $query_vars;
} );
The template is not being displayed on the static category page, though. It just displays static content. When I deactivate the code, the template is displayed normally. Is there a way around it?
Hi there,
what is the Content Template being used for ? Is it to display the archive loop ?
Yes.
The templates only have a single loop to display the_content. So it's either an archive loop or its a page content loop.... not both.
Aside of writing your own custom templates, the alternative would be to hook in the Page content that would need to be pulled in from a custom post type / custom fields.
But I've set my category pages to be static pages so, in theory, it should work, no? Can I at least exclude a certain category from the code provided in the first post?
Nope - that code is changing the category archive request to a static page - the archive template which has the_loop for displaying the archives is not being loaded.
To exclude a category from static pages - you could change the first condition ie.
if ( is_admin() ) {
to include a is_category template tag eg.
if ( is_admin() || is_category('your-category-slug') ) {
Hmmm. I tried your suggestion but I am getting a 404 error on the selected category. I've also deleted the original Travel static category page and resaved my permalinks.
add_filter('request', function( array $query_vars ) {
if ( is_admin() || is_category('travel') ) {
return $query_vars;
}
if ( isset( $query_vars['category_name'] ) ) {
$pagename = $query_vars['category_name'];
$query_vars = array( 'pagename' => "$pagename" );
}
return $query_vars;
} );
Hmmm... not sure on this - you could try removing that condition and add a secondary condition to the $query_var addition:
e.g
if ( isset( $query_vars['category_name'] ) ) {
$pagename = $query_vars['category_name'];
if ( $pagename != 'your-category-slug') {
$query_vars = array( 'pagename' => "$pagename" );
}
}
Perfect, that did the trick!
Thanks, David.
Great - glad to hear that!!