Site logo

Archived topic

Author box via hook element

17 replies · Started by Nadine on October 28, 2022

Viewing posts 1–15 of 18

Hello, I have created an author box with Element - Hook and dynamic data. We have multiple authors. However, I want to show this box only for three specific author names. How is that possible? because I haven´t no selection in the display box.

Hi there,

you would need to use a PHP Snippet to disable the Block Element for posts that have different authors.

Try this:


add_filter( 'generate_element_display', function( $display, $element_id ) {

    $author = get_the_author();
    $author_list = array( 'display name', 'another name', 'another name' );
    
    if ( 100 === $element_id && in_array( $author , $author_list ) ) {
        $display = true;
    }

    return $display;

}, 10, 2 );

1. you need to update the array of author display names with the authors who will have the author box here:
$author_list = array( 'display name', 'another name', 'another name' );

2. The 100 is the ID of the Element, swap that for the one found in your Elements URL when editing it.

Thank you for your feedback.

I get a error message about snippet:
syntax error, unexpected token ";"

And what do you mean about topic 1 and 2?

I am really sorry about that, i have updated the code above

Ok in that code there are things you need to change:

1. In this line of the code:
$author_list = array( 'display name', 'another name', 'another name' );

You need to change the values in the array to the display names of your 3 authors.

2. Edit your block element and check the browser URL field and make a not of the ID= value eg.. 55.
In the code above, change this line:

if ( 100 === $element_id && !in_array( $author , $author_list ) ) {


Hello, i´m sorry, but it doesn`t works. I send you two links of two images. That are screenshots of author-box (with URL wp-admin/post.php?post=156224&action=edit) and php-script.
Maybe do you see a mistake because i can always see the box in all blog articles of all authors and not only of this three names.

Hello David,

i took this code again, i changed array field with author names and i replaced again the id number with url-id. But it doesn´t works???
Is it possible, to add a fields in hook-location like blog-author? So that i can select an author name?

Sorry about this, the author name always messes me up.
Try this snippet:



add_filter( 'generate_element_display', function( $display, $element_id ) {
	
	// list of authors to show element too
	$author_list = array( 'display name', 'another name', 'another name' );
	
	// Get the current post author
	global $post;
	$author_id = get_post_field( 'post_author', get_the_id() );
        $author = get_the_author_meta('display_name', $author_id);
    
        // if author in array the set display to true
	if ( 137 === $element_id && is_single() && !is_admin() && in_array( $author , $author_list ) ) {
            $display = true;
        }

        return $display;

}, 10, 2 );

Hello David,
i`m sorry but it doesn´t works. Maybe it is better to cancel this author box?

Hmmm... did you se the array: $author_list = array( 'display name', 'another name', 'another name' ); to include the author display names ?

Yes I have seen. And i filled it with my author-names

And the ID of the element? ie.

The 137 in this line needs changing to match the Elements ID?

if ( 137 === $element_id

Sorry for the back and forth, but i tested the code on my local site and it works.

Can you Edit the Element and remove ALL the Display Rules. There should be none as the code is what sets them.

Hello David, yes I have deleted all display rules, but the box will not show in my blog posts.

This archived topic is closed to new replies.