简体   繁体   English

将模板与“高级自定义字段”帖子类型相关联

[英]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? 如何将php文件与自定义帖子类型相关联?

The objective is to be able to embed the partial in a post. 目的是能够将部分嵌入到帖子中。

Here is my partial: cta-partial.php 这是我的部分: 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. 这是我要呈现与我的自定义字段帖子类型关联的php的文件。

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. 这是ACF的文档: https : //www.advancedcustomfields.com/resources/我一直在搜索文档,但不确定是否可行。 Thank you for the help so far, still researching. 谢谢您到目前为止所提供的帮助,仍在研究中。

image of embedded cta in post 帖子中嵌入cta的图像

I assumed that what you want is assigned a page to that file. 我假设您要为该文件分配一个页面。 You can use a archive-template.php type file. 您可以使用archive-template.php类型的文件。

https://codex.wordpress.org/Creating_an_Archive_Index 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. 您可以将帖子ID(或用户ID,术语或其他字段)传递到ACF字段以检索位于其他位置的字段。 And as a side note, it looks like WP_Query is overkill for this. WP_Query ,看来WP_Query是过大的。 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. 我的posts_per_page假定您只希望页面上有一个CTA。 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: 您可能想要查看ACF选项页面 ,将字段放在选项页面上,然后使用以下命令调用它们:

<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. 我认为您不需要在cta-partial.php部分中进行查询。

If you are using <?php get_template_part('cta-partial.php'); ?> 如果您使用的是<?php get_template_part('cta-partial.php'); ?> <?php get_template_part('cta-partial.php'); ?> assigned variables aren't available in the partial. <?php get_template_part('cta-partial.php'); ?>分配的变量在局部变量中不可用。

If you use <?php include( locate_template('cta-partial.php') ); ?> 如果使用<?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. <?php include( locate_template('cta-partial.php') ); ?>相反,那么您确实有权访问从页面或帖子分配的变量。

In your partial, you might need to adjust your the_field() usage to pull in current post id. 在您的部分the_field() ,您可能需要调整the_field()用法以获取当前帖子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. 只需在您调用部分页面的原始页面中设置$page_or_post_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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