2

我需要在 Wordpress 的 single.php 文件中显示帖子的作者。该参考表明 the_author(); 仅在循环内有效。

我一直在寻找其他论坛,但没有找到。

任何想法?

谢谢。

编辑:

    <div class="bar_info">
         <?php echo "By: ".the_author(); ?>
         <?php
                foreach((get_the_category()) as $category) { 
                    echo category->cat_name.', ';
                }
         ?>
    </div>
4

3 回答 3

9

在你的single.php,你很可能有一个电话the_post()。你会发现WordPress 模板标签在这一行之后就可以正常工作了。换句话说,您可以使用the_authorin single.php

编辑:根据您在问题中更新的代码,您需要在顶部添加以下内容single.php

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

此外,如果您想在echo声明中使用作者姓名,请get_the_author改用。the_author实际上已经为你呼应了。

于 2013-05-28T17:23:36.070 回答
2

只要有一个 $post 对象,从技术上讲,您就处于“循环中”——即使查询对象中只存在一个帖子,在 single.php 中就是这种情况。只要你执行了 the_post(); 模板标签是可访问的,所以the_author(); 会工作得很好。如果你想指向作者档案the_author_posts_link(); 将输出指向适当存档的链接以及锚文本中的作者姓名。

更新:

你的代码也是错误的。the_author 回显作者姓名, get_the_author() 会将其视为变量。这会起作用:

    <?php the_post(); ?>
    <div class="bar_info">
         By: <?php the_author(); ?>
         <?php
                foreach((get_the_category()) as $category) { 
                    echo category->cat_name.', ';
                }
         ?>
    </div>

或者,这也可以:

     <?php the_post(); ?>      
     <div class="bar_info">
         echo "By: " . get_the_author();
                foreach((get_the_category()) as $category) { 
                    echo category->cat_name.', ';
                }
         ?>
    </div>
于 2013-05-28T17:45:33.030 回答
0
  • 在 post 循环内的 single.php 中使用the_author
  • 您可以使用echo get_the_author 代替 the_author。
  • 如果您需要作者姓名和存档链接,您可以使用the_author_posts_link 。
于 2019-05-15T16:07:59.847 回答