如何在 wp_query() 循环中显示我的 meta_key 值
How do I display my meta_key value in my wp_query() loop
我找到了很多使用元值过滤 post 的方法,但我似乎无法简单地显示它们。我创建了一个带有元字段的自定义 post 类型。我还通过特定的元值对我的 wp_query() 进行了过滤。但是我找不到在特定位置简单地显示元值的方法。
<?php
// WP_Query arguments
$args = array(
'p' => 'products',
'post_type' => array( 'products' ),
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'custom_product_position',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo the_title();
echo get_the_content();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
?>
我想在我的循环回声中添加(custom_product_position 的元值)我该怎么做? meta_value('custom_product_position') 不起作用。
这似乎是一个超级容易解决的问题,但我不知道如何搜索正确的东西。
我认为您只需要使用 get_post_meta
,这是文档的 link:
https://developer.wordpress.org/reference/functions/get_post_meta/
这是您的代码:
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// Check if the custom field has a value.
if ( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
我找到了很多使用元值过滤 post 的方法,但我似乎无法简单地显示它们。我创建了一个带有元字段的自定义 post 类型。我还通过特定的元值对我的 wp_query() 进行了过滤。但是我找不到在特定位置简单地显示元值的方法。
<?php
// WP_Query arguments
$args = array(
'p' => 'products',
'post_type' => array( 'products' ),
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_key' => 'custom_product_position',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo the_title();
echo get_the_content();
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
?>
我想在我的循环回声中添加(custom_product_position 的元值)我该怎么做? meta_value('custom_product_position') 不起作用。
这似乎是一个超级容易解决的问题,但我不知道如何搜索正确的东西。
我认为您只需要使用 get_post_meta
,这是文档的 link:
https://developer.wordpress.org/reference/functions/get_post_meta/
这是您的代码:
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// Check if the custom field has a value.
if ( ! empty( $key_1_value ) ) {
echo $key_1_value;
}