[英]Using custom fields in Wordpress
我是wordpress的新手,我嘗試使用自定義字段為我的博客設置自定義帖子類型。
為此,我使用了“自定義帖子類型” UI插件: https : //wordpress.org/plugins/custom-post-type-ui/我創建了一個支持自定義字段的新類型“電影”。 我在帖子中添加了一個名為“發布日期”的自定義字段,但是找不到打印該字段的方法。
我使用以下代碼顯示帖子:
<ul>
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 3 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li class="news-item">
<h3><?php the_title()?></h3>
<h6><?php the_field('release_date')?></h6>
<p><?php the_content()?></p>
</li>
<?php
}
}
else {
echo 'no movies here!';
}
?>
</ul>
但是什么也沒印出來。 但是,如果我刪除了<h6><?php the_field('release_date')?></h6>
其他一切都很好。
我在這里做錯了什么?
嘗試以下操作:(假設您沒有使用ACF插件。)
$release_date = get_post_meta($post->ID, 'release_date', true);
if( $release_date ) {
echo $release_date;
} else {
echo "Error getting release date";
}
在這里檢查如何獲取發布元字段 。 你可以用這樣的東西
<?php
$key_1_value = get_post_meta( get_the_ID(), 'release_date', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
} else
{
echo "release date not present ";
}
?>
為了更好的開發,我建議使用Advance自定義字段插件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.