Site logo

Archived topic

Add element to posts by specific author

19 replies · Started by Takeru on January 16, 2021

Viewing posts 1–15 of 20

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?

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 ?

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.

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.

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

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;
} );

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.

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

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

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;
} );

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.

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

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

Nice one. Glad it worked for you. :)

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

This archived topic is closed to new replies.