[Support request] Author box via hook element

Home Forums Support [Support request] Author box via hook element

Home Forums Support Author box via hook element

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2391088
    Nadine

    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.

    #2391271
    David
    Staff
    Customer Support

    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.

    #2391393
    Nadine

    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?

    #2391437
    David
    Staff
    Customer Support

    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 ) ) {

    #2391468
    Nadine


    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.

    #2391840
    David
    Staff
    Customer Support
    #2391874
    Nadine

    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?

    #2392575
    David
    Staff
    Customer Support

    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 );
    #2404490
    Nadine

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

    #2404708
    David
    Staff
    Customer Support

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

    #2404767
    Nadine

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

    #2404883
    David
    Staff
    Customer Support

    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.

    #2404978
    Nadine

    Yes of course. I have changed id-number like 156224 (https://www.kagels-trading.de/wp-admin/post.php?post=156224&action=edit)

    #2405001
    David
    Staff
    Customer Support

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

    #2406175
    Nadine

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

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