您想了解如何在 WordPress 博客文章中显示文章元数据吗?帖子元数据是关于您的博客帖子的相关信息,例如发布日期、类别、作者姓名等。在本文中,我们将向您展示如何在 WordPress 帖子中显示帖子元数据。
您想了解如何在 WordPress 博客文章中显示文章元数据吗?帖子元数据是关于您的博客帖子的相关信息,例如发布日期、类别、作者姓名等。在本文中,我们将向您展示如何在 WordPress 帖子中显示帖子元数据。

什么是WordPress中的发布元数据? (What is Post Meta Data in WordPress?)
Post meta data is information about a post that is not part of the actual content. This includes information like post date, author, 类别和标签, or custom taxonomies.
帖子元数据是关于帖子的信息,不属于实际内容。 这包括发布日期,作者, 类别和标签或自定义分类法等信息。

Depending on your WordPress theme, this information can be displayed on different locations. For example, below post title, 发布内容后, in a sidebar column, and more.
根据您的WordPress主题,此信息可以显示在不同的位置。 例如,在帖子标题下方, 发布内容后 ,在侧栏栏中等等。
This information helps your users learn more about the content they are viewing. If used correctly, post meta data can 增加页面浏览量, improve user experience, and make your site look more professional.
此信息可帮助您的用户更多地了解他们正在查看的内容。 如果使用正确,发布元数据可以增加页面浏览量 ,改善用户体验,并使您的网站看起来更加专业。
不利的一面是,太多的元数据会让您的网站看起来很混乱。
缺点是太多的元数据会使您的网站看起来混乱。

Depending on your website, you can add or remove information and add 自定义 CSS 样式 to make it more useful.
根据您的网站,您可以添加或删除信息,以及添加自定义 CSS 样式以使其更有用。
让我们看看如何在 WordPress 中显示帖子元数据。
让我们看看如何在 WordPress 中显示帖子元数据。
Note: This article requires basic understanding of how WordPress themes work. You’ll also need to edit WordPress files. If you haven’t done this before, then take a look at our guide on 如何在 WordPress 中复制和粘贴代码.
注意:本文需要对WordPress主题的工作原理有基本的了解。 您还需要编辑WordPress文件。 如果您以前没有做过,请查看我们的指南, 了解如何在 WordPress 中复制和粘贴代码 。
WordPress主题如何显示帖子元数据? (How WordPress Themes Display Post Meta Data?)
有多种方法可以显示帖子元数据。有些主题在帖子标题下方有更简单的代码。
有多种方法可以显示帖子元数据。有些主题在帖子标题下方有更简单的代码。
By <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y'); ?> in <?php the_category(', '); ?> <?php edit_post_link(__('{Edit}'), ''); ?>
此代码仅显示作者姓名、发布日期和类别。
此代码仅显示作者姓名、标题日期和类别。
Other themes may define their own 模板标签, functions, and classes to display post meta data. These functions are then called in the theme files responsible for displaying posts.
其他主题可以定义自己的模板标签 ,功能和类以显示帖子元数据。 然后在负责显示帖子的主题文件中调用这些功能。
通常,您会在主题的index.php、single.php、archive.php 和内容模板中找到发布元数据代码。
通常,您会在主题的index.php、single.php、archive.php 和内容模板中找到帖子元数据代码。
You can 创建一个子主题 to override these theme files. If you are creating your own custom theme, then you can directly add or modify the code in your existing theme files.
您可以创建副主题以覆盖这些主题文件。 如果要创建自己的自定义主题,则可以直接在现有主题文件中添加或修改代码。
让我们看一下如何在 WordPress 中显示不同帖子元数据的一些示例。
让我们看一些如何在 WordPress 中显示不同帖子元数据的示例。
如何在WordPress中显示或隐藏发布日期 (How to Display or Hide Post Date in WordPress)
要显示帖子的发布日期,您需要将此代码添加到您的主题中。
要显示帖子的发布日期,您需要将此代码添加到您的主题中。
<p>This article was published on: <?php the_time('m/j/y g:i A') ?></p>
Notice the characters inside the_time
function. These are called format characters, and they tell PHP how to format the date and time. To learn more, see our article on 如何更改 WordPress 中的日期和时间格式.
注意the_time
函数中的字符。 这些称为格式字符,它们告诉PHP如何格式化日期和时间。 要了解更多信息,请参阅有关如何更改 WordPress 中的日期和时间格式文章。

Want to remove dates from your WordPress posts? You’ll need to locate the code with the_time
or the_date
functions in your theme files and delete those lines.
是否要从WordPress帖子中删除日期? 您需要在主题文件中使用the_time
或the_date
函数查找代码,然后删除这些行。
如何显示WordPress帖子的最后更新日期 (How to Display Last Update Date for WordPress Posts)
如果您经常更新网站上的旧文章,那么您可能需要显示帖子的最后更新日期。这有助于您的内容看起来新鲜,并吸引那些可能没有读过几年前发表的帖子的读者。
如果您经常更新网站上的旧文章,您可能需要显示帖子的上次更新日期。这有助于使您的内容看起来新鲜并吸引可能没有阅读过几年前发表的帖子的读者。
只需将以下代码添加到要显示上次更新日期的主题文件中:
只需将以下代码添加到要显示上次更新日期的主题文件中:
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
echo "<p>Last modified on ";
the_modified_time('F jS, Y');
echo " at ";
the_modified_time();
echo "</p> ";
}

For alternate methods and more detailed instructions, see our guide on how to 在 WordPress 中显示帖子的最后更新日期.
有关替代方法和更详细的说明,请参阅有关如何在 WordPress 中显示帖子的最后更新日期指南。
如何在WordPress中显示或隐藏作者姓名 (How to Show or Hide Author Name in WordPress)
To display 作者姓名, you need to add the following code to your theme files.
要显示作者姓名 ,您需要在主题文件中添加以下代码。
<p>This article was written by <?php the_author(); ?></p>
This code uses the_author
tag, which only displays author name.
此代码使用the_author
标记,该标记仅显示作者姓名。
You can also display author name linked to all posts written by that author. Simply replace the_author
tag with the the_author_posts_link
:
您还可以显示链接到该作者撰写的所有帖子的作者姓名。 只需将the_author
标记替换为the_author_posts_link
:
<p>View all articles by <?php the_author_posts_link(); ?></p>

如果您想从主题中删除作者姓名,则需要在主题文件中找到这些标签并将其删除。
如果您想从主题中删除作者姓名,则需要在主题文件中找到这些标签并将其删除。
如何在WordPress帖子中显示或隐藏类别 (How to Show or Hide Categories in WordPress Posts)
To display 类别, you need to add the following code to your theme files:
要显示类别 ,您需要在主题文件中添加以下代码:
<p>This post was filed under: <?php the_category(', ') ?></p>
此代码将显示以逗号分隔的帖子类别。您可以将逗号替换为要用作类别名称之间分隔符的任何字符。
此代码将显示以逗号分隔的帖子类别。您可以将逗号替换为要用作类别名称之间分隔符的任何字符。

显示帖子类别的另一种方法是一次显示一个类别。这使您可以更好地控制样式。
显示帖子类别的另一种方法是一次显示一个类别。这使您可以更好地控制样式。
例如,您可以将以下代码添加到 WordPress 主题文件中:
例如,您可以将以下代码添加到 WordPress 主题文件中:
<?php
$categories = get_the_category( $post->ID );
foreach ( $categories as $category ) {
echo '<span class="wpb-category"><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></span>';
}
?>
Now you can use wpb-category
class in your 自定义 CSS 样式类别 names.
现在,您可以在自定义CSS使用wpb-category
类设置类别名称的风格 。
Want to remove category names from WordPress posts? You’ll need to locate the line with the_category
tag in your theme files and delete it.
是否要从WordPress帖子中删除类别名称? 您需要在主题文件中找到带有the_category
标记的行并将其删除。
如何在WordPress帖子中显示或隐藏标签 (How to Show or Hide Tags in WordPress Posts)
要显示帖子标签,您需要将以下代码添加到主题文件中:
要显示帖子标签,您需要将以下代码添加到主题文件中:
<p>Tags: <?php the_tags(); ?></p>
此代码将仅显示与帖子关联的所有标签,并以逗号分隔。您可以将逗号替换为要用作分隔符的任何字符。
此代码将仅显示与帖子相关的所有标签,以逗号分隔。您可以将逗号替换为要用作分隔符的任何字符。
例如,以下代码将显示由斜杠分隔的标签。
例如,以下代码将显示由斜杠分隔的标签。
<?php the_tags( 'Tags: '/ ', ', '<br />' ); ?>
如您所见,the_tags 函数接受三个参数。
如您所见,the_tags 函数接受三个参数。
the_tags($before, $separator, $after)
您可以使用 before 和 after 参数添加您想要添加的任何文本或 HTML。这允许您添加 CSS 类,稍后您可以使用这些类在 WordPress 中设置标签样式。
您可以使用 before 和 after 参数添加任何您想要的文本或 HTML。这允许您添加稍后可用于在 WordPress 中设置标签样式的 CSS 类。
看一下下面的例子:
看下面的例子:
<?php the_tags('<div class="wpb-tags">Tags: ', ' ', '</div>');

If you don’t want to display tags before or after each post, then locate the line with the_tags()
code and delete it from your theme files.
如果您不想在每个帖子之前或之后显示标签,请找到带有the_tags()
代码的行并将其从主题文件中删除。
使用WordPress中的发布元数据做更多事情 (Doing More with Post Meta Data in WordPress)
到目前为止,我们向您展示了如何显示或隐藏基本的默认帖子元数据项。主题开发人员可以使用相同的基本模板标签以多种不同的方式显示帖子元数据。
到目前为止,我们已经向您展示了如何显示或隐藏基本的默认帖子元数据项。主题开发人员可以使用相同的基本模板标签以多种不同的方式显示帖子元数据。
For example, you can use post meta data to display 作者信息框, or 将发布日期替换为相对日期.
例如,您可以使用发布元数据显示作者信息框 ,或将发布日期替换为相对日期 。
Want to take it to the next level? Check out 自定义字段, which allow you to add your own meta data to WordPress posts. You can even create 自定义元框 to easily add those custom fields.
是否想将其提升到一个新的水平? 签出自定义字段 ,使您可以将自己的元数据添加到WordPress帖子中。 您甚至可以创建自定义元框来轻松添加这些自定义字段。
目前为止就这样了。
现在就这样。
We hope this article helped you learn how to display post meta data in WordPress. You may also want to see our list of the 最想要的 WordPress 提示、技巧和技巧.
我们希望本文能帮助您学习如何在WordPress中显示帖子元数据。 您可能还想查看我们最想要的 WordPress 提示、技巧和技巧列表 。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-post-meta-data-in-wordpress-themes/