Do you want to display custom fields outside the loop in WordPress? Normally, custom fields are displayed inside the WordPress loop along with other post content and metadata. In this article, we will show you how to display custom fields outside the loop in WordPress.
是否要在WordPress循环外显示自定义字段? 通常,自定义字段会与其他帖子内容和元数据一起显示在WordPress循环内。 在本文中,我们将向您展示如何在WordPress循环外显示自定义字段。
什么是WordPress中的自定义字段 (What Are Custom Fields in WordPress)
Custom fields allow you to add additional meta data into your WordPress posts and then display them along with your post content.
自定义字段允许您将其他元数据添加到WordPress帖子中,然后将其与帖子内容一起显示。
You can add custom fields by simply enabling the custom fields metabox under the Screen Options. You can also create custom metaboxes in WordPress to give your custom fields a better user interface.
您只需添加“ 屏幕选项”下的“自定义字段”元框即可添加自定义字段。 您还可以在WordPress中创建自定义元框 ,以为自定义字段提供更好的用户界面。
For more details, see our beginner’s guide on using WordPress custom fields.
有关更多详细信息,请参阅有关使用WordPress自定义字段的初学者指南。
Since custom fields add metadata to posts, they can be easily displayed inside the WordPress loop along with other post content. However, sometimes you may want to display them outside the loop. For example, in a sidebar widget. This is when it becomes a bit tricky.
由于自定义字段将元数据添加到帖子中,因此可以轻松地将它们与其他帖子内容一起显示在WordPress循环内。 但是,有时您可能希望在循环外显示它们。 例如,在边栏小部件中。 这是有点棘手的时候。
That being said, let’s see how to easily display custom fields outside the loop in WordPress.
话虽这么说,让我们看看如何轻松地在WordPress循环外显示自定义字段。
在WordPress循环外显示自定义字段数据 (Display Custom Fields Data Outside The Loop in WordPress)
Instead of displaying custom fields meta data outside the loop, we’ll actually show you how to use multiple loops in your WordPress themes without affecting the main loop.
实际上,我们将向您展示如何在WordPress主题中使用多个循环而不影响主循环,而不是在循环外部显示自定义字段元数据。
This article requires you to add code to your WordPress theme files. If you haven’t done this before, then you may want to see our guide on how to copy and paste code in WordPress.
本文要求您将代码添加到WordPress主题文件中。 如果您以前没有做过,那么您可能想看一下我们有关如何在WordPress中复制和粘贴代码的指南。
You’ll need to add the following code to your theme files where you want to display the custom fields data in WordPress.
您需要在主题文件中添加以下代码,以在WordPress中显示自定义字段数据。
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>
This code simply loads up the global variable $wp_query to get the post ID. After that, it uses get_post_meta()
function to fetch and output your custom field data.
此代码只是加载全局变量$ wp_query以获得帖子ID。 之后,它使用get_post_meta()
函数来获取并输出您的自定义字段数据。
Don’t forget to change Your-Custom-Field with your actual custom field.
不要忘记用您的实际自定义字段更改您的自定义字段。
You can customize the code to match your needs. You can also use other query arguments to fetch and display custom fields data for different posts and pages.
您可以自定义代码以满足您的需求。 您还可以使用其他查询参数来获取和显示不同文章和页面的自定义字段数据。
Let’s take a look at another example. This one uses WP_Query class which is a much better and more flexible way to use multiple loops in your WordPress theme files.
让我们看另一个例子。 该类使用WP_Query类,这是在WordPress主题文件中使用多个循环的一种更好,更灵活的方法。
Simply add this code to your theme or child theme where you would like to show the custom field.
只需将此代码添加到您想要显示自定义字段的主题或子主题 。
$args = array (
// Post or Page ID
'p' => 231,
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_post_meta( get_the_ID(), 'Mood', true);
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo 'Nothing found';
}
Don’t forget to replace Mood with your own custom field name and post ID with your own post or page id.
不要忘记用您自己的自定义字段名称替换Mood,并用您自己的帖子或页面ID替换帖子ID。
That’s all for now.
目前为止就这样了。
We hope this article helped you learn how to display custom fields outside the loop in WordPress. You may also want to see our WordPress theme cheat sheet for beginners.
我们希望本文能帮助您学习如何在WordPress循环外显示自定义字段。 您可能还想看一下针对初学者的WordPress主题备忘单 。
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视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-custom-fields-outside-the-loop-in-wordpress/