wordpress 如何显示来自自定义帖子类型的自定义数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8015217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display custom data from custom post types
提问by Prusprus
I've created a custom post type. It will load just fine in the Wordpress dashboard and I will be able to save it aswell. Now let's say it's a custom post type that contains data for a few strings and a few dates.
我创建了一个自定义帖子类型。它将在 Wordpress 仪表板中加载得很好,我也可以保存它。现在假设它是一个自定义帖子类型,其中包含一些字符串和一些日期的数据。
I want to be able to retrieve these custom post types (which i've done using WP_Query and specifying the post_type to the name of my custom post type). When i call print_r on the returned object, nowhere in the object is the custom data (strings and dates) stored. How would i retrieve these from the database?
我希望能够检索这些自定义帖子类型(我已经使用 WP_Query 完成并将 post_type 指定为我的自定义帖子类型的名称)。当我在返回的对象上调用 print_r 时,对象中的任何地方都没有存储自定义数据(字符串和日期)。我将如何从数据库中检索这些?
I've looked around for hours and haven't found any approach to retrieving this data.
我已经环顾了几个小时,但没有找到任何检索这些数据的方法。
As requested: This is how the data is stored:
根据要求:这是数据的存储方式:
function update_obituary(){
global $post;
update_post_meta($post->ID, "first_name", $_POST["first_name"]);
update_post_meta($post->ID, "last_name", $_POST["last_name"]);
update_post_meta($post->ID, "birth_date", $_POST["birth_date"]);
update_post_meta($post->ID, "death_date", $_POST["death_date"]);
update_post_meta($post->ID, "publication_date", $_POST["publication_date"]);
}
This function is tied to the 'save_post' hook. The data will be redisplayed when i reopen the custom post type instance in edit mode. That means that it's stored in the database, right?
此函数与“save_post”挂钩。当我在编辑模式下重新打开自定义帖子类型实例时,数据将重新显示。这意味着它存储在数据库中,对吗?
回答by Johannes Pille
If the metadata shows up when editing posts of the type, then yes, it must have been successfully stored in the DB.
如果元数据在编辑该类型的帖子时出现,那么是的,它必须已成功存储在数据库中。
There's two wp functions to retrieve the custom post type's metadata: get_post_custom_values
and get_post_meta
. The difference being, that get_post_custom_values
can access non-unique custom fields, i.e. those with more than one value associated with a single key. You may choose to use it for unique fields also though - question of taste.
有两个 wp 函数可以检索自定义帖子类型的元数据:get_post_custom_values
和get_post_meta
. 不同之处在于,它get_post_custom_values
可以访问非唯一的自定义字段,即具有多个与单个键关联的值的字段。您也可以选择将它用于独特的领域 - 品味问题。
Assuming, that your post type is called "obituary":
假设您的帖子类型称为“讣告”:
// First lets set some arguments for the query:
// Optionally, those could of course go directly into the query,
// especially, if you have no others but post type.
$args = array(
'post_type' => 'obituary',
'posts_per_page' => 5
// Several more arguments could go here. Last one without a comma.
);
// Query the posts:
$obituary_query = new WP_Query($args);
// Loop through the obituaries:
while ($obituary_query->have_posts()) : $obituary_query->the_post();
// Echo some markup
echo '<p>';
// As with regular posts, you can use all normal display functions, such as
the_title();
// Within the loop, you can access custom fields like so:
echo get_post_meta($post->ID, 'birth_date', true);
// Or like so:
$birth_date = get_post_custom_values('birth_date');
echo $birth_date[0];
echo '</p>'; // Markup closing tags.
endwhile;
// Reset Post Data
wp_reset_postdata();
A word of caution, to avoid confusion:
Leaving out the boolean in get_post_meta
will make it return an array rather than a string. get_post_custom_values
always returns an array, which is why, in the above example, we're echoing the $birth_date[0]
, rather than $birth_date
.
提醒一句,以避免混淆:省略布尔值 inget_post_meta
将使其返回一个数组而不是一个字符串。get_post_custom_values
总是返回一个数组,这就是为什么在上面的例子中,我们回显$birth_date[0]
, 而不是$birth_date
。
Also I'm not 100% certain at the moment, whether $post->ID
will work as expected in the above. If not, replace it with get_the_ID()
. Both should work, one will for sure. Could test that, but saving myself the time...
此外,我目前还不能 100% 确定,是否$post->ID
会按上述预期工作。如果没有,请将其替换为get_the_ID()
。两者都应该有效,一个肯定会。可以测试,但节省自己的时间......
For the sake of completeness, check the codex on WP_Query
for more query arguments and correct usage.
为了完整起见,请检查代码以WP_Query
获取更多查询参数和正确用法。