学习wordpress开发时的未定义变量

Undefined variable when i was learning wordpress development

Hello Community 当我在学习wordpress时,我试图通过这行代码获得作者URL:

   <?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; ?>

显示此错误:

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作者不详,连我都抄袭了我的老师。 (Udemy 课程)。

您看到的错误告诉您变量 $post_author 未定义。这是在这一行:

<a href="<?php echo get_author_posts_url( $post -> $post_author ); ?>">

您实际想要使用的是 $post 变量中的 post_author 属性,它前面没有 $,因此您应该改为写 $post->post_author 并且应该 return post 作者 ID。

我建议看看 php class properties