Archived topic
Text on Custom Category Pages displaying on all Pages (Pagination)
24 replies · Started by Mark on August 13, 2021
Hi there,
I have created Elements for each Category. The idea is to add complementary text to each category to give them context (for the reader and Google).
I added as a Block > Hook > generate after header
The problem is that the text displays on all pages within the Category, not just the 1st page. How can I get the text to display on only the 1st page so that I avoid any duplicate content issues in the eyes of Google?
Thanks
Hi there,
Take a look at the third example here:
https://docs.generatepress.com/article/generate_element_display/
Adding PHP: https://docs.generatepress.com/article/adding-php/
Let me know if this helps :)
Hi Leo,
I believe this is the code I should be implementing:
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 10 === $element_id && is_paged() ) {
$display = true;
}
return $display;
}, 10, 2 );
In the notes on the link you sent, it says "You need to replace 10 with the ID of your Element." Do I replace both "10" placements in the above, and how do I determine the number of the Element(s) I want to change?
I have 3 elements I want to change - can I add them all in this one piece code, or do I need to copy/paste the code in functions.php for each Category and change the "10" of each?
Hi there,
only in this line - where it references the ID:
if ( 10 === $element_id && is_paged() ) {
You can multiple snippets for each of your conditons.
OR you can add multiple conditions if you like:
eg.
if ( ( 10 === $element_id || 20 === $element_id || 30 === $element_id ) && is_paged() ) {
This will check if any of the $element_IDs are present, AND if its paged.
NOTE: The || = OR
Thanks David.
How do I determine the Element ID? I have three of them. I don't see an ID within the Elements section of the WP Dashboard.
Hi Mark,
You can determine the Element ID by clicking "Edit" on the Element and checking the URL.
Normally you'll see something like this:
https://www.yoursite.domain/wp-admin/post.php?post=10&action=edit
The number post=10 is the element ID.
Alternatively, on Chrome Desktop, when you hover on the link to edit an Element, you normally see the destination of the link.
It should show the same URL sample I've given. From there, you should see the ID.
Hi Elvin,
I have added the code in functions.php, tweaked the post ID accordingly, and cleared the cache.
Unfortunately, it has not made any difference.
Can you share the exact code you're using and the type of Element you're trying to filter?
Also, are you using the latest version of GP Premium? (2.0.3)
Hi Elvin,
Yes, I'm using GP Premium (2.0.3).
I'm using a Child Theme (the template from GP)
In the functions.php section of the child theme, I've added the following code:
/*
========================================================
Display Custom Category text on 1st Category page ONLY
========================================================
*/
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 4003 === $element_id && is_paged() ) {
$display = true;
}
return $display;
}, 10, 2 );
=======
4003 is the postid for the Element I created for a particular category.
Element type: Hook
Hook name: after_header
Change the:
$display = true;
to:
$display = false;
Thanks David, that worked.
Is there an easy way that I can, after page 1, keep the header and breadcrumbs, but not the descriptive text?
We can hide it from being displayed.
Can you try adding this CSS?
body.paged .cat-description {
display:none;
}
And then go to the Block element's edit page and make sure to add cat-description on the Additonal CSS Class(es) of the Container Block holding the description. :)
Can you confirm exactly where I add this code? I seem to be getting errors when I add it. Here is what exists already:
add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 4003 === $element_id && is_paged() ) {
$display = false;
}
return $display;
}, 10, 2 );
I also want to let the breadcrumb display too
It's a CSS.
Here's how to add CSs - https://docs.generatepress.com/article/adding-css/
The quickest way is to add it on Appearance > Customize > Additional CSS. You should see a code area for CSS there. :)
I know where to add the code.
What I'm saying is I get an error when I add the code.
Here is a screenshot of the error: https://ibb.co/4WjcbLD