- This topic has 19 replies, 3 voices, and was last updated 2 years, 6 months ago by David.
-
AuthorPosts
-
October 7, 2021 at 12:14 pm #1956363Chris
Hi,
Unfortunately the display settings won’t allow me to disable a custom element for a specific author. Could you please give me a workaround snippet that basically says:
If author is “ID number”, disable custom element “ID number, ID number, ID, number”
Thanks!!
October 7, 2021 at 12:23 pm #1956374LeoStaffCustomer SupportHi Chris,
Which element are you trying to disable?
Let me know 🙂
October 7, 2021 at 12:43 pm #1956393ChrisA custom one I created. A hook, a block-hook etc.
October 7, 2021 at 12:44 pm #1956395LeoStaffCustomer SupportI believe this filter is what you are looking for:
https://docs.generatepress.com/article/generate_element_display/Let me know 🙂
October 7, 2021 at 1:03 pm #1956403ChrisSo I would use the first one like this:
add_filter( 'generate_element_display', function( $display, $element_id ) { if ( 100 === $element_id && is_author( 'Tom' ) ) { $display = false; } return $display; }, 10, 2 );
Where I wouldn’t want to display element ID 100 for every post with the author Tom?
Could we change it to author ID instead of name?
This doesn’t work for me.
October 7, 2021 at 1:10 pm #1956409LeoStaffCustomer SupportWhere I wouldn’t want to display element ID 100 for every post with the author Tom?
That is correct – Element ID 100 won’t display when the author is Tom.
Could we change it to author ID instead of name?
This should help: https://codex.wordpress.org/Conditional_Tags#An_Author_Page
October 7, 2021 at 1:13 pm #1956412ChrisUnfortunately this doesn’t work for me.
Just to be sure, the element ID is what is displayed in the edit URL as “post=100”, correct?
October 7, 2021 at 1:19 pm #1956422LeoStaffCustomer SupportThat’s correct.
Can you share your full code here and link me to the page in question?
October 7, 2021 at 1:23 pm #1956428Chrisadd_filter( 'generate_element_display', function( $display, $element_id ) { if ( 1351 === $element_id && is_author( '2' ) ) { $display = false; } return $display; }, 10, 2 );
I can’t link the page, but it’s the most recent version for plugin and theme.
October 7, 2021 at 1:29 pm #1956435LeoStaffCustomer SupportIt’s not related to the theme/plugin versions.
Issues like these are tough to help without seeing the site live.
Can you create a staging site and provide the backend access?
Thanks!
October 7, 2021 at 1:41 pm #1956451ChrisI tried it with multiple installs now. The snippet doesn’t work in any of them.
October 7, 2021 at 2:46 pm #1956505LeoStaffCustomer SupportMaybe we are not using the right conditional tag.
Can you create a staging site and provide the backend access?
October 8, 2021 at 11:38 am #1957481ChrisNot really, but I can show you a snippet that works just fine:
add_filter( 'option_generate_blog_settings', 'custom_author_page_settings' ); function custom_author_page_settings( $options ) { $author = get_the_author_meta( 'ID' ); if ( 2 === $author ) { $options['author'] = false; $options['single_author'] = false; } return $options; }
So I don’t understand why the other one won’t work. Did you test the code yourself? With the new Block/Hook Elements for author meta for example?
October 9, 2021 at 2:11 am #1957806DavidStaffCustomer SupportHi there,
where is this element being displayed?
The
is_author
tag is used to check if you’re on an author archive page.Whereas your second working code is checking the author meta which will apply to a single post.
If you require the latter – try this:
add_filter( 'generate_element_display', function( $display, $element_id ) { $author = get_post_field( 'post_author', get_the_ID() ); if ( 1351 === $element_id && '2' === $author ) { $display = false; } return $display; }, 10, 2 );
October 9, 2021 at 7:37 am #1958042ChrisThe element is displayed on a post. Author box or post meta for example, or a hook in wp_head etc.
I want to disable an element on posts by a specific author.
Unfortunately the snippet you provided does not work either.
-
AuthorPosts
- You must be logged in to reply to this topic.