2013-01-02 114 views
0

我有一個由成員在這裏創建的函數。它獨立工作在一個PHP文件中。但只要我嘗試在Wordpress模板中使用它(single.php是精確的),它就會停止工作(無論如何它都會輸出false)。我已經包含了原始代碼和代碼,因爲我試圖在我的模板的single.php中使用它。將不勝感激任何幫助。謝謝。功能不能在Wordpress中工作

獨奏:

<?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. ?> 
+0

不知道我明白這個問題。此代碼只是檢查陣列當前時間以確定當前時間是否在業務小時內。它本身就是一個PHP文件,如同它在返回真實的業務是開放的。在Wordpress中,它返回false。 – bionemesis

+0

確保你使用[date_default_timezone_set](http://php.net/manual/en/function.date-default-timezone-set.php)正確設置你的時區。服務器可能使用UTC或GMT,或者誰知道默認情況下還有什麼。 – Levi

+0

已經排除了,現在纔剛剛出現。無論是單獨還是在Wordpress中,輸出都與時間()相同。我還在date_default_timezone_set中添加了一些內容,但沒有任何區別。 – bionemesis

回答

0

首先,你應該將你的函數定義while循環外,如該腳本將盡快有更多的突破超過1個職位。接下來,確保你的服務器時區是正確的。其他一切都很好。

+0

我在循環之前移動了函數,在get_header()之後,並且使用date_default_timezone_set設置了時區。仍然是假的。 – bionemesis

+0

添加回顯日期(「H:I」);並檢查它是否與您當前時間匹配 –

+0

這並不符合。 21:16自己輸出腳本,從Wordpress輸出04:16。那麼,爲什麼會這樣呢? – bionemesis