- This topic has 9 replies, 3 voices, and was last updated 1 year, 3 months ago by David.
-
AuthorPosts
-
January 26, 2023 at 8:29 am #2510019Saul
I am trying to create a Post Meta Section inline to this reference https://prnt.sc/K0q7lVS-Q8_p where I need to display Post Author and Medically reviewed by Author (Fact checked by author in my case). The fact checked by author will be a person from WordPress users list. To accomplish this I have already created a custom field, type “User” https://prnt.sc/s1HiUsHiUr77 which shows a dropdown (https://prnt.sc/nx_eUkeCh8p0) though which I can set any WP User as the Second Author/Fact Checker. I have used Post Meta Template to render this inside WP Posts. However, there are two issues:
1. The template is not Fact Checked by Author field value: https://prnt.sc/hl40O-XmvC5c and instead it’s showing the Author Name it’s showing Author ID.
2. I want the Post Meta to appear after Post’s H1 tag but there is no hook to render it after H1 tag. The Post Template using WordPress’s default template. All posts in WP were actually HTML Pages in our old website which we migrated to WP using a custom PHP script.Please refer to this screenshot: https://prnt.sc/gngqdj-Pk921
January 26, 2023 at 9:40 am #2510086DavidStaffCustomer SupportHi there,
Dealing with #2 first – the issue: that post has the H1 title printed in some custom HTML ( i assume from the old theme ) – see here:
Thats not from GPs templates so there are no hooks there, as its part of the post content….
Not sure if you can correct that ?For #1 …. try changing the ACF fields return value to … i thinks its Object… not sure.
January 26, 2023 at 9:55 am #2510109Saul#2. You are right the custom HTML is coming from old HTML. All posts in WP were actually HTML Pages in our old HTML based website which we migrated to WP using a custom PHP script.
#1. I have tried all ACF Return Formats: User Array, User Object and User ID but no luck!January 26, 2023 at 11:34 am #2510219YingStaffCustomer SupportHi Saul,
Can you set the return value to ID?
Then add an additional CSS class to the headline, eg.
fact-check-by-class
.
https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/Then we can use this PHP to pull the user data:
add_filter( 'render_block', function( $block_content, $block ) { $user_info = get_userdata(get_post_meta( get_the_ID(), 'fact_check_by', true)); $username = $user_info->display_name; if ( ( ! empty( $block['attrs']['className'] ) && 'fact-check-by-class' === $block['attrs']['className'] )) { $block_content = '<span>'.$username.'</span>'; } return $block_content; }, 10, 2 );
For more info about
get_userdata
: https://developer.wordpress.org/reference/functions/get_userdata/January 26, 2023 at 2:04 pm #2510331SaulIt doesn’t seems to show the name for the user! I added a class “fact-checked-by-class” and replaced the ‘checked_by’ from your example with my custom field name “fact_checked_by” which pulls the user details from the Post meta.
January 26, 2023 at 2:26 pm #2510363YingStaffCustomer SupportCan you share the exact code you are using right now?
January 26, 2023 at 3:21 pm #2510408SaulI made an update in the code and it’s fixed… Thanks a lot!
January 26, 2023 at 3:27 pm #2510411YingStaffCustomer SupportGlad to hear that 🙂
January 27, 2023 at 8:59 pm #2511674SaulRelated to the same question… I wanted to extend the functionality and show a Popup with the respective author bio on the hover. I created two Popups following this this video: https://theadminbar.com/dynamic-author-bio-popup/ but the second popup is not pulling the right data may be because it’s not switching the user dynamically. I added “fact-checked-by-class” to the Name field (https://prnt.sc/cTEZzPecqg2P) and it pulled the right name but the rest of the info belongs to the current user. Not the second user (fact checker). Also, the URL to the correct profile page is missing. Here is the screenshot: https://prnt.sc/VqVUHvVDq6vW I think we need to extend the filter to include the URL to the Author Profile and fetch the other info and then we can add “fact-checked-by-class” to other headings.
January 28, 2023 at 3:50 am #2511835DavidStaffCustomer SupportThe dynamic data source is the current post eg. current post, current post meta, current post author meta etc.
But the fact checked by authors data doesn’t get stored in current posts meta its in that users data.So to get the data from the fact checker you need to use Yings method for each piece of data.
You can rinse and repeat her snippet with the updated user_data and block class name.
I revised the code to add 2 variable at the top where you can set the user_data property and the blocks CSS class:// Get user_data value for fact_checker add_filter( 'render_block', function( $block_content, $block ) { // load your userdata property and class name $userdata = 'some_property'; // eg. display_name $target_class = 'the-class-name'; // load ID of the fact_check_by author in $user_info $user_info = get_userdata(get_post_meta( get_the_ID(), 'fact_check_by', true)); // get the value of $userdata value from $user_info $display_value = $user_info->$userdata; // if block has CSS attribute of $target_class if ( ( ! empty( $block['attrs']['className'] ) && $target_class === $block['attrs']['className'] )) { // replace the block content with $display_value $block_content = '<span>' . $display_value . '</span>'; } return $block_content; }, 10, 2 );
If the data you want is stored in a Custom Field then you would do something like this instead:
// Get custom filed value for fact_checker add_filter( 'render_block', function( $block_content, $block ) { // load your custom field and class name $field_name = 'the_custom_field'; $target_class = 'the-class-name'; // load ID of the fact_check_by author in $user_info $user_info = get_userdata(get_post_meta( get_the_ID(), 'fact_check_by', true)); // get the value of $field_name value from $user_info $display_value = get_field( $field_name, 'user_'. $user_info ); // if block has CSS attribute of $target_class if ( ( ! empty( $block['attrs']['className'] ) && $target_class === $block['attrs']['className'] )) { // replace the block content with $display_value $block_content = '<span>' . $display_value . '</span>'; } return $block_content; }, 10, 2 );
-
AuthorPosts
- You must be logged in to reply to this topic.