[Support request] Add element to posts by specific author

Home Forums Support Add element to posts by specific author

  • This topic has 19 replies, 3 voices, and was last updated 1 month ago by David.
Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #1621926
    Takeru

    Element ID
    /wp-admin/post.php?post=553&action=edit

    Author ID
    /wp-admin/user-edit.php?user_id=2

    Add a filter

    add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
        $author_id = get_post_field( 'post_author', get_the_ID() );
    
        if ( 553 === $element_id && '2' == $author_id ) {
            $display = true;
        }
    
        return $display;
    } );

    I added the above code to the Code Snippets plugin, but it did not work.

    Is there something wrong with the above code?

    #1622198
    David
    Staff
    Customer Support

    Hi there,

    what Element are you trying to display? That is specifically for the Hook Element, there are other filters for Header Element, Layout Element and Block Element.

    If it is the Hook Element – does it work if you do not use the filter and just set the Display Location ?

    #1622715
    Takeru

    I want to create and display a different author box for each author below the content using GenerateBlocks.

    So, if I set the Display Location without using a filter, it will show up in all the author’s posts.

    #1623422
    David
    Staff
    Customer Support

    Yes, i understand – just need to check whether the Hooked element is working correctly by giving it a specific display rule. If it does work then we can look at why that filter is not working.

    #1624125
    Takeru

    I added the code using the following page as a reference, changing only the ID, but it doesn’t work.

    https://generatepress.com/forums/topic/add-element-to-posts-by-specific-author/

    Which points should I check?

    #1624616
    David
    Staff
    Customer Support

    What Element is it you’re using ? Is it a Block Element ? If so instead of:

    generate_hook_element_display

    you use:

    generate_block_element_display

    #1625448
    Takeru

    I use the Block Element.

    I finally added the following code as you suggested, but I got an error.

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
        $author_id = get_post_field( 'post_author', get_the_ID() );
    
        if ( 553 === $element_id && '2' == $author_id ) {
            $display = true;
        }
    
        return $display;
    } );
    #1625824
    Elvin
    Staff
    Customer Support

    Hi,

    Can you provide the error you’re getting so we could have an idea?

    ( 553 === $element_id && '2' == $author_id ) on this line, can you try changing '2' to just 2.

    Let us know.

    A wise man once said:
    "Have you cleared your cache?"

    #1625827
    Takeru

    Thank you.
    However, it still doesn’t seem to work.

    The error seems to have security information in it and I am not sure which part to tell you.

    What part of the error should I tell you about?
    For example, is it a url like the following?

    /wp-includes/class-wp-hook.php on line 289 and exactly
    /wp-content/plugins/gp-premium/elements/elements.php(74): GeneratePress_Block_Element->__construct(97)

    The following errors are shown in bold.

    /wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 1

    #1625837
    Elvin
    Staff
    Customer Support

    You can use the private information text field for sensitive information so only the support members could see it.

    A wise man once said:
    "Have you cleared your cache?"

    #1625840
    Takeru

    Thank you.

    Now, I will send you the error after registering the following code.

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
        $author_id = get_post_field( 'post_author', get_the_ID() );
    
        if ( 553 === $element_id && 2 == $author_id ) {
            $display = true;
        }
    
        return $display;
    } );
    #1625871
    Elvin
    Staff
    Customer Support

    Try this instead:

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
        $author_id = get_post_field( 'post_author', get_the_ID() );
    
        if ( 553 === $element_id && '2' == $author_id ) {
            $display = true;
        } else if ( 553 === $element_id && '2' != $author_id ){ 
    	$display = false; 
        }
    
        return $display;
    }, 15, 2 );

    Are you applying it on a post list loop? If so, I don’t think this’ll work.

    But if it’s on single posts page, I can confirm that this particular snippet will work.

    A wise man once said:
    "Have you cleared your cache?"

    #1625875
    Takeru

    It worked!
    I greatly appreciate the support staff’s kindness and patience!

    #1625877
    Elvin
    Staff
    Customer Support

    It worked!
    I greatly appreciate the support staff’s kindness and patience!

    Nice one. Glad it worked for you. 🙂

    A wise man once said:
    "Have you cleared your cache?"

    #1627051
    Takeru

    On the author page, the block element I created goes under each post, is there any way to work around this?

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