2016-07-26 137 views
0

我有一個函數,我在我的functions.php頁面中寫了一個圖庫,以在某些頁面上顯示。它顯示的自定義模板,但現在我需要它來顯示上的index.php這是從我的functions.php文件中的代碼:調用PHP函數在index.php中不起作用

function min_get_page_gallery($echo = true) { 
global $post; 



$show_gallery = get_post_meta($post->ID, 'min_gallery-show', true); 

if (empty($show_gallery)) { 
    return; 
} 

$gallery  = get_post_meta($post->ID, 'min_image_advanced', false); 

ob_start(); 
?> 
<div class="gallery" id="gallery-<?php echo $post->ID; ?>"> 
    <button class="gallery-move-left"><i class="fa fa-arrow-circle-left"  aria-hidden="true"></i></button> 
    <div class="image_container clearfix"> 

     <?php 
      $count = count($gallery); 
      $num = ceil($count/3); 
      //$width_container = $num * 100; 
      //$width_row = 100/$num; 
      //echo '<div class="gallery_inner" style="width:' . $width_container . '%;">'; 
      echo '<div class="gallery_inner">'; 
      for ($i = 0; $i < $count; $i++) { 
       if ($i % 3 == 0) { 
        //echo '<div class="row" style="width: ' . $width_row . '%;">'; 
        echo '<div class="row'. (0 == $i ? ' active': ' inactive') .'">'; 
       } 
       echo '<div class="col-sm-4 img_container' . (0 == $i ? ' active': ' inactive') . '">'; 
       echo wp_get_attachment_image($gallery[$i], 'thumb-gallery'); 
       echo '</div>'; 
       if ($i % 3 == 2 || ($i+1) == $count) { 
        echo '</div>'; 
       } 

      } 
      echo '</div>'; 
     ?> 
    </div> 
    <button class="gallery-move-right"><i class="fa fa-arrow-circle-right" aria-hidden="true"></i></button> 
</div> 
<?php 
$return = ob_get_contents(); 
ob_end_clean(); 

if ($echo) { 
    echo $return; 
    } else { 
     return $return; 
    } 

} 

該代碼工作就像一個魅力。這裏是我稱之爲min_get_page_gallery()的地方;在awards.php它完美的作品:

<?php 
/* Template Name: Awards Page Template */ 

get_header(); ?> 

<div class="container" id="block-no-sidebar"> 
    <h1><?php the_title(); ?></h1> 
<div id="award-list"> 
    <?php echo min_get_awards(); ?> 
</div> 
<div class="row"> 
    <?php min_get_page_gallery(); ?> 
</div> 
    <?php min_get_page_tabs(); ?> 
</div> 
<?php get_footer(); ?> 

如今終於,我嘗試添加min_get_page_gallery相同的函數調用();在我的index.php文件是這樣的:

<?php 
    // Silence is golden. 
     if (! defined ('ABSPATH')) { 
     exit; 
     } 
    ?> 

    <?php get_header(); ?> 

     <style class="take-to-head"> 
      #block-main-content-with-sidebar { background: #ffffff; } 
     </style> 

     <div class="container" id="block-main-content-with-sidebar"> 
     <div class="row"> 

     <div class="col-sm-8"> 
      <?php 
      if (have_posts()) : while (have_posts()) : the_post(); 
       l('block-' . get_post_type()); 
      endwhile; else: 
       l('block-none'); 
      endif; 

      ?> 

     </div> 
     <div class="col-sm-4"> 
      <?php l('block-sidebar'); ?> 
     </div> 
     </div> 
     <div class="row"> 
      <?php min_get_page_gallery(); ?> 
     </div> 
    </div> 

有我丟失的東西?

+0

我沒有看到這一點你的代碼,但你如何在index.php中包含functions.php?你使用'require'還是'require_once'? functions.php必須包含在index.php之前,你可以調用函數function.php – CharlesEF

+0

@CharlesEF啊hhh我看我讓我檢查 – MikeL5799

+0

你有沒有解決這個問題? – CharlesEF

回答

0

好了,所以我不得不做一些調整,以獲得元到functions.php中顯示我添加這些行: $pagemain = is_page();

然後:

if ($pagemain == is_page(35393) ) { 
    $meta[] = array(
     'id'   => 'imageupload', 
     'post_types' => array('page'), 
     'context' => 'normal', 
     'priority' => 'high', 
     'title' => __('Image Gallery', 'min'), 
     'fields' => array(
      array(
       'name' => __('Show', 'min'), 
       'id' => "{$prefix}_gallery-show", 
       'desc' => __('', 'meta-box'), 
       'type' => 'checkbox', 
       'clone' => false, 
      ), 
      array(
       'id'    => "{$prefix}_image_advanced", 
       'name'    => __('Image Advanced', 'min'), 
       'type'    => 'image_advanced', 
       // Delete image from Media Library when remove it from post meta? 
       // Note: it might affect other posts if you use same image for multiple posts 
       'force_delete'  => false, 
       // Maximum image uploads 
       //'max_file_uploads' => 2, 
      ), 
     ), 
    ); 
} 
0

試着更詳細地描述你的index.php有什麼問題?加載input.php或空白頁面時是否有輸出?

正確定義ABSPATH?如果沒有,您的腳本將從一開始退出。

對於流過腳本的更具體的認識,嘗試寫有一些echo

例如

<div class="row"> 
    <?php echo "Before calling min_get_page_gallery()" ?> 
    <?php min_get_page_gallery(); ?> 
    <?php echo "After calling min_get_page_gallery()" ?> 
</div> 

然後看看,如果你可以在調用所需函數之前和之後看到來自echo的消息。

+0

謝謝馮橡樹。它打印這2條消息很好,但沒有畫廊。 – MikeL5799

+0

index.php實際上效果很好,只是對圖庫函數的調用不起作用。 – MikeL5799

+0

您是否啓用了錯誤報告? '的error_reporting(E_ALL); ini_set(「display_errors」,1);'出於測試目的,將這些行添加到腳本的開頭。 index.php文件中的 –