I want to return information about a blog. It could be, author name, title, date, country whatever just to test.
<?php
$post_id = get_the_ID();
while ($post_id != 0)
{
$queried_post = get_post($post_id);
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name',$author_id);
$post_id++;
}
?>
In the following code, I'm trying to get all the author's name between post_id 20 to 25. When the code is run, it only displays:
The author is:
The author is:
The author is:
The author is:
The author is:
The author is:
Can you tell me how i would fix it so that it would return any kind of information about the blog (in this case, the name of the author).
THE UPDATED CODE DOESN'T DISPLAY ANYTHING.
I want to return information about a blog. It could be, author name, title, date, country whatever just to test.
<?php
$post_id = get_the_ID();
while ($post_id != 0)
{
$queried_post = get_post($post_id);
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name',$author_id);
$post_id++;
}
?>
In the following code, I'm trying to get all the author's name between post_id 20 to 25. When the code is run, it only displays:
The author is:
The author is:
The author is:
The author is:
The author is:
The author is:
Can you tell me how i would fix it so that it would return any kind of information about the blog (in this case, the name of the author).
THE UPDATED CODE DOESN'T DISPLAY ANYTHING.
Share Improve this question edited May 9, 2019 at 19:03 J patel asked May 9, 2019 at 15:39 J patelJ patel 154 bronze badges 3 |2 Answers
Reset to default 1You need to re-arrange your code.
<?php
$post_id = 20;
while($post_id <= 25) {
$queried_post = get_post($post_id);
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name', $author_id);
$post_id++;
}
?>
Within while
loop above you have $queried_post
which is an object of class WP_Post
. Member Variables of WP_Post can be used to display data about each post.
I hope this may help.
UPDATE
what if i want to the loop to apply to all of the post that there are on the site. What would be the changes i would need to make in order for me to apply to all the posts?
In that case you may code like that using get_posts()
which returns an array of objects
<?php
$arg = array( 'numberposts' => -1 ); // get all posts
$queried_posts = get_posts( $arg);
// Now loop through $queried_posts
foreach( $queried_posts as $queried_post ) {
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name', $author_id);
}
?>
You will want to get the author name inside the loop. Once your inside the loop you will be dealing with one post at a time, then you can access any post info you need. https://codex.wordpress/Class_Reference/WP_Post
**Qaisar has it right for your while loop. Inside the loop you have to get the post. $queried_post = get_post($post_id); then you can get the author...
UPDATED AGAIN
<?php
$args = [
'post__in' => range( 20, 25 ), //**get post in your range**
];
$queried_posts= get_posts( $args ); // **now you have the post you want**
foreach( $queried_posts as $queried_post ) { // **loop through**
$author_id = $queried_post->post_author;
echo get_the_author_meta('display_name', $author_id) .'</br>';
}
?>
The author is: <?php the_author_meta( 'display_name', $author_id ); ?>
- do you have something like that in your code? Or maybeecho 'The author is: ' . get_the_author_meta( 'display_name', $author_id );
? And if you're in the standard Loop (while ( have_posts() ) ...
), you wouldn't need to specify the second parameter there (the$author_id
- i.e. the user ID). – Sally CJ Commented May 9, 2019 at 20:12