[Resolved] Add element to posts by specific author

Home Forums Support [Resolved] Add element to posts by specific author

Home Forums Support Add element to posts by specific author

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1286517
    Carson

    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!

    #1287561
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
    #1288779
    Carson

    Thank you!!!

    #1289254
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

    #1621686
    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?

    #1621823
    Leo
    Staff
    Customer Support

    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 ๐Ÿ™‚

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