简体   繁体   中英

How to pull ACF values into a custom post type archive

So my issue is that I just created a custom post type and have created a couple of posts to go along with this. I am using ACF fields in the template single-post_type.php That I created. These show up fine on the individual posts, but I need some of the ACF field values that are on the individual post page to show up on the custom post index. I've tried searching for answers but just can't seem to find what I'm looking for.

For reference, Here is the code I have registering the custom post type:

// 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);
}

And I have some basic ACF fields on single-available_homes.php

Then I have a page called archive-available_homes.php which has this code:

<?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 Fields and everything else is working fine on the individual posts but I just don't know what to do to get the ACF field data to show on the archive page. Help?

ACF Fields must be in the loop. Your archive-available_homes.php file doesn't have a loop, it calls

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

So it's in content.php you have to add your field you want to render. Otherwise, you can create your own content-available_homes.php file and call :

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

U can use my plugin ACF CPT Options Pages

Download here

Have you looked into how to post fields from other pages here: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/

This may be what you are looking for.

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM