Hello Community When i was learning wordpress I try to get author URL by this lines of codes:
`
<div>
<article <?php post_class(); ?>>
<h2> <a href=" <?php the_permalink(); ?> " > <?php the_title(); ?> </a> </h2>
<?php the_content(); ?>
<footer>
<p >
Author:
<a href="<?php echo get_author_posts_url( $post -> $post_author ); ?>">
<?php the_author(); ?>
</a>
| Date: <?php the_time( 'M. d, Y' ); ?> |
Categories: <?php the_category( ', ' ); ?> |
Tags: <?php the_tags( '',', ','' ); ?>
</p>
</footer>
</article>
</div>
<?php endwhile; else: ?>
<?php _e("Sorry no content found in your website please add some post to see Designs" , "phpforwp"); ?>
<?php endif; ?>`
Showing this error: Author: ( ! ) Notice: Undefined variable: post_author in C:\Users\Apu\OneDrive\Sites\www.firststep.dev.cc\wp-content\themes\weebablog\index.php on line 20 Call Stack #TimeMemoryFunctionLocation 10.0436481080{main}( )...\index.php:0 20.0479483248require( 'C:\Users\Apu\OneDrive\Sites\www.firststep.dev.cc\wp-blog-header.php' )...\index.php:17 38.308825513240require_once( 'C:\Users\Apu\OneDrive\Sites\www.firststep.dev.cc\wp-includes\template-loader.php' )...\wp-blog-header.php:19 48.340225637680include( 'C:\Users\Apu\OneDrive\Sites\www.firststep.dev.cc\wp-content\themes\weebablog\index.php' )...\template-loader.php:106 " rel="nofollow noreferrer">https://www.firststep.dev.cc/author/"> mridul | Date: Apr. 13, 2022 | Categories: Uncategorized | Tags:
Post author is undefined, Even I have copied my teacher. (Udemy course).
CodePudding user response:
try to use echo get_the_author();
CodePudding user response:
The error you're seeing is telling you that the variable $post_author is undefined. This is on this line:
<a href="<?php echo get_author_posts_url( $post -> $post_author ); ?>">
What you actually want to be using is the post_author property within the $post variable, which doesn't have a $ in front of it, so you should instead write $post->post_author
and that should return the post author ID.
I'd suggest taking a look at php class properties