调用PHP函数中的ACF字段



我有这个PHP函数来添加一个预定的横幅到我的网站。我想让它可以用ACF字段。

函数:

function holiday_site_banner() {

$from_time = new DateTime( 'get_field('banner_start_date')' );
$to_time = new DateTime( 'get_field('banner_end_date')' );
if ( new DateTime() > $from_time && new DateTime() < $to_time ) {
?><div class="site-banner">
<span>
<strong style="">&#9432; LET OP:</strong> <?php get_field('banner_message')?>
</span>
</div><?php
}
}
add_action( 'wp_footer', 'holiday_site_banner' );

我试图使用get_field来实现$from_time$to_time中的ACF值,但它不起作用。我将ACF字段设置为输出Y-m-d H:i的日期拾取器,命名为[banner_start_date][banner_end_date]

然后我也有一个名为[banner_message]的ACF文本区域字段放置在<span>标签之间,你可以看到这也不工作。

将ACF字段添加到使用以下代码创建的自定义选项页面:

if( function_exists('acf_add_options_page') ) {

acf_add_options_page(array(
'page_title'    => 'DWD Vakantiesluiting',
'menu_title'    => 'DWD Vakantie',
'menu_slug'     => 'dwd-vakantiesluiting',
'capability'    => 'edit_posts',
'icon_url'      => 'dashicons-lock',
'redirect'      => true,
'updated_message' => __("Vakantiemodus ingesteld", 'acf'),
));

}

我修改了代码,但仍然不起作用:

function scheduled_site_banner() {
// Get the start and end date values from ACF fields
$start_date = get_field('banner_start_date', 'option');
$end_date = get_field('banner_end_date', 'option');
$banner_message = get_field('banner_message', 'option');
// Create DateTime objects for the start and end date
$start_time = new DateTime($start_date);
$end_time = new DateTime($end_date);
// Format the start and end date
$formatted_start_date = date("F j, Y",strtotime($start_date));
$formatted_end_date = date("F j, Y",strtotime($end_date));
// Check if the current date and time falls between the start and end dates
if ( new DateTime() > $start_time && new DateTime() < $end_time ) {
?>
<div class="site-banner">
<span>
<strong>Important Notice:</strong> <?php echo $banner_message; ?>  Our site-wide banner is scheduled to display from <?php echo $formatted_start_date ?> to <?php echo $formatted_end_date ?>
</span>
</div>
<?php
}
}
add_action( 'wp_footer', 'scheduled_site_banner' );

// Get the start and end date values from ACF fields中使用双引号也不起作用。

有谁能帮我做这件事吗?提前感谢!

我完成了,谢谢你的帮助。问题是日期格式和其他一些东西。我还添加了Europe/AmsterdamTimezone。

这是正确的代码:

function scheduled_site_banner() {
// Get the start and end date values from ACF fields
$start_date = get_field("banner_start_date", "option");
$end_date = get_field("banner_end_date", "option");
$banner_message = get_field("banner_message", "option");
// Create DateTime objects for the start and end date and set Amsterdam timezone
$start_time = DateTime::createFromFormat("Y-m-d H:i:s", $start_date, new DateTimeZone('Europe/Amsterdam'));
$end_time = DateTime::createFromFormat("Y-m-d H:i:s", $end_date, new DateTimeZone('Europe/Amsterdam'));
if($start_time === false || $end_time === false) {
// date is not in correct format
return;
}
// Check if the current date and time falls between the start and end dates
if ( new DateTime('Europe/Amsterdam') > $start_time && new DateTime('Europe/Amsterdam') < $end_time ) {
?>
<div class="site-banner">
<span>
<strong>Important:</strong> <?php echo $banner_message; ?>
</span>
</div>
<?php
}
}
add_action( 'wp_footer', 'scheduled_site_banner' );
add_filter('acf/format_value', 'do_shortcode');

我还添加了2个短代码,可以在消息中使用,以显示在acf字段中设置的开始日期和结束日期。我使用了下面的代码:

function register_acf_shortcodes() {
add_shortcode('begin_vakantie', 'acf_banner_start_date_shortcode');
add_shortcode('einde_vakantie', 'acf_banner_end_date_shortcode');
}
add_action( 'init', 'register_acf_shortcodes');
function acf_banner_start_date_shortcode() {
$start_date = get_field('banner_start_date', 'option');
return date('d-m-Y', strtotime($start_date));
}
function acf_banner_end_date_shortcode() {
$end_date = get_field('banner_end_date', 'option');
return date('d-m-Y', strtotime($end_date));
}

并将此过滤器添加到function scheduled_site_banner()中,如您所见:

add_filter('acf/format_value', 'do_shortcode');

最后但并非最不重要的是,我使用这个CSS样式的横幅和定位在页面的底部:

.site-banner {
position: fixed;
bottom: 0;
width: 100%;
background-color: #cf2e2e;
padding: 1em;
z-index: 99999;
text-align: center;
color: #ffffff;
}

最新更新