admin管理员组

文章数量:1429243

I need help looping through a simple php code. What I'm trying to achieve is the following:

Start Loop
Look for all the posts
Add the author first & last name as a tag (links to /tag/firstName-lastName)
Once clicked on the tag, take me to the author page with all their posts 
end loop

I'm writing all this in single.php (I'm aware it only effects the page I open and doesn't effect all the blogs)

My Code so far;

<?php 
    $post_id = get_the_ID();
    $queried_post = get_post($post_id);
    $user_info = get_userdata($post->the_author);
    $first = $user_info->last_name; 
    wp_set_post_tags( $post_id, $first, true );     
?>

I need help looping through a simple php code. What I'm trying to achieve is the following:

Start Loop
Look for all the posts
Add the author first & last name as a tag (links to /tag/firstName-lastName)
Once clicked on the tag, take me to the author page with all their posts 
end loop

I'm writing all this in single.php (I'm aware it only effects the page I open and doesn't effect all the blogs)

My Code so far;

<?php 
    $post_id = get_the_ID();
    $queried_post = get_post($post_id);
    $user_info = get_userdata($post->the_author);
    $first = $user_info->last_name; 
    wp_set_post_tags( $post_id, $first, true );     
?>
Share Improve this question edited May 9, 2019 at 12:57 ROCKETS asked May 8, 2019 at 20:02 ROCKETSROCKETS 33 bronze badges 6
  • You use different slugs for the same tag, once you create slug from $first, once from $first and $last. You can not use variables wrapped with '. Look at recent comments – nmr Commented May 9, 2019 at 10:22
  • I looked at the recents comments and used their code but it only displays the blogs i've visited not all. Meaning, when the author is clicked, display all the blogs by the author. Which file should I editing in? – ROCKETS Commented May 9, 2019 at 13:30
  • When you click on link (tag with author name), you only see the visited posts because tag is create, because the attempt to create a tag is made while displaying the post. That's why I suggested creating the tag right after the post was created. Posts that have not been displayed by anyone yet do not have a tag set, so they are not visible on the tag page. – nmr Commented May 9, 2019 at 14:19
  • Is there a way i manually assign the author name to each and every article as a tag and link to all their posts? – ROCKETS Commented May 9, 2019 at 14:27
  • I do but it doesn't automatically assign it to every post. I would have to go through all the post and refresh it and see it. – ROCKETS Commented May 9, 2019 at 14:30
 |  Show 1 more comment

2 Answers 2

Reset to default 0
$args = array(
    'posts_per_page'   => -1,
    'post_type'        => 'post',
);
$the_query = new WP_Query( $args );

If you set the posts_per_page to -1 it will return all posts. Then you can loop through and do what you want with the single post.

while ( $the_query->have_posts() ) {
     echo get_the_author_meta( );//I have NOT used this so you should look into it.
}

Theses links will help you understand the attributes on the_post. https://codex.wordpress/Class_Reference/WP_Post https://developer.wordpress/reference/functions/get_permalink/

You have no 'loop' to loop through the data that is returned by the query.

Take a look at The Loop information in the Codex - lots of examples there (as well as 'the loop' info in the googles/bings/ducks). https://codex.wordpress/The_Loop

本文标签: functionshow to loop through blog posts in php