- This topic has 23 replies, 2 voices, and was last updated 2 years, 9 months ago by Elvin.
-
AuthorPosts
-
July 18, 2021 at 4:38 pm #1860366nomadiceman
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?
July 18, 2021 at 9:04 pm #1860460ElvinStaffCustomer SupportHi 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 );
July 18, 2021 at 9:41 pm #1860475nomadicemanOk thank you. Im a little confused. Can you give an example of what you mean?
July 18, 2021 at 9:52 pm #1860486ElvinStaffCustomer SupportCheck 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.July 18, 2021 at 10:39 pm #1860526nomadicemanthis 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?
July 19, 2021 at 12:34 am #1860578ElvinStaffCustomer SupportYou’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>
July 19, 2021 at 12:58 am #1860615nomadicemanAwesome. Ill give all that a try.
Thank you
July 19, 2021 at 8:27 pm #1861937nomadicemanIs 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 );
July 19, 2021 at 8:43 pm #1861957ElvinStaffCustomer SupportYou’ll have to change the
10
and20
on the conditions of the code to the IDs of the 2 header elements you’re using. 😀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
July 19, 2021 at 8:47 pm #1861963nomadicemanwhat 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 20So to me that means if first page show both 10 and 20
Am I being stupid? lol
July 19, 2021 at 8:56 pm #1861978ElvinStaffCustomer Supportif 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 20ah 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.
July 19, 2021 at 9:11 pm #1861997nomadicemanHmm, 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?
July 19, 2021 at 9:16 pm #1862004ElvinStaffCustomer SupportAh 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
isgenerate_block_element_display
.July 19, 2021 at 9:18 pm #1862006nomadicemanAh great. Thank you. I’ll give that a shot now
I was going insane trying to figure that out 😂
July 19, 2021 at 9:25 pm #1862011nomadicemanIt seems to be working however the Element header 1 is now appearing I the other category archives
Like its not following the elements rules
-
AuthorPosts
- You must be logged in to reply to this topic.