6

所以我的问题是我刚刚创建了一个自定义帖子类型,并创建了几个帖子来配合这个。我在我创建的模板 single-post_type.php 中使用 ACF 字段。这些在个别帖子上显示得很好,但我需要个别帖子页面上的一些 ACF 字段值显示在自定义帖子索引上。我试过寻找答案,但似乎找不到我要找的东西。

作为参考,这是我注册自定义帖子类型的代码:

// Add new post type for Available Homes
add_action('init', 'available_homes');
function available_homes() 
{
$available_homes_labels = array(
    'name' => _x('Available Homes', 'post type general name'),
    'singular_name' => _x('Available Home', 'post type singular name'),
    'all_items' => __('All Available Homes'),
    'add_new' => _x('Add New Listing', 'recipes'),
    'add_new_item' => __('Add New Listing'),
    'edit_item' => __('Edit Listing'),
    'new_item' => __('New Listing'),
    'view_item' => __('View Listing'),
    'search_items' => __('Search in Available Homes'),
    'not_found' =>  __('No listings found'),
    'not_found_in_trash' => __('No listings found in trash'), 
    'parent_item_colon' => ''
);
$args = array(
    'labels' => $available_homes_labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 20,
    'supports' => array('title','editor','custom-fields'),
    'has_archive'  =>  'available_homes'
); 
register_post_type('available_homes',$args);
}

我在 single-available_homes.php 上有一些基本的 ACF 字段

然后我有一个名为archive-available_homes.php 的页面,其中包含以下代码:

<?php get_header(); ?>
<h1 class="page-title">Available Homes</h1>
    <div id="primary">
        <div id="content" role="main">

        <?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>

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

                <?php get_template_part( 'content', get_post_type() );     ?>

            <?php endwhile; ?>

        <?php else : ?>
            <article id="post-0" class="post no-results not-found">
                <header class="entry-header">
                    <h1 class="entry-title"><?php _e( 'Nothing   Found', 'twentyeleven' ); ?></h1>
                </header><!-- .entry-header -->
                <div class="entry-content">
                    <p><?php _e( 'Apologies, but no results were   found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); ?></p>
                    <?php get_search_form(); ?>
                </div><!-- .entry-content -->
            </article><!-- #post-0 -->
        <?php endif; ?>

        </div><!-- #content -->
    </div><!-- #primary -->
<div class="widget_area">
<?php dynamic_sidebar("gravity_form_widget_area"); ?>
</div>
<?php get_footer(); ?>

ACF 字段和其他所有内容在各个帖子上都运行良好,但我只是不知道该怎么做才能让 ACF 字段数据显示在存档页面上。帮助?

4

4 回答 4

2

你可以使用我的插件ACF CPT Options Pages

在这里下载

于 2016-02-26T11:48:50.427 回答
2

ACF 字段必须在循环中。您的archive-available_homes.php文件没有循环,它调用

<?php get_template_part( 'content', get_post_type() );     ?>

所以它在 content.php 中,你必须添加你想要渲染的字段。否则,您可以创建自己的content-available_homes.php文件并调用:

<?php get_template_part( 'content', 'available_homes' );     ?>
于 2015-11-03T14:19:58.633 回答
1

您是否在此处查看过如何从其他页面发布字段:http: //www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/

这可能是您正在寻找的。

于 2014-02-27T20:24:53.523 回答
0

2020 - 我用这个插件在几分钟内完成了它:https ://wordpress.org/plugins/acf-archive/

于 2020-03-05T13:19:47.167 回答