Site logo

Archived topic

I want only element on mainpage.

10 replies · Started by ER on July 30, 2022

Viewing posts 1–11 of 11

Hi. I use a hook and element on my categories on top of categories and on mainpage.

When I go to second category page with posts the element hook is still there. Is it possible to have the hook element only on mainpage or
top of 1st category page. How do I disable element for other category pages e.g. 2 3 4 and so on.

Hi there,

you can use a PHP Snippet like so:

add_filter( 'generate_element_display', function( $display, $element_id ) {
    if ( 99 === $element_id && is_paged() ) {
        $display = false;
    }

    return $display;
}, 10, 2 );

You need to change the 99 to the ID of your Element ( which can be found in the Browser URL when editing the element ).

How to add PHP:
https://docs.generatepress.com/article/adding-php/

Ok, so if I only want element on 1st page or mainpage of a category, it's the code I use?

It worked, thanks. Can I do like this:

add_filter( 'generate_element_display', function( $display, $element_id ) {
if ( 3137 === $element_id && is_paged() ) {
$display = false;
}

return $display;
}, 10, 2 );

*3137,3032,2352,2523,2523

Can I replace 3137 with this: 3137,3032,2352,2523,2523 if I want the function work for several elements?

Try this:

 add_filter( 'generate_element_display', function( $display, $element_id ) {
    $your_element_ids = array( 3137, 3032, 2352, 2523, 2523 );
    if (  in_array( (int)$element_id, $your_element_ids ) && is_paged() ) {
         $display = false;
     }
 
     return $display;
 }, 10, 2 );

Just update the $your_element_ids = array() values

Didn't work for me, you are sure this is correct?

Works now! Thanks a lot.

Glad to be of help

I use a hook and element on my categories on top of categories and on main page.

When I go to second category page with posts the element hook is still there. Is it possible to have the hook element only on main page or
top of 1st category page. How do I disable element for other category pages e.g. 2 3 4 and so on.

Hi. I have the same question as above but I would like to implement it for tags, can you provide me with the code for it?

Hi ER,

Will the Element be displaying on all categories archives- just the first page though?

This archived topic is closed to new replies.