我在名为game_offer的自定义帖子类型中添加了一个新的自定义字段。
现在,我有一个插件小部件,可以让我显示最新的自定义帖子类型。 在这个插件中,我希望能够将自定义字段放在循环中。
要显示自定义字段,我尝试了之前使用过的代码:
<?php the_field('game_offer'); ?>
当我查看页面时,我看到了这个错误:
Fatal error: Call to undefined function the_field()
我在循环中使用了以下代码:
if( $pq->have_posts() ) :
?>
<div class="flex">
<?php while($pq->have_posts()) : $pq->the_post(); ?>
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="menu_image" style="background: url('. $url.')">';
echo '</div>';
}
else {
echo '<div class="menu_image fallback_menus_image">';
echo '</div>';
}
?>
</div>
<div class="right">
<div class="container">
<div class="flex">
<div class="left">
<?php the_title(); ?>
<ul>
<li><?php echo the_field('game_offer'); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php wp_reset_query();
echo '</div>';
endwhile; ?>
<?php endif; ?>
我的整个插件在需要时看起来像这样:
function n2wp_latest_cpt_init() {
if ( !function_exists( 'register_sidebar_widget' ))
return;
function n2wp_latest_cpt($args) {
global $post;
extract($args);
// These are our own options
$options = get_option( 'n2wp_latest_cpt' );
$title = $options['title']; // Widget title
$phead = $options['phead']; // Heading format
$ptype = $options['ptype']; // Post type
$pshow = $options['pshow']; // Number of Tweets
$beforetitle = '';
$aftertitle = '';
// Output
echo $before_widget;
if ($title) echo $beforetitle . $title . $aftertitle;
$pq = new WP_Query(array( 'post_type' => $ptype, 'showposts' => $pshow ));
if( $pq->have_posts() ) :
?>
<div class="flex">
<?php while($pq->have_posts()) : $pq->the_post(); ?>
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="menu_image" style="background: url('. $url.')">';
echo '</div>';
}
else {
echo '<div class="menu_image fallback_menus_image">';
echo '</div>';
}
?>
</div>
<div class="right">
<div class="container">
<div class="flex">
<div class="left">
<?php the_title(); ?>
<ul>
<li><?php the_field('game_offer'); ?></li>
</ul>
</div>
</div>
</div>
</div>
<?php wp_reset_query();
echo '</div>';
endwhile; ?>
<?php endif; ?>
<!-- NEEDS FIX: to display link to full list of posts page
<?php $obj = get_post_type_object($ptype); ?>
<div class="latest_cpt_icon"><a href="<?php site_url('/'.$obj->query_var); ?>" rel="bookmark"><?php _e( 'View all ' . $obj->labels->name . ' posts' ); ?>→</a></div>
//-->
<?php
// echo widget closing tag
echo $after_widget;
}
/**
* Widget settings form function
*/
function n2wp_latest_cpt_control() {
// Get options
$options = get_option( 'n2wp_latest_cpt' );
// options exist? if not set defaults
if ( !is_array( $options ))
$options = array(
'title' => 'Latest Posts',
'phead' => 'h2',
'ptype' => 'post',
'pshow' => '5'
);
// form posted?
if ( $_POST['latest-cpt-submit'] ) {
$options['title'] = strip_tags( $_POST['latest-cpt-title'] );
$options['phead'] = $_POST['latest-cpt-phead'];
$options['ptype'] = $_POST['latest-cpt-ptype'];
$options['pshow'] = $_POST['latest-cpt-pshow'];
update_option( 'n2wp_latest_cpt', $options );
}
// Get options for form fields to show
$title = $options['title'];
$phead = $options['phead'];
$ptype = $options['ptype'];
$pshow = $options['pshow'];
// The widget form fields
?>
<label for="latest-cpt-title"><?php echo __( 'Widget Title' ); ?>
<input id="latest-cpt-title" type="text" name="latest-cpt-title" size="30" value="<?php echo $title; ?>" />
</label>
<label for="latest-cpt-phead"><?php echo __( 'Widget Heading Format' ); ?></label>
<select name="latest-cpt-phead"><option selected="selected" value="h2">H2 - <h2></h2></option><option selected="selected" value="h3">H3 - <h3></h3></option><option selected="selected" value="h4">H4 - <h4></h4></option><option selected="selected" value="strong">Bold - <strong></strong></option></select><select name="latest-cpt-ptype"><option value="">- <?php echo __( 'Select Post Type' ); ?> -</option></select><?php $args = array( 'public' => true );
$post_types = get_post_types( $args, 'names' );
foreach ($post_types as $post_type ) { ?>
<select name="latest-cpt-ptype"><option selected="selected" value="<?php echo $post_type; ?>"><?php echo $post_type;?></option></select><?php } ?>
<label for="latest-cpt-pshow"><?php echo __( 'Number of posts to show' ); ?>
<input id="latest-cpt-pshow" type="text" name="latest-cpt-pshow" size="2" value="<?php echo $pshow; ?>" />
</label>
<input id="latest-cpt-submit" type="hidden" name="latest-cpt-submit" value="1" />
<?php
}
wp_register_sidebar_widget( 'widget_latest_cpt', __('Latest Custom Posts'), 'n2wp_latest_cpt' );
wp_register_widget_control( 'widget_latest_cpt', __('Latest Custom Posts'), 'n2wp_latest_cpt_control', 300, 200 );
}
add_action( 'widgets_init', 'n2wp_latest_cpt_init' );
?>
答案 0 :(得分:1)
使用get_post_meta($ post-&gt; ID,'game_offer',true)在自定义帖子类型插件中显示自定义字段值;功能强>
<?php
if( $pq->have_posts() ) :
?>
<div class="flex">
<?php while($pq->have_posts()) : $pq->the_post(); ?>
<div class="left">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
if ( has_post_thumbnail() ) {
echo '<div class="menu_image" style="background: url('. $url.')">';
echo '</div>';
}
else {
echo '<div class="menu_image fallback_menus_image">';
echo '</div>';
}
?>
</div>
<div class="right">
<div class="container">
<div class="flex">
<div class="left">
<?php the_title(); ?>
<ul>
<li><?php
echo get_post_meta($post->ID, 'game_offer', true);
?></li>
</ul>
</div>
</div>
</div>
</div>
<?php wp_reset_query();
echo '</div>';
endwhile; ?>
<?php endif; ?>