【问题标题】:Associate template to Advanced Custom Fields post type将模板关联到高级自定义字段帖子类型
【发布时间】:2018-02-19 21:21:37
【问题描述】:

我正在尝试使用高级自定义字段插件并将其与我编写的部分(可能不是正确的词)相关联,该部分将显示来自自定义帖子类型的字段。

如何将 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(); ?>

这是我要呈现与我的自定义字段帖子类型关联的 php 的文件。

这里是 ACF 的文档:https://www.advancedcustomfields.com/resources/ 我一直在搜索文档,但不确定是否可行。感谢您到目前为止的帮助,仍在研究中。

image of embedded cta in post

【问题讨论】:

    标签: php wordpress custom-post-type advanced-custom-fields


    【解决方案1】:

    我假设您想要的内容已分配给该文件的页面。您可以使用 archive-template.php 类型的文件。

    https://codex.wordpress.org/Creating_an_Archive_Index

    【讨论】:

    • 我想将它嵌入到帖子中,而不是为该文件分配一个页面,这样我就不必打破帖子循环。
    • 这可能是我想做的事情的线索。我只是不想在我的帖子中包含我的标记:shortcode
    【解决方案2】:

    您可以将帖子 ID(或用户 ID、术语或其他字段)传递给 ACF 字段以检索位于其他位置的字段。附带说明一下,看起来WP_Query 对此有点矫枉过正。试试这个:

    <?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;
      }
    

    我的posts_per_page 假设您只希望页面上有一个 CTA。如果是这样的话,帖子类型也可能是矫枉过正。您可能想查看ACF options page,将您的字段放在选项页面上,然后调用它们:

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

    【讨论】:

      【解决方案3】:

      我认为您不需要在 cta-partial.php 部分中进行查询。

      如果您使用的是&lt;?php get_template_part('cta-partial.php'); ?&gt;,则分配的变量在部分中不可用。

      如果您改用&lt;?php include( locate_template('cta-partial.php') ); ?&gt;,那么您确实可以访问从页面或帖子分配的变量。

      在您的部分中,您可能需要调整您的 the_field() 用法以获取当前帖子 ID。像这样:

      &lt;?php the_field('cta_background', $page_or_post_id); ?&gt;

      只需在您调用部分的原始页面中设置 $page_or_post_id

      【讨论】:

        猜你喜欢
        • 2014-07-07
        • 2018-01-29
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 2021-04-30
        相关资源
        最近更新 更多