- This topic has 17 replies, 3 voices, and was last updated 4 months, 2 weeks ago by
Ying.
-
AuthorPosts
-
October 28, 2022 at 2:04 am #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.
October 28, 2022 at 4:26 am #2391271David
StaffCustomer SupportHi 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.Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 28, 2022 at 6:04 am #2391393Nadine
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?
October 28, 2022 at 6:41 am #2391437David
StaffCustomer SupportI 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 ) ) {
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 28, 2022 at 6:56 am #2391468Nadine
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.October 28, 2022 at 8:29 am #2391840David
StaffCustomer SupportI made change to the code above:
https://generatepress.com/forums/topic/author-box-via-hook-element/#post-2391271
Can you try that.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 28, 2022 at 8:54 am #2391874Nadine
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?October 29, 2022 at 5:15 am #2392575David
StaffCustomer SupportSorry 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 );
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 7, 2022 at 2:26 am #2404490Nadine
Hello David,
i`m sorry but it doesn´t works. Maybe it is better to cancel this author box?November 7, 2022 at 6:12 am #2404708David
StaffCustomer SupportHmmm… did you se the array:
$author_list = array( 'display name', 'another name', 'another name' );
to include the author display names ?Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 7, 2022 at 7:10 am #2404767Nadine
Yes I have seen. And i filled it with my author-names
November 7, 2022 at 7:49 am #2404883David
StaffCustomer SupportAnd 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.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 7, 2022 at 7:58 am #2404978Nadine
Yes of course. I have changed id-number like 156224 (https://www.kagels-trading.de/wp-admin/post.php?post=156224&action=edit)
November 7, 2022 at 8:12 am #2405001David
StaffCustomer SupportCan you Edit the Element and remove ALL the Display Rules. There should be none as the code is what sets them.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/November 8, 2022 at 6:10 am #2406175Nadine
Hello David, yes I have deleted all display rules, but the box will not show in my blog posts.
-
AuthorPosts
- You must be logged in to reply to this topic.