调用PHP函数在index.php中不起作用

时间:2016-07-26 17:02:05

标签: php wordpress custom-wordpress-pages

我在我的functions.php页面中编写了一个函数,用于在某些页面上显示的库。它显示在自定义模板上,但现在我需要它在index.php上显示以下是我的functions.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>

有什么我想念的吗?

2 个答案:

答案 0 :(得分:0)

尝试更详细地描述您的index.php有什么问题?加载input.php或空白页时是否有输出?

是否正确定义ABSPATH?如果没有,您的脚本将在开头退出。

要更加具体地了解流程,请尝试在那里写一些echo

E.g。

<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的消息。

答案 1 :(得分: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,
            ),
        ),
    );
}