Wordpress 如何在自定义帖子类型插件中显示自定义字段值

Wordpress 如何在自定义帖子类型插件中显示自定义字段值,wordpress,custom-post-type,custom-fields,Wordpress,Custom Post Type,Custom Fields,我在自定义帖子类型中添加了一个新的自定义字段,名为game\u offer 现在,我有了一个插件小部件,可以显示最新的自定义帖子类型。 在这个插件中,我希望能够将自定义字段也放入循环中 要显示自定义字段,我尝试了以前使用过的以下代码: <?php the_field('game_offer'); ?> 我在循环中使用了以下代码: if( $pq->have_posts() ) : ?> <div class="flex"> <

我在自定义帖子类型中添加了一个新的自定义字段,名为game\u offer

现在,我有了一个插件小部件,可以显示最新的自定义帖子类型。 在这个插件中,我希望能够将自定义字段也放入循环中

要显示自定义字段,我尝试了以前使用过的以下代码:

<?php the_field('game_offer'); ?>
我在循环中使用了以下代码:

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' ); ?>&rarr;</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' );

?>
使用get_post_meta$post->ID,“game_offer”,在自定义帖子类型插件中显示自定义字段值,true;作用


我仍然犯了同样的错误:致命错误:对未定义函数get_fieldget_field的调用来自ACF插件。您是否已安装并激活它。不,我没有安装。以前从未使用过该插件。我创建了自己的自定义分类法,并希望通过这个get\u post\u meta插件来显示它。是的,没错。我只需要在比赛前删除未得分,它100%有效。请编辑您的答案,以便我可以标记为最喜欢的。。感谢您的支持。
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' ); ?>&rarr;</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' );

?>
    <?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; ?>