如何在WordPress循环外显示自定义字段

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循环外显示自定义字段。

How to display custom fields outside the WordPress loop
什么是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中创建自定义元框 ,以为自定义字段提供更好的用户界面。

Adding custom field to a WordPress post or page

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。

Custom field in WordPress sidebar widget

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视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-custom-fields-outside-the-loop-in-wordpress/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些创建类似Advanced Custom Fields的WordPress插件的指导。 首先,您需要了解WordPress插件的基本结构和如何创建它们。可以从WordPress官方网站上找到相关的文档和教程。 接下来,您需要决定插件的功能和设计。插件的主要功能是允许用户在后台创建和管理自定义字段。这意味着您需要创建一个界面,使用户能够创建新的自定义字段并指定它们的名称、类型和其他属性。 对于自定义字段的类型,您需要考虑哪些类型最适合您的插件。例如,如果您的插件是为博客文章创建的,您可能需要支持文本、日期、图片、文件等字段类型。如果您的插件是为电子商务网站创建的,您可能需要支持价格、SKU等字段类型。 创建自定义字段时,您需要考虑如何在WordPress主题中使用这些字段。这可以通过使用WordPress的内置函数来实现。例如,如果您创建了一个自定义字段来存储产品价格,您可以使用get_post_meta()函数来获取价格并在主题中显示它。 最后,您需要测试您的插件以确保它能够按预期工作。您可以在本地安装WordPress并使用它来测试插件。您还可以使用在线测试环境来测试插件的兼容性和性能。 希望这些指导能够帮助您创建一个类似Advanced Custom Fields的WordPress插件。如果您需要更多的帮助或指导,请随时提出问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值