Archived topic
Adding "job title" to post meta data
7 replies · Started by Matthias on December 20, 2021
Hello Team,
first of all thank you for the amazing theme, which we use for a long time now and are extremely happy with.
I know, that my question is out of the scope of your normal duties, however, maybe you can still guide me in the right direction with some code?
We use "Simple Author Box Pro" to show information about the author at the end of each article. Example here:
https://demenz.lanaprinzip.com/vergesslichkeit-demenz/
As of Google EAT criteria, we would like to show at the beginning of the article not only the name of the author, but also the "Job Title" from the post author plugin.
I am talking abou the line under the post title: "Published 15. Dezember 20217. Changed Juli 2021 von Dr. Silvia Nold"
Basically behind the name should be the job title.
The author of SAB told me:
If you are comfortable editing your theme's code, you can retrieve the Job Title by:
$job_title = get_the_author_meta('sab_box_job_title', $author_id);
Hence my question, would you have a snippet code for me?
In CSS I use:
/* Display updated date */
.posted-on .updated {
display: inline-block;
margin-right: 1em;
}
/*Add date prefixes */
.posted-on .updated:before {
content: 'Aktualisiert: ';
}
.posted-on .entry-date:before {
content: 'Veröffentlicht: ';
}
Thank you very much,
Matthias
Hi there,
So you could try the following PHP Snippets to filter in your own post meta as explained in this doc:
https://docs.generatepress.com/article/generate_header_entry_meta_items/
For you requirement:
1. Register a new post Meta item:
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'job' === $item ) {
global $post;
$author_id = $post->post_author;
$job_title = get_the_author_meta( 'sab_box_job_title', $author_id );
echo $job_title;
}
} );
2. Then we append our new job meta item to the end of the post meta:
add_filter( 'generate_header_entry_meta_items', function() {
$items[] = 'job';
return $items;
} );
Hi David,
thank you for your time and response.
I added one Snippet with add_action and add_filter.
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'job' === $item ) {
global $post;
$author_id = $post->post_author;
$job_title = get_the_author_meta( 'sab_box_job_title', $author_id );
echo $job_title;
}
} );
add_filter( 'generate_header_entry_meta_items', function() {
$items[] = 'job';
return $items;
} );
In this case only the Users Title is shown, but the publish and change date, the author name is gone. Did I miss something?
Thank you,
Matthias
Hi Matthias,
Try this filter instead:
add_filter( 'generate_header_entry_meta_items', function() {
return array(
'date',
'author',
'job',
);
} );
Let me know :)
Hi Ying,
this one works perfectly fine! Thank you.
One last question:
Is there a possiblity to have the job displayed between brackets (Job Title)?
Thank you,
Matthias
Yes, try to change this PHP snippet:
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'job' === $item ) {
global $post;
$author_id = $post->post_author;
$job_title = get_the_author_meta( 'sab_box_job_title', $author_id );
echo $job_title;
}
} );
to
add_action( 'generate_post_meta_items', function( $item ) {
if ( 'job' === $item ) {
global $post;
$author_id = $post->post_author;
$job_title = get_the_author_meta( 'sab_box_job_title', $author_id );
echo '('.$job_title.')';
}
} );
Let me know :)
Hi Ying,
WONDERFUL! Thank you :) It works perfectly fine...
All the best,
Matthias
You are welcome Matthias :)