我有一个由成员在这里创建的函数。它在 PHP 文件中单独工作。但是,一旦我尝试在我的 Wordpress 模板中使用它(准确地说是 single.php),它就会停止工作(无论如何它都会输出 false)。当我试图在我的模板的 single.php 中使用它时,我已经包含了原始代码和代码。将不胜感激任何帮助。谢谢。
独奏:
<?php
// Normalize time to count from 0
function timeOfWeek($time) {
if (!is_int($time)) $time = strtotime($time) ;
$secondsInWeek = (7 * 24 * 3600);
return (($time - strtotime('monday 00:00')) % $secondsInWeek + $secondsInWeek) % $secondsInWeek;
}
function isOpen($opened, $time) {
$time = timeOfWeek($time);
foreach ($opened as $openday) {
list($open, $close) = $openday;
if ($open < $close) {
if ($time > $open && $time < $close) return true;
} else {
if ($time > $open || $time < $close) return true; // Special case sunday -> monday
}
}
return false;
}
$opened = array();
$opened[] = array(timeOfWeek('monday 10:00'), timeOfWeek('monday 23:00'));
$opened[] = array(timeOfWeek('tuesday 10:00'), timeOfWeek('tuesday 23:00'));
$opened[] = array(timeOfWeek('wednesday 10:00'), timeOfWeek('wednesday 23:00'));
$opened[] = array(timeOfWeek('thursday 10:00'), timeOfWeek('thursday 23:00'));
$opened[] = array(timeOfWeek('friday 10:00'), timeOfWeek('saturday 01:00'));
$opened[] = array(timeOfWeek('saturday 10:00'), timeOfWeek('sunday 01:00'));
$opened[] = array(timeOfWeek('sunday 10:00'), timeOfWeek('monday 01:00'));
$open = isOpen($opened, time());
var_dump($open);
?>
在 single.php 中:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php if (et_get_option('lucid_integration_single_top') <> '' && et_get_option('lucid_integrate_singletop_enable') == 'on') echo (et_get_option('lucid_integration_single_top')); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('entry clearfix'); ?>>
<?php
$index_postinfo = et_get_option('lucid_postinfo2');
if ( $index_postinfo ){
echo '<p class="meta-info">';
et_postinfo_meta( $index_postinfo, et_get_option('lucid_date_format'), esc_html__('0 comments','Lucid'), esc_html__('1 comment','Lucid'), '% ' . esc_html__('comments','Lucid') );
echo '</p>';
}
?>
<?php
global $wp_embed;
$thumb = '';
$et_full_post = get_post_meta( $post->ID, '_et_full_post', true );
$width = apply_filters('et_blog_image_width',630);
if ( 'on' == $et_full_post ) $width = apply_filters( 'et_single_fullwidth_image_width', 960 );
$height = apply_filters('et_blog_image_height',210);
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Singleimage');
$thumb = $thumbnail["thumb"];
$et_video_url = get_post_meta( $post->ID, '_et_lucid_video_url', true );
?>
<?php if ( '' != $thumb && 'on' == et_get_option('lucid_thumbnails') ) { ?>
<div class="post-thumbnail">
<?php
if ( 'video' == get_post_format( $post->ID ) && '' != $et_video_url ){
$video_embed = $wp_embed->shortcode( '', $et_video_url );
$video_embed = preg_replace('/<embed /','<embed wmode="transparent" ',$video_embed);
$video_embed = preg_replace('/<\/object>/','<param name="wmode" value="transparent" /></object>',$video_embed);
$video_embed = preg_replace("/height=\"[0-9]*\"/", "height=350", $video_embed);
$video_embed = preg_replace("/width=\"[0-9]*\"/", "width={$width}", $video_embed);
echo $video_embed;
} else {
print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext);
}
?>
</div> <!-- end .post-thumbnail -->
<?php } ?>
<div class="post_content clearfix">
<h1 class="title"><?php the_title(); ?></h1>
<?php
// Normalize time to count from 0
function timeOfWeek($time) {
if (!is_int($time)) $time = strtotime($time) ;
$secondsInWeek = (7 * 24 * 3600);
return (($time - strtotime('monday 00:00')) % $secondsInWeek + $secondsInWeek) % $secondsInWeek;
}
function isOpen($opened, $time) {
$time = timeOfWeek($time);
foreach ($opened as $openday) {
list($open, $close) = $openday;
if ($open < $close) {
if ($time > $open && $time < $close) return true;
} else {
if ($time > $open || $time < $close) return true; // Special case sunday -> monday
}
}
return false;
}
$opened = array();
$opened[] = array(timeOfWeek('monday 10:00'), timeOfWeek('monday 23:00'));
$opened[] = array(timeOfWeek('tuesday 10:00'), timeOfWeek('tuesday 23:00'));
$opened[] = array(timeOfWeek('wednesday 10:00'), timeOfWeek('wednesday 23:00'));
$opened[] = array(timeOfWeek('thursday 10:00'), timeOfWeek('thursday 23:00'));
$opened[] = array(timeOfWeek('friday 10:00'), timeOfWeek('saturday 01:00'));
$opened[] = array(timeOfWeek('saturday 10:00'), timeOfWeek('sunday 01:00'));
$opened[] = array(timeOfWeek('sunday 10:00'), timeOfWeek('monday 01:00'));
$open = isOpen($opened, time());
var_dump($open);
?>
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<p><strong>'.esc_attr__('Pages','Lucid').':</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php edit_post_link(esc_attr__('Edit this page','Lucid')); ?>
</div> <!-- end .post_content -->
</article> <!-- end .entry -->
<?php if (et_get_option('lucid_integration_single_bottom') <> '' && et_get_option('lucid_integrate_singlebottom_enable') == 'on') echo(et_get_option('lucid_integration_single_bottom')); ?>
<?php
if ( et_get_option('lucid_468_enable') == 'on' ){
if ( et_get_option('lucid_468_adsense') <> '' ) echo( et_get_option('lucid_468_adsense') );
else { ?>
<a href="<?php echo esc_url(et_get_option('lucid_468_url')); ?>"><img src="<?php echo esc_url(et_get_option('lucid_468_image')); ?>" alt="468 ad" class="foursixeight" /></a>
<?php }
}
?>
<?php
if ( 'on' == et_get_option('lucid_show_postcomments') ) comments_template('', true);
?>
<?php endwhile; // end of the loop. ?>