Site logo

Archived topic

Add element to posts by specific author

5 replies · Started by Carson on May 15, 2020

Viewing posts 1–6 of 6

Hi there,

is there any way to display an element only on posts by specific authors?

Alternatively, can I add CSS to pages by specific authors in any other way?

Thanks!

Hi there,

You can:

1. Add a new Element, but leave the Display Rules empty.
2. Take note of the Element ID (in the URL while editing the Element).
3. Add a filter:

add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
    $author_id = get_post_field( 'post_author', get_the_ID() );

    if ( 1234 === $element_id && '123' == $author_id ) {
        $display = true; // If Element ID is 1234 and author ID is 123, display the Element.
    }

    if ( 5678 === $element_id && '456' == $author_id ) {
        $display = true; // If Element ID is 5678 and author ID is 456, display the Element.
    }

    return $display;
} );

Thank you!!!

You're welcome :)

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,

The code looks good to me.

Any chance you can open a new topic and link us to the page in question?

Thanks :)

This archived topic is closed to new replies.