如何在具有高级自定义字段的许多 Wordpress 页面中使用字段块?

How to use a field block in many Wordpress Pages with Advanced Custom Fields?

我正在使用 Wordpress 和 Woocommerce 构建一个电子商务网站,但遇到了障碍,不知道如何解决。

我正在使用 ACF(高级自定义字段)创建一个我想在许多不同页面中重复使用的内容块。问题是,当您创建字段块时,您必须选择要在何处显示它。我的想法是,让我们将它显示在主页的管理部分,这将是管理员能够操作此内容的唯一地方。

然后我的想法是在代码中创建一个部分(当然不是管理员),然后从我想显示此内容的所有页面调用这个部分。

问题是,内容显示在主页中,但在其他任何地方我只得到没有内容的空 HTML 标签。

我的部分:

内容页脚-callouts.php

<?php  

// FOOTER CALLOUTS
// ---------------

// Left
$left_callout_image              = get_field('left_callout_image');
$left_callout_title              = get_field('left_callout_title');
$left_callout_description        = get_field('left_callout_description');

// Center
$center_callout_title            = get_field('center_callout_title');
$center_callout_description      = get_field('center_callout_description');

// Right
$right_callout_image             = get_field('right_callout_image');
$right_callout_title             = get_field('right_callout_title');
$right_callout_description       = get_field('right_callout_description');

?>


<div class="row">

    <div class="gives-back section phone-12 laptop-4 columns">

        <img src="<?php echo $left_callout_image['url']; ?>" alt="<?php echo $left_callout_image['alt']; ?>">

        <h3><?php echo $left_callout_title; ?></h3>

        <?php echo $left_callout_description; ?>

    </div><!-- .gives-back -->


    <div class="social section phone-12 laptop-4 columns">

        <ul class="social-links">

            <?php  

                $social_footer_args = array(
                    'post_type'      => 'social',
                    'order_by'       => 'post_id',
                    'order'          => 'ASC',
                    'posts_per_page' => '4'
                );

                $social_icons = new WP_Query($social_footer_args);

            ?>

            <?php while ($social_icons -> have_posts()) : $social_icons -> the_post(); ?>

                <li>
                    <a href="<?php the_field('social_url'); ?>" target="_blank">
                        <i class="fa fa-<?php the_field('social_icon'); ?> fa-2x"></i>
                    </a>
                </li>

            <?php endwhile; wp_reset_query(); ?>

        </ul><!-- #social-links -->

        <h3><?php echo $center_callout_title; ?></h3>

        <?php echo $center_callout_description; ?>                
    </div><!-- .social -->


    <div class="purchase section phone-12 laptop-4 columns">

        <img src="<?php echo $right_callout_image['url']; ?>" alt="<?php echo $right_callout_image['alt']; ?>">

        <h3><?php echo $right_callout_title; ?></h3>

        <?php echo $right_callout_description; ?>

    </div><!-- .purchase -->

</div><!-- .row -->

我从任何页面调用:

<section class="engage row-full padded-section">

    <?php get_template_part('template-parts/content', 'footer-callouts'); ?>

</section><!-- .engage .row-full -->

ACF界面截图:

将 "Home" 页面的 post ID 作为第二个参数添加到 get_field() 调用中。

// Your home page post ID
$post_id = 1;

// Your get_field() calls should look like the one below
$field_var = get_field( 'meta_key', $post_id );

有关详细信息,请参阅文档:http://advancedcustomfields.com/resources/get_field

您还应该在输出前使用适当的清理功能。查看下面的 link 以找到 WordPress 核心卫生功能。尽可能严格。

https://codex.wordpress.org/Data_Validation#Output_Sanitation