[Support request] I want only element on mainpage.

Home Forums Support [Support request] I want only element on mainpage.

Home Forums Support I want only element on mainpage.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2298586
    ER

    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.

    #2298748
    David
    Staff
    Customer Support

    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/

    #2298750
    ER

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

    #2298791
    ER

    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?

    #2298796
    David
    Staff
    Customer Support

    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

    #2298831
    ER

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

    #2298833
    David
    Staff
    Customer Support

    Oh my – sorry i pasted in the wrong code …. i fixed it here:

    https://generatepress.com/forums/topic/i-want-only-element-on-mainpage/#post-2298796

    give that a try

    #2298840
    ER

    Works now! Thanks a lot.

    #2298845
    David
    Staff
    Customer Support

    Glad to be of help

    #2334170
    ER

    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?

    #2334318
    Fernando
    Customer Support

    Hi ER,

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

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.