[英]How to use a field block in many Wordpress Pages with Advanced Custom Fields?
我正在使用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()調用中。
// 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.