未显示ACF简单文本字段值

时间:2018-06-28 作者:Nancy

我在函数中注册了自定义帖子类型。php类似:

function ourproducts_post_type() {

   /*Labels*/
  $labels = array(
    \'name\' => _x("Our Products", "post type general name"),
    \'singular_name\' => _x("Our Products", "post type singular name"),
    \'menu_name\' => \'Our Products\',
    \'add_new\' => _x("Add New", "ourproducts item"),
    \'add_new_item\' => __("Add New Product Category"),
    \'edit_item\' => __("Edit Product category"),
    \'new_item\' => __("New Product Category"),
    \'view_item\' => __("View Product Category"),
    \'search_items\' => __("Search Product categories"),
    \'not_found\' =>  __("No Product categories Found"),
    \'not_found_in_trash\' => __("No Product Categories Found in Trash"),
    \'parent_item_colon\' => \'\'
  );

  /*Register ourproducts post type*/
  register_post_type(\'ourproducts\' , array(
    \'labels\' => $labels,
    \'public\' => true,
    \'orderby\' => \'menu_order\', 
    \'has_archive\' => false,
    \'menu_icon\' => \'dashicons-groups\',
    \'rewrite\' => false,
    \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'page-attributes\'),
    \'show_ui\'             => true, 
    \'show_in_menu\'        => \'custom-options\',
    //\'menu_position\'       => 40,

  ) );
}
add_action( \'init\', \'ourproducts_post_type\', 0 );
在前端(打开front-page.php 模板页),我尝试在循环外部显示字段值,如下所示:

    <h3><?php the_field(\'nasi_proizvodi\', $post->ID); ?></h3>
此外,我在循环内部进行了如下尝试:

<?php while ( have_posts() ) : the_post(); ?>

            <h3><?php the_field(\'nasi_proizvodi\'); ?></h3>

<?php endwhile;  ?>
但什么都没有显示。除非我特别这样写帖子id:

<?php while ( have_posts() ) : the_post(); ?>

            <h3><?php the_field(\'nasi_proizvodi\', 4268); ?></h3>

<?php endwhile;
但我需要动态更改它们,不仅仅是针对这个特定的帖子id。有趣的是,我已经在首页上创建了几个文本字段(我正在开发一个主题),并且动态显示这些字段没有问题。请问我做错了什么,有什么建议吗?

1 个回复
最合适的回答,由SO网友:Dharmishtha Patel 整理而成

请检查您的PostType并添加参数

<?php 
$query = new WP_Query( array( \'post_type\' => \'ourproducts\' ) );
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : 
        $query->the_post(); 
?>
        <h3><?php echo get_field(\'nasi_proizvodi\'); ?></h3>
<?php 
    endwhile; 
    wp_reset_postdata(); 
endif; 
?>

结束

相关推荐

未显示ACF简单文本字段值 - 小码农CODE - 行之有效找到问题解决它

未显示ACF简单文本字段值

时间:2018-06-28 作者:Nancy

我在函数中注册了自定义帖子类型。php类似:

function ourproducts_post_type() {

   /*Labels*/
  $labels = array(
    \'name\' => _x("Our Products", "post type general name"),
    \'singular_name\' => _x("Our Products", "post type singular name"),
    \'menu_name\' => \'Our Products\',
    \'add_new\' => _x("Add New", "ourproducts item"),
    \'add_new_item\' => __("Add New Product Category"),
    \'edit_item\' => __("Edit Product category"),
    \'new_item\' => __("New Product Category"),
    \'view_item\' => __("View Product Category"),
    \'search_items\' => __("Search Product categories"),
    \'not_found\' =>  __("No Product categories Found"),
    \'not_found_in_trash\' => __("No Product Categories Found in Trash"),
    \'parent_item_colon\' => \'\'
  );

  /*Register ourproducts post type*/
  register_post_type(\'ourproducts\' , array(
    \'labels\' => $labels,
    \'public\' => true,
    \'orderby\' => \'menu_order\', 
    \'has_archive\' => false,
    \'menu_icon\' => \'dashicons-groups\',
    \'rewrite\' => false,
    \'supports\' => array(\'title\', \'editor\', \'thumbnail\', \'page-attributes\'),
    \'show_ui\'             => true, 
    \'show_in_menu\'        => \'custom-options\',
    //\'menu_position\'       => 40,

  ) );
}
add_action( \'init\', \'ourproducts_post_type\', 0 );
在前端(打开front-page.php 模板页),我尝试在循环外部显示字段值,如下所示:

    <h3><?php the_field(\'nasi_proizvodi\', $post->ID); ?></h3>
此外,我在循环内部进行了如下尝试:

<?php while ( have_posts() ) : the_post(); ?>

            <h3><?php the_field(\'nasi_proizvodi\'); ?></h3>

<?php endwhile;  ?>
但什么都没有显示。除非我特别这样写帖子id:

<?php while ( have_posts() ) : the_post(); ?>

            <h3><?php the_field(\'nasi_proizvodi\', 4268); ?></h3>

<?php endwhile;
但我需要动态更改它们,不仅仅是针对这个特定的帖子id。有趣的是,我已经在首页上创建了几个文本字段(我正在开发一个主题),并且动态显示这些字段没有问题。请问我做错了什么,有什么建议吗?

1 个回复
最合适的回答,由SO网友:Dharmishtha Patel 整理而成

请检查您的PostType并添加参数

<?php 
$query = new WP_Query( array( \'post_type\' => \'ourproducts\' ) );
if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : 
        $query->the_post(); 
?>
        <h3><?php echo get_field(\'nasi_proizvodi\'); ?></h3>
<?php 
    endwhile; 
    wp_reset_postdata(); 
endif; 
?>

相关推荐