【问题标题】:How to use a field block in many Wordpress Pages with Advanced Custom Fields?如何在许多带有高级自定义字段的 Wordpress 页面中使用字段块?
【发布时间】:2015-11-18 15:20:18
【问题描述】:

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

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

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

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

我的部分:

content-footer-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 界面的截图:

【问题讨论】:

  • 将“主页”页面的帖子 ID 作为第二个参数添加到 get_field() 调用中。那应该可以解决你的小问题。有关更多信息,请参阅文档:advancedcustomfields.com/resources/get_field
  • 老兄,非常感谢。如果你想写同样的东西作为回复而不是评论,那么我可以接受答案并为你投票。

标签: php wordpress advanced-custom-fields


【解决方案1】:

将“主页”页面的帖子 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

您还应该在输出前使用适当的卫生功能。查看下面的链接以查找 WordPress 核心卫生功能。尽量严格要求。

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

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2015-01-21
    • 2017-05-01
    相关资源
    最近更新 更多