Or even the author id from the post id. I am trying to return the author meta (author page link and avatar) in the sidebar of a single post page (outside of the post loop). What is the best way to do this? I am using a custom function (see below) to return the post id, but am not sure what function to call next.
function this_post_id() {
global $wp_query;
$thePostID = $wp_query->post->ID;
return $thePostID;
}
I figured it out.
<?php $author_id=$post->post_author; ?>
<img src="<?php the_author_meta( 'avatar' , $author_id ); ?> " width="140" height="140" class="avatar" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />
<?php the_author_meta( 'user_nicename' , $author_id ); ?>
If you want it outside of loop then use the below code.
<?php
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'display_name' , $author_id );
echo $display_name;
?>
**use this code for display Auth Name**
<?php
$auth_id = $post->post_author;
echo get_the_author_meta( 'display_name', $auth_id );
?>
<?php
$field = 'display_name';
the_author_meta($field);
?>
Valid values for the $field parameter include:
admin_color
aim
comment_shortcuts
description
display_name
first_name
ID
jabber
last_name
nickname
plugins_last_view
plugins_per_page
rich_editing
syntax_highlighting
user_activation_key
user_description
user_email
user_firstname
user_lastname
user_level
user_login
user_nicename
user_pass
user_registered
user_status
user_url
yim
This should work
Post ID
$post_id = get_the_ID();
$author_id = get_post_field ('post_author', $post_id);
$display_name = get_the_author_meta( 'nickname' , $author_id );
echo $display_name;
Note: You need to use this function inside the loop.
Related
I have a custom post called 'project'.
I need to get 'titles' that logged in user wrote.
I tried the following code but it doesn't show any titles.
I'm a beginner... I looked into but I can't find which one has problems.
Would you please correct my code?
function output_projects_list() {
global $wpdb;
$custom_post_type = 'project'; // define your custom post type slug here
$current_user = get_userdata(get_current_user_id());
$current_user_name = $current_user->display_name;
// A sql query to return all the logged in users' post titles
$results = $wpdb->get_results( $wpdb->prepare( "
SELECT ID
, post_title
FROM {$wpdb->posts}
WHERE post_type = %s
, author = %s"
, and post_status = 'publish'
", $custom_post_type, $current_user_name ), ARRAY_A );
// Return null if we found no results
if ( ! $results )
return;
foreach( $results as $index => $post ) {
$output = $post['post_title'];
}
return $output;
}
echo output_projects_list();
Thank you.
I would use WP_Query instead, it's cleaner and easier to read. Take look at the following code:
function user_published_posts()
{
$query = new WP_Query(array(
"author" => get_current_user_id(),
"post_type" => "project",
"post_status" => "publish"
));
while ($query->have_posts()) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<span><?php the_author() ?></span>
<?php };
}
Let me know if it's what you're looking for!
i am using advanced custom filed and i made custom author field (it could be Publisher Or Brand etc) now this author's name is not printing on product (Book) page . in custom field its for author's name slug is 'names'
add_action( 'woocommerce_after_single_product_summary', "ACF_product_content", 10 );
function ACF_product_content(){
echo '<h2> ACF Content </h2>';
if (function_exists('the_field')){
echo '<p>Woohoo, the_field function exists! </p>';
//the_field('authors');
echo '<pre>';
print_r(get_the_ID());
echo '</pre>';
echo '<pre>';
print_r(get_field('authors'));
echo '</pre>';
die;
}
}
for this i got the result
Check this report screenshot
. now problem is to show the Authors name which is ['post_title'] in this array.
i tried so many solutions but not working its not showing the result.
i used to show this result by this code
echo the_field('names');
'names' is the field name in 'Authors' custom field.
try this code for ACF
<?php
echo get_field('your custom filed slug name',get_the_ID());
?>
fetch the post title
<?php echo get_the_title(); ?>
for display author name for below function
<?php echo get_the_author(); ?>
You may use one of the methods below . You have to strictly set $post otherwise get_the_ID() funtion return false.
global = $post;
$custom_key = 'author';
$post_id = get_the_ID();
echo $customer_key_value = get_post_meta( $post_id, $custom_key , true );
OR
global = $post;
$custom_key = 'author';
$post_id = get_the_ID();
$custom_values = get_post_custom_values( $custom_key , $post_id );
foreach ( $custom_values as $key => $value ) {
echo "$key => $value <br />";
}
In WordPress I need to fetch name of author who created post using author_id.
How can I find author_name ?
You can use get_the_author_meta(), to get author data.
echo get_the_author_meta('display_name', $author_id);
Hope this helps!
This should work like charm
<?php echo get_the_author(); ?>
For more detailed information.
https://codex.wordpress.org/Function_Reference/get_the_author
Use below code in single.php or the relevant page you want author name
<?php get_the_author_meta( 'display_name', $author_id ); ?>
When used in the WordPress Custom REST API endpoint you can do it like this:
function customrestapiplugin_getpost( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data[$i]['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['excerpt'] = $post[0]->post_excerpt;
$data['slug'] = $post[0]->post_name;
$data['date'] = $post[0]->post_date;
$data['link'] = get_permalink($post[0]->ID);
$data['author'] = get_the_author_meta('display_name', $post[0]->post_author);
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
Add This code in single-post.php
<?php echo get_the_author(); ?>
I hope this will work !!
For those who are looking for a seamless solution on how to get an author of a post in WordPress, this is another lightweight method.
global $wpdb;
$post_id = 12; // your post id
$post_author_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_id ) );
$author = new WP_User( $post_author_id );
$display_name = $author->display_name;
$avartar = get_avatar( $post_author_id, 30 ); // get_avatar( userid, size )
$author_url = get_author_posts_url( $post_author_id );
I need in the act of publishing a post, a value insert in a custom_field, the same post_title value!
function add_custom_field_automatically_two($post_ID) {
global $post;
$post_id = $post->ID;
global $wpdb;
//$my_customf = $post->post_title;
//$my_customf = get_the_title( $post_id );
$my_customf = "something string"; // worked, but I need post_title!
add_post_meta($post_ID, 'my_customf', $my_customf, true);
}
add_action('publish_page', 'add_custom_field_automatically_two');
add_action('publish_post', 'add_custom_field_automatically_two');
?>
Thanks.
I'm trying to get the_title(); from a wordpress page to use it in a php script
my code:
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'bin-o' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post); ?>
$project_name = the_title();
$post_id = get_page_id('$project_name');
var_dump($project_name);
?>
<a href="<?php echo get_site_url() . '/?p=' . $post_id ?>"><h1><?php the_title() ?></h1>
<?php the_content() ?></a>
The functions.php:
<?php
// Get the id of a page by its name
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
return $page_name;
}
?>
The problem is it only gives the_title() When it is printed.
so I can't use the_title() to use it for a php script cause the_title will return NULL
how can I fix this so I can use the requested title to use this further in the php script
You use get_the_title().
Many wordpress in-loop functions have corresponding get_ versions, which will return the value, instead of echoing it.
I've used this now:
<?php
$project_name = trim(ucfirst(get_the_title())); //The title of current post!
$project_info = get_page_by_title($project_name);
$project_id = $project_info->ID;
?>
<a href="<?php echo get_site_url() . '/?p=' . $project_id ?>"><h1><?php the_title() ?></h1>
<?php the_content() ?></a>
The project_info get's all the project info, project_id gets the ID out of the project info and uses that to redirect to the wanted page. so I dont have to use the functions anymore