简体   繁体   中英

Using ACF fields in archive page for custom posts

I've created a custom posts called Projects and created a Projects template inside of archive-projects.php. I've also created a single-projects.php to display individual project. However, this became a problem when I had to use an ACF on the Projects template. I want to display a featured project on the Project page and also list out few of the posts I've created for projects on the same page. Below is my code to get featured project image.

<?php echo get_field('featured_project'); ?>


<?php
    $featured_project = get_field('featured_project');
    if ($featured_project):

        $project = $featured_project;
        setup_postdata( $project );

        if ( has_post_thumbnail($featured_project->ID) ) { // check if the post has a Post Thumbnail assigned to it.
        $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($featured_project->ID), 'large' );
        $featured_image = $featured_image[0];
    } 

?>

I've read few articles and similar questions on Stackoverflow but haven't found an answer yet. Has anyone been successful figuring this out?

You can try to use my plugin ACF CPT Options Page

That creates options page for each custom post type, then you can add Relationship Field to get posts easily

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