如何在WordPress主题中显示博客文章元数据

Do you want to learn how to display post meta data in WordPress blog posts? Post meta data are relevant information about your blog post such as published date, category, author name, etc. In this article, we will show you how to display post meta data in WordPress posts.

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

Displaying post meta data in 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, categories and tags, or custom taxonomies.

帖子元数据是关于帖子的信息,不属于实际内容。 这包括发布日期,作者, 类别和标签或自定义分类法等信息。

Post meta data shown in a WordPress blog post

Depending on your WordPress theme, this information can be displayed on different locations. For example, below post title, after post content, 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 increase page views, improve user experience, and make your site look more professional.

此信息可帮助您的用户更多地了解他们正在查看的内容。 如果使用正确,发布元数据可以增加页面浏览量 ,改善用户体验,并使您的网站看起来更加专业。

On the downside, too much meta data can make your website look messy.

不利的一面是,太多的元数据会使您的网站看起来混乱。

Messy display of post meta data

Depending on your website, you can add or remove information and add custom css styles to make it more useful.

根据您的网站,您可以添加或删除信息,以及添加自定义CSS样式以使其更有用。

Let’s take a look at how to display post meta data in 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 how to copy and paste code in WordPress.

注意:本文需要对WordPress主题的工作原理有基本的了解。 您还需要编辑WordPress文件。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码

WordPress主题如何显示帖子元数据? (How WordPress Themes Display Post Meta Data?)

There are multiple ways to display post meta data. Some themes have simpler code located below the post title.

有多种显示帖子元数据的方法。 某些主题在帖子标题下方具有更简单的代码。


By <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y'); ?>  in <?php the_category(', '); ?> <?php edit_post_link(__('{Edit}'), ''); ?>

This code simply displays author’s name, post date, and category(s).

此代码仅显示作者的姓名,职务日期和类别。

Other themes may define their own template tags, functions, and classes to display post meta data. These functions are then called in the theme files responsible for displaying posts.

其他主题可以定义自己的模板标签 ,功能和类以显示帖子元数据。 然后在负责显示帖子的主题文件中调用这些功能。

Usually, you will find post meta data code in your theme’s index.php, single.php, archive.php, and content templates.

通常,您会在主题的index.php,single.php,archive.php和内容模板中找到发布元数据代码。

You can create a child theme 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.

您可以创建子主题以覆盖这些主题文件。 如果要创建自己的自定义主题,则可以直接在现有主题文件中添加或修改代码。

Let’s take a look at some examples of how to display different post meta data in WordPress.

让我们看一些如何在WordPress中显示不同的帖子元数据的示例。

如何在WordPress中显示或隐藏发布日期 (How to Display or Hide Post Date in WordPress)

To display the publish date of a post, you need to add this code to your theme.

要显示帖子的发布日期,您需要将此代码添加到主题中。


<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 how to change date and time format in WordPress.

注意the_time函数中的字符。 这些称为格式字符,它们告诉PHP如何格式化日期和时间。 要了解更多信息,请参阅有关如何在WordPress中更改日期和时间格式的文章。

Showing publish date for WordPress posts

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_timethe_date函数查找代码,然后删除这些行。

如何显示WordPress帖子的最后更新日期 (How to Display Last Update Date for WordPress Posts)

If you frequently update old articles on your website, then you may want to display the last updated date of your posts. This helps your content look fresh and attract readers who who may not read a post that was published years ago.

如果您经常更新网站上的旧文章,则可能需要显示帖子的最新更新日期。 这有助于使您的内容看起来新鲜,并吸引可能不会阅读几年前发布的帖子的读者。

Simply add the following code to your theme files where you want to display the last updated date:

只需将以下代码添加到您想要显示最近更新日期的主题文件中:


$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> "; 
} 

Showing last updated date for a WordPress post

For alternate methods and more detailed instructions, see our guide on how to display the last update date of your posts in WordPress.

有关替代方法和更详细的说明,请参阅有关如何在WordPress中显示帖子的最后更新日期的指南。

如何在WordPress中显示或隐藏作者姓名 (How to Show or Hide Author Name in WordPress)

To display author name, 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>

Showing author link in WordPress posts

If you want to remove the author’s name from your theme, then you will need to locate these tags in your theme files and delete them.

如果要从主题中删除作者的姓名,则需要在主题文件中找到这些标签并将其删除。

如何在WordPress帖子中显示或隐藏类别 (How to Show or Hide Categories in WordPress Posts)

To display categories, you need to add the following code to your theme files:

要显示类别 ,您需要在主题文件中添加以下代码:


<p>This post was filed under: <?php the_category(', ') ?></p>

This code will display post categories separated by a comma. You can replace the comma with any character you want to use as a separator between category names.

此代码将显示用逗号分隔的帖子类别。 您可以将逗号替换为要用作类别名称之间的分隔符的任何字符。

Showing post categories

Another way to display post categories is by displaying one category at a time. This gives you more control over styling.

显示帖子类别的另一种方法是一次显示一个类别。 这使您可以更好地控制样式。

For example, you can add the following code to your WordPress theme files:

例如,您可以将以下代码添加到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 custom CSS to style category 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)

To display post tags, you need to add the following code to your theme files:

要显示帖子标签,您需要在主题文件中添加以下代码:


<p>Tags: <?php the_tags(); ?></p>

This code will simply show all tags associated with the post separated by a comma. You can replace the comma with any character you want to use as a separator.

此代码将仅显示与帖子相关的所有标签,并用逗号分隔。 您可以将逗号替换为要用作分隔符的任何字符。

For example, the following code will show tags separated by a slash.

例如,以下代码将显示用斜杠分隔的标签。

 <?php the_tags( 'Tags: '/ ', ', '<br />' ); ?>

As you can see, the_tags function accepts three parameters.

如您所见,the_tags函数接受三个参数。


the_tags($before, $separator, $after)

You can use the before and after parameter to add any text or HTML you want to add. This allows you to add CSS classes, which you can later use to style tags in WordPress.

您可以使用before和after参数添加要添加的任何文本或HTML。 这使您可以添加CSS类,以后可用于在WordPress中设置标签样式。

Take a look at the following example:

看下面的例子:


<?php the_tags('<div class="wpb-tags">Tags: ', '  ', '</div>');

Showing post tags

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)

So far we showed you how to display or hide the basic default post meta data items. Theme developers can use the same basic template tags to display post meta data in many different ways.

到目前为止,我们已经向您展示了如何显示或隐藏基本的默认post meta数据项。 主题开发人员可以使用相同的基本模板标签以多种不同方式显示帖子元数据。

For example, you can use post meta data to display author info box, or replace post date with relative dates.

例如,您可以使用发布元数据显示作者信息框 ,或将发布日期替换为相对日期

Want to take it to the next level? Check out custom fields, which allow you to add your own meta data to WordPress posts. You can even create custom meta boxes to easily add those custom fields.

是否想将其提升到一个新的水平? 签出自定义字段 ,使您可以将自己的元数据添加到WordPress帖子中。 您甚至可以创建自定义元框来轻松添加这些自定义字段。

That’s all for now.

目前为止就这样了。

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 most wanted WordPress tips, tricks, and hacks.

我们希望本文能帮助您学习如何在WordPress中显示帖子元数据。 您可能还想查看我们最想要的WordPress技巧,窍门和hack列表

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-post-meta-data-in-wordpress-themes/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值