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:UsersApuOneDriveSiteswww.firststep.dev.ccwp-contentthemesweebablogindex.php on line 20 Call Stack #TimeMemoryFunctionLocation 10.0436481080{main}( )...index.php:0 20.0479483248require( 'C:UsersApuOneDriveSiteswww.firststep.dev.ccwp-blog-header.php' )...index.php:17 38.308825513240require_once( 'C:UsersApuOneDriveSiteswww.firststep.dev.ccwp-includestemplate-loader.php' )...wp-blog-header.php:19 48.340225637680include( 'C:UsersApuOneDriveSiteswww.firststep.dev.ccwp-contentthemesweebablogindex.php' )...template-loader.php:106 https://www.firststep.dev.cc/author/"> mridul | Date: Apr. 13, 2022 | Categories: Uncategorized | Tags:
帖子作者未定义,即使我抄袭了我的老师。(Udemy课程)。
您看到的错误是告诉您变量$post_author未定义。在这一行:
<a href="<?php echo get_author_posts_url( $post -> $post_author ); ?>">
你真正想要使用的是$post变量中的post_author属性,它前面没有$,所以你应该写$post->post_author
,这应该返回post作者ID。
我建议看一下php的类属性