我最近安装了名为“添加到日历”的 Wordpress 插件,并试图将生成按钮的代码合并到我创建的事件简码中。
他们提供的按钮代码如下所示:
<html>
<add-to-calendar-button
name="title"
startDate="2023-05-11"
startTime="10:15"
endTime="23:30"
timeZone="America/Los_Angeles"
description=""
options="'Apple','Google','iCal','Outlook.com','Yahoo'"
buttonsList
hideTextLabelButton
buttonStyle="round"
lightMode="bodyScheme"
></add-to-calendar-button>
</html>
我想要做的是用来自自定义帖子类型的信息填充此代码的不同字段。例如将帖子标题拉入名称字段,看起来像这样:
name="Post Title"
如果有帮助,这是我的简码的整个编码:
<?php
add_shortcode( 'Calendar', 'calendar' );
function calendar( $atts, $shrt_content = null ) {
$atts = shortcode_atts(array(
'category' => ''
), $atts, 'calendar');
$categories = ! empty( $atts['category'] ) ? explode( ',' , $atts['category'] ) : '';
extract( $atts );
$query_args = array(
'post_type' => 'event',
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1,
'category_name' => 'private',
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'slug',
'operator' => 'AND',
'terms' => $categories
) )
);
$post_query = new WP_Query( $query_args );
$number_of_events = $events_query->found_posts;
ob_start();
// If post is there
if ( $post_query->have_posts() ) { ?>
<?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<?php
$monthStart = get_field('event_date');
$formatMonthStart = date("M", strtotime($monthStart));
$dateStart = get_field('event_date');
$formatDateStart = date("j", strtotime($dateStart));
$yearStart = get_field('event_date');
$formatYearStart = date("y", strtotime($yearStart));
?>
<div id="eventDetails" >
<h1><?php the_title(); ?></h1>
<p>
<?php echo the_field('event_date'); ?>
<?php $value = get_field( "event_date" );
if( $value ) { echo ' - ' . get_field('event_date') .' ';
} else { echo ''; } ?>
</p>
<p><?php the_content(); ?></p>
<html>
<add-to-calendar-button
name="title"
startDate="2023-05-11"
startTime="10:15"
endTime="23:30"
timeZone="America/Los_Angeles"
description=""
options="'Apple','Google','iCal','Outlook.com'"
buttonsList
hideTextLabelButton
buttonStyle="round"
lightMode="bodyScheme"
></add-to-calendar-button>
</html>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php
}
$retVal = ob_get_contents();
ob_end_clean();
return $retVal;
}
我要填充的字段如下:name、startDate、startTime、endTime 和 description
我认为这是可行的,但短代码对我来说仍然很新。
另外,我试过这样的事情:
name=<?php echo the_title(); ?>
这部分有效,当单击按钮时 - 相应的日历打开,但事件名称中唯一显示的是第一个词,空格后的任何内容都不会显示。