'Undefined variable when i was learning wordpress development

Hello Community When I was learning wordpress I try to get author URL by this lines of codes:

   <?php if ( have_posts() ) : while( have_posts() ) : the_post(); ?>

    <div>  

        <article <?php post_class(); ?>>

            <h2> <a href=" <?php the_permalink(); ?> " > <?php the_title(); ?> </a> </h2>
    
            <?php the_content(); ?>

            <footer>

                <p class="byline">
                    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 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).



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jon Walter