Archived topic
Header Element different one for the first page of Category archive only?
23 replies · Started by nomadiceman on July 18, 2021
Hi,
I am trying to make it so that my 1st category archive page has a header element with lots of text in it, explaining what this category is all about
But on the next pages of that archive, page 2,3,4... etc id like a different header element. As on the later page I don't need to have all the text and it'll be canonicalised to the first archive page anyway
Is that possible?
Hi there,
You can try making 2 different header elements and then filter them both using !is_paged().
Example:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && !is_paged() ) { //if 1st page of archive, display header element id 10
$display = true;
}
if ( 10 === $element_id && is_paged() ) { //if page 2 onwards of archive, don't display header element id 10
$display = false;
}
if ( 20 === $element_id && !is_paged() ) { //if 1st page of archive, don't display header element id 20
$display = false;
}
if ( 20 === $element_id && is_paged() ) { //if 1st page of archive, display header element id 20
$display = true;
}
return $display;
}, 10, 2 );
Ok thank you. Im a little confused. Can you give an example of what you mean?
Check these 2 pages.
Category Campaigns archive page home - https://dev-generate-press.pantheonsite.io/category/campaigns/
This one is using Header 1 (in red)
Category Campaigns archive page #2 - https://dev-generate-press.pantheonsite.io/category/campaigns/page/2/
You can see that they're using 2 different Header Elements.
This one is using Header 2 (in blue )
Through the PHP I've provided, I've changed how the 2 Header elements will display.
For Header 1, I've assigned it to display on Campaign archive's home page ONLY.
For Header 2, I've filtered it to display on Campaign archive pages 2 onwards.
On my PHP snippet, you basically have to change the $element_id value it checks on the conditions to match your Header Element's ID.
this is awesome! thank you for the effort to explain that
I will give that a try now
One thing, is there a way to put "Page 2" etc etc in the titles?
Using you example https://dev-generate-press.pantheonsite.io/category/campaigns/page/2/, the title would say "Campaigns Header 2 - Page 2"
Is that possible?
You'll need a shortcode to get the current page number.
Here's a PHP snippet for it.
add_shortcode( 'current_page', function() {
ob_start();
// Start your PHP below
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo $paged;
// End your PHP above
return ob_get_clean();
} );
This lets you use a simple [current_page] that displays the current page number.
As for usage:
Say, for example you want "Title - page # n" where n is the page number.
You can add something like this on the Header Element code area.
<h1 style="color: white;">
{{post_title}} - page # [current_page]
</h1>
Awesome. Ill give all that a try.
Thank you
Is this code correct? It doesn't appear to work. From my very limited understanding I don't think its coded correctly
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && !is_paged() ) { //if 1st page of archive, display header element id 10
$display = true;
}
if ( 10 === $element_id && is_paged() ) { //if page 2 onwards of archive, don't display header element id 10
$display = false;
}
if ( 20 === $element_id && !is_paged() ) { //if 1st page of archive, don't display header element id 20
$display = false;
}
if ( 20 === $element_id && is_paged() ) { //if 1st page of archive, display header element id 20
$display = true;
}
return $display;
}, 10, 2 );
You'll have to change the 10 and 20 on the conditions of the code to the IDs of the 2 header elements you're using. :D
You can find out what's the ID of the Header element by editing it and checking it on the URL as shown in this example https://share.getcloudapp.com/geugG5X6
what I mean is the text you have written. it doesn't seem to make sense to me:
you've said:
if 1st page of archive, display header element id 10
if page 2 onwards of archive, don't display header element id 10
if 1st page of archive, don't display header element id 20
if 1st page of archive, display header element id 20
So to me that means if first page show both 10 and 20
Am I being stupid? lol
if 1st page of archive, display header element id 10
if page 2 onwards of archive, don’t display header element id 10
if 1st page of archive, don’t display header element id 20
if 1st page of archive, display header element id 20
ah yeah the comment is wrong. My bad.
You can ignore or better yet, completely remove it as it's not an important part of the code.
Example:
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 1234 === $element_id && !is_paged() ) {
$display = true;
}
if ( 1234 === $element_id && is_paged() ) {
$display = false;
}
if ( 5678 === $element_id && !is_paged() ) {
$display = false;
}
if ( 5678 === $element_id && is_paged() ) {
$display = true;
}
return $display;
}, 10, 2 );
This code set header ID 1234 to display on the 1st page of the archive page and set header id 5678 on page 2 onwards.
Hmm, seems still not to function correctly.
Ive added this in the "Code Snippet" plugin, is it correct? I've give you the link to my staging site
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
if ( 9508 === $element_id && !is_paged() ) {
$display = true;
}
if ( 9508 === $element_id && is_paged() ) {
$display = false;
}
if ( 11954 === $element_id && !is_paged() ) {
$display = false;
}
if ( 11954 === $element_id && is_paged() ) {
$display = true;
}
return $display;
}, 10, 2 );
9508 - The top element header
11954 - The element header that ive put "Page 2+" text to the side of the title
Any ideas of what's going on?
Ah I see the issue here.
I thought you were using Header Element. This page is using a Block element as a header.
In this case we can simply change the filter name from generate_header_element_display is generate_block_element_display.
Ah great. Thank you. I’ll give that a shot now
I was going insane trying to figure that out 😂
It seems to be working however the Element header 1 is now appearing I the other category archives
Like its not following the elements rules