I am new to wordpress and I tried to set up a custom post types for my blog using custom fields.
To do so, I used the Custom Post Types UI plugin: https://wordpress.org/plugins/custom-post-type-ui/ I created a new type "Movies" which supports custom fields. I have added a custom field named 'Release Date' in my post but I can't find a way to print it.
I display my posts using the following code:
<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>
But nothing gets printed. However if I removed the <h6><?php the_field('release_date')?></h6>
everything else works just fine.
What am I doing wrong here?
Try this: (Assuming you are not using ACF plugin.)
$release_date = get_post_meta($post->ID, 'release_date', true);
if( $release_date ) {
echo $release_date;
} else {
echo "Error getting release date";
}
check here how to get post meta field . you can use something like this
<?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自定义字段插件
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.