admin管理员组文章数量:1431461
I'm trying to display the author name as a tag. I looked at many forums and it's referring to the get_author
methods and the PHP files from the template PHP. However, I do not know what changes to make in the get_author
method in order to return his name as a tag for a simple blog post.
I've looked at the following references:
I do not know which files to edit and where to put it as PHP is not my go to language. It would be really helpful if someone can guide me and help me return the author's name as a tag.
It should display something like:
Posted by author name (but it's a tag)
When I do this:
This post was written by <!--?php the_author(); ?-->
It doesn't display the authors name.
Right before my post in dashboard:
<p>The author of the post: <?php the_author(); ?></p>
After doing more research, it says this code must be put in the themes's index.php
however, I do not know where in that index.php
.
Reference:
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata">Posted in <?php the_category(', '); ?><?php the_author(); ?></p>
<?php get_header(); ?>
<div id="middle">
<div id="container">
<div id="ipilogue_logo"></div>
<!-- Display the Time. -->
<?php the_time('F j, Y'); ?><br/><br/>
<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- The following tests if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box will be given the CSS class "post". -->
<?php if ( in_category('3') ) { ?>
<div class="post-cat-three">
<?php } else { ?>
<div class="post">
<?php } ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the Time. -->
<small><?php the_time('F j, Y'); ?></small>
<!-- Display the Post's Content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<p>The author of the post: <?php the_author(); ?></p>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else: ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p>Sorry, no posts matched your criteria.</p>
<!-- REALLY stop The Loop. -->
<?php endif; ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
</div>
</div>
I'm trying to display the author name as a tag. I looked at many forums and it's referring to the get_author
methods and the PHP files from the template PHP. However, I do not know what changes to make in the get_author
method in order to return his name as a tag for a simple blog post.
I've looked at the following references:
- https://codex.wordpress/Template_Tags#Author_tags
- https://codex.wordpress/Function_Reference/the_author
I do not know which files to edit and where to put it as PHP is not my go to language. It would be really helpful if someone can guide me and help me return the author's name as a tag.
It should display something like:
Posted by author name (but it's a tag)
When I do this:
This post was written by <!--?php the_author(); ?-->
It doesn't display the authors name.
Right before my post in dashboard:
<p>The author of the post: <?php the_author(); ?></p>
After doing more research, it says this code must be put in the themes's index.php
however, I do not know where in that index.php
.
Reference:
- https://wp-kama/function/the_author
- https://codex.wordpress/The_Loop
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata">Posted in <?php the_category(', '); ?><?php the_author(); ?></p>
<?php get_header(); ?>
<div id="middle">
<div id="container">
<div id="ipilogue_logo"></div>
<!-- Display the Time. -->
<?php the_time('F j, Y'); ?><br/><br/>
<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- The following tests if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box will be given the CSS class "post". -->
<?php if ( in_category('3') ) { ?>
<div class="post-cat-three">
<?php } else { ?>
<div class="post">
<?php } ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the Time. -->
<small><?php the_time('F j, Y'); ?></small>
<!-- Display the Post's Content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<p>The author of the post: <?php the_author(); ?></p>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else: ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p>Sorry, no posts matched your criteria.</p>
<!-- REALLY stop The Loop. -->
<?php endif; ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
</div>
</div>
Share
Improve this question
edited Apr 16, 2019 at 1:00
Sven
3,6841 gold badge35 silver badges48 bronze badges
asked Apr 15, 2019 at 17:17
ehahaehaha
11 bronze badge
10
|
Show 5 more comments
1 Answer
Reset to default 0<!--?php the_author(); ?-->
is an HTML comment. Means it won't fire the PHP inside it.
As you already found out it should be <?php the_author(); ?>
when placed in an PHP template file inside a so called The Loop. Which you are already doing right.
Now there's WordPress' Template Hierarchy. Means you need to place that snippet in the one template that's actually rendering the content you are looking at in your browser. Taking the template hierarchy for a Single Post you can see the order WordPress is looking for existing template files in your theme.
A single post will be rendered via
single-{post-type}-{slug}.php
- or
single-{post-type}.php
if 1. not found. - or
single.php
if 1. and 2. not found. - or
singular.php
if 1., 2. and 3. not found. - or
index.php
as last resort.
Look for one of them, test-edit them until you found the right. Finally, there als may be a template-parts
subdirectory inside your theme to render specific content. And the template you are looking for may be referenced as template part and you may need to edit just this specific template part in the end.
本文标签: phpReturn author name as a tag using getauthor
版权声明:本文标题:php - Return author name as a tag using get_author 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745584821a2664836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<?php the_author(); ?>
– MikeNGarrett Commented Apr 15, 2019 at 17:38