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

时间:2016-07-27 作者:MikeL5799

我在函数中写了一个函数。用于在特定页面上显示库的php页面。它显示在自定义模板上,但现在我需要它显示在索引上。php这里是我函数中的代码。php文件:

if ( \'templates/awards.php\' == $template ||  \'templates/events.php\' == $template ) {
    $meta[] = array(
        \'id\'         => \'imageupload\',
        \'post_types\' => array( \'page\', $post_type),
        \'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,
            ),
        ),
    );
}

if ( \'index.php\' == $template ) {
    $meta[] = array(
        \'id\'         => \'imageupload\',
        \'post_types\' => array( \'page\', $post_type),
        \'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,
            ),
        ),
    );
}







          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);

        echo \'<div class="gallery_inner">\';
        for ( $i = 0; $i < $count; $i++) {
            if ( $i % 3 == 0 ) {

                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;
  }

}
该代码在其他页面模板(如awards)上的效果非常好。php。这里我称之为min\\u get\\u page\\u gallery();在奖项中。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\\u get\\u page\\u gallery()相同的函数调用;在我的索引中。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 个回复
最合适的回答,由SO网友:Andy Macaulay-Brook 整理而成

我想你误解了功能get_page_template_slug

当你打电话的时候$template = get_page_template_slug( $post_id ); 它不会再回来了index.php 如果您使用的是默认主题模板。它仅在查看页面时起作用,并将返回指定给该页面的自定义页面模板的slug,否则返回空字符串。

所以if ( \'index.php\' == $template ) 永远都是假的。

与其尝试检测哪个模板文件正在运行,不如使用WP的is_u函数。

true == is_front_page()
例如,在您网站的主页上。

有一整套这些函数可以检查您所处的页面的类型。请阅读在线文档中的相关内容。如果真的觉得有必要检查你是否在使用索引。php模板然后您可以在模板的开头设置一个全局标志变量,并在您的函数中进行检查。

SO网友:MikeL5799

好的,所以我不得不做一些调整,让meta在函数中显示出来。php I添加了以下行:$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,
            ),
        ),
    );
}