I am playing with the beta on this site http://dev-rns-dc.pantheonsite.io/ and inside the header builder area it’s populating all the posts rather than the pages. I saw in a different thread (https://theme.co/apex/forum/t/assign-header-footer-to-pages/27393/8) that there is a limit in loading the posts and pages. is there any way around this? I’ll never need to assign custom headers to the posts but I will need to do it for the pages. Thanks!
Hi @suzannereid,
Thanks for reaching out about this. Yes, there is a built in limitation on the number of posts to be retrieved for performance. It will never load more than 2500 posts (of any type) and the results aren’t actually sorted so it could be mixture of items. We observed that going higher than this risks crashing the application on underpowered servers that have large databases. There are two ways you can modify this behavior, but both require adding a code snippet in functions.php of a child theme.
Option 1 - Increase the query limit. This would for example allow 10,000 entries to populate.
add_filter('cs_query_limit', function() {
return 10000;
});
Option 2 - Restrict the posts types. This would be better for performance if you truly don’t need the posts.
add_filter('cs_header_assignment_post_types', function( $post_types ) {
return array( 'page' );
});
Update: Check out RC4 once it’s available. I reversed the sorting which naturally makes pages come up first. You can still use the above code snippets to fine tune your results.
aah sweet, thank you so much, Alex! I’ll swing back around if I come up with any issues! Appreciate it!
Sounds good! I was able to get RC4 pushed out Friday so just let me know if you have any other issues.
So If I were to do option 2 for performance’s sake, how can i show both pages and archives?
Hi again,
I’m sorry, this isn’t possible because it needs to encounter a post of the given type before it registers that type at all. This is something we can improve later on. For now, it is always possible to programmatically determine a header assignment based on the current query. Here’s an example:
add_filter( 'cs_locate_header_assignment', function( $match ) {
// Conditionally assign $match to the numeric ID of your header.
// The ID can be found in the URL when editing a header.
if ( is_post_type_archive( 'thing' ) ) {
$match = 1234;
}
if ( is_tax( 'my-taxonomy' ) || is_tax( 'actor' ) ) {
$match = 4321;
}
return $match;
});
Thank you so much, Alex! I appreciate your help!
You’re most welcome!
Quick question regarding this code-- will this work for individual posts within a certain category?
Sure! It runs after the WordPress query, so you can use all the WordPress templating functions. For example:
$term = get_the_category();
if ( $term && $term->slug = 'category-slug') {
return 1234; // Numeric header ID
}
I discovered that and it worked!
This was my code:
add_filter('cs_match_header_assignment', 'custom_search_header');
function custom_search_header($match) {
if ( in_category('opinion') ) {
$match = 3795013;
}
if ( in_category('news') ) {
$match = 3795087;
}
return $match;
}
Ok great! Yours looks nicer too! Nice job. Glad to hear you got it worked out.
I’m wondering if my code isn’t specific enough because it the header shows on other categories as well:
https://live-rns-dc.pantheonsite.io/category/faith/islam/
I just want it to show on the opinion and news categories as well as their posts. Thoughts?
Is it possible that a latter condition applies, which overrides something earlier? You could short circuit it by just returning right away. Here’s an idea that may help:
$is_opinion = in_category('opinion') || is_category('opinion');
$is_news = in_category('news') || is_category('news');
if ( $is_opinion ) {
return 3795013;
}
if ( $is_news ) {
return 3795087;
}
Ok, i’ll try that, thank you!
You’re welcome!
Hey Alexander, this is a very big problem and also it has some caching errors over PRO. You can’t even select all post to be showed in the header right sidebar button.
How could I assign all categories to one header?
Hi @armandogomez,
I’m not sure what you mean by caching errors. Let me know if there’s any additional information you could provide about this.
To match a header to all categories You could try something like this:
add_filter( 'cs_locate_header_assignment', function( $match ) {
if ( is_archive() && ! is_home() ) {
$match = 4321;
}
return $match;
});
Inside that function you can use any template functions to conditionally return a header ID based on the current WordPress query.