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
|
Show 1 more comment
2 Answers
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
$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