我正在构建一个使用Wordpress和Woocommerce的eCommerce站点,遇到了一个障碍,不知道如何解决。
我正在使用ACF (高级自定义字段)来创建一个内容块,我想在许多不同的页面中重用这些内容。问题是,当您创建字段块时,您必须选择要在哪里显示它。我的想法是,让我们在主页的Admin部分显示它,这将是管理员能够操作这些内容的唯一地方。
然后,我的想法是在代码中创建一个分部(当然不是管理员),然后从我想要显示这个内容的所有页面中调用这个部分。
问题是,内容显示在主页中,但在其他地方,我只得到没有内容的空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界面的截图:

发布于 2015-11-19 08:25:58
将“主页”页面的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 );有关更多信息,请参见文档化:field
在输出之前,还应该使用适当的卫生功能。查看下面的链接,查找WordPress核心卫生功能。尽量严格要求。
https://stackoverflow.com/questions/33783837
复制相似问题