简体   繁体   中英

Associate template to Advanced Custom Fields post type

I'm trying to use the advanced custom fields plugin and associate it with a partial (probably not the right word) that I wrote that would display the fields from the custom post type.

How do I associate a php file with the custom post type?

The objective is to be able to embed the partial in a post.

Here is my partial: cta-partial.php

<?php
/*
Template Name: CTA-Partial
*/
?>
<!-- cta markup -->
<!-- if there is an article then load -->
<?php
    $args = array(
        'post_type' => 'cta_post_type'
    );
    $query = new WP_Query( $args ); ?>
    <?php if( $query->have_posts() ) : while( $query->have_posts() ) : 
$query->the_post();?> 
    </div>
</div>
<!-- end the markup so as to allow full-width -->
<div class="article" style="background-image: url('<?php the_field('cta_background'); ?>')">
    <div class="custom-background">
        <div class="columns small-12 medium-6 image-container">
            <div class="hide-for-medium">
                <img src="<?php the_field('cta_background'); ?>">
            </div>
        </div>
        <div class="columns small-12 medium-6 mobile-style">
            <h1> <?php the_field('cta_title');?></h1>
            <p><?php the_field('cta_text'); ?></p>
            <a href="<?php the_field('button_link');?>"><button><?php the_field('button_label')?></button></a>
        </div>
    </div>
</div>
<div class="row">
    <div class="columns small-12 medium-12 content-wrapper">
    <!-- continue content loop -->
    <?php endwhile; endif; wp_reset_postdata(); ?>

This is the file i'd like to render the php associated with my custom fields post type.

Here is the documentation for ACF: https://www.advancedcustomfields.com/resources/ I've been scouring the docs and i'm not sure if it's possible. Thank you for the help so far, still researching.

image of embedded cta in post

I assumed that what you want is assigned a page to that file. You can use a archive-template.php type file.

https://codex.wordpress.org/Creating_an_Archive_Index

You can pass a post id (or user id, or term, or other field) to an ACF field to retrieve the field located elsewhere. And as a side note, it looks like WP_Query is overkill for this. Try this:

<?php
  $cta_posts = get_posts( array(
    'posts_per_page' => 1,
    'post_type' => 'cta_post_type'
  ) );
  if( $cta_posts ){
    foreach( $cta_posts as $cta_post ):
    ?>
      <div class="article" style="background-image: url(<?php the_field( 'cta_background', $cta_post->ID ); ?>)">
        ...
      </div>
    <?php
    endforeach;
  }

My posts_per_page assumes you just want one CTA on the page. And if that's the case, a post type may also be overkill. You may want to look at an ACF options page instead, put your fields on the options page, and then call them with:

<div class="article" style="background-image: url(<?php the_field( 'cta_background', 'option' ); ?>)">

I don't think you need to have a query in your cta-partial.php partial.

If you are using <?php get_template_part('cta-partial.php'); ?> <?php get_template_part('cta-partial.php'); ?> assigned variables aren't available in the partial.

If you use <?php include( locate_template('cta-partial.php') ); ?> <?php include( locate_template('cta-partial.php') ); ?> instead, then you do have access to variables assigned from the page or post.

In your partial, you might need to adjust your the_field() usage to pull in current post id. Like this:

<?php the_field('cta_background', $page_or_post_id); ?>

Just setup that $page_or_post_id in the original page where you call in the partial.

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