最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - use loop to return blog details

programmeradmin1浏览0评论

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
  • The author is: <?php the_author_meta( 'display_name', $author_id ); ?> - do you have something like that in your code? Or maybe echo '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
  • @SallyCJ how would i return $post_id using that code? $author_id = $queried_post->post_id; Did that but nothing. – – J patel Commented May 10, 2019 at 17:21
  • I'm sure you already have the answer by now, @Jpatel. :) – Sally CJ Commented May 10, 2019 at 19:15
Add a comment  | 

2 Answers 2

Reset to default 1

You 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>';
    }
?>
发布评论

评论列表(0)

  1. 暂无评论