2013-05-28 91 views
2

我需要在WordPress中的single.php文件显示帖子的作者。该参考指示the_author();只在循环中起作用。显示作者姓名(WordPress的)

我一直在寻找其他的论坛并没有什么发现。

有什么想法?

谢谢。

编辑:

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

我不明白这一点。你仍然在'single.php'中利用循环,所以你仍然可以使用'the_author();'或'get_the_author();',etch。 – Ohgodwhy

回答

6

在你single.php,你极有可能the_post()通话。你会发现WordPress的template tags此行之后会工作得很好。换句话说,您可以在single.php中使用the_author

编辑:根据你在你的问题已经更新的代码,你需要把类似的东西在single.php顶部以下内容:

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

另外,如果你想若要在echo语句中使用作者姓名,请改为使用get_the_authorthe_author实际上已经为你回声了。

+0

我在主要问题编辑的代码和那里是一个循环,是the_author不single.php中,但在PHP文件。 – Apalabrados

+0

不可思议.....如果包括这样的行,它的工作原理! – Apalabrados

+1

太棒了。我很高兴能够提供帮助。 –

1

只要有一个$ 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>