0

I have a function I wrote in my functions.php page for a gallery to display on certain pages. It displays on custom templates, but now I need it to display on index.php Here is the code from my functions.php file:

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

}

That code works like a charm on other page templates, such as awards.php . Here is where I call it as min_get_page_gallery(); in awards.php where it works flawlessly:

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

Now finally, I try to add the same function call of min_get_page_gallery(); in my index.php file like this:

    <?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>

Is there something I'm missing??

  • Is the beginning of the code missing from the question? – Andy Macaulay-Brook Jul 27 '16 at 16:31
  • No I took the chunk of code from functions.php that initiates the function for the gallery – MikeL5799 Jul 27 '16 at 16:36
  • Have a look at the top of your question. There seems to be a code fragment within a sentence and your function doesn't begin function . – Andy Macaulay-Brook Jul 27 '16 at 16:37
  • Yes I just saw that someone made an suggested edit and it went through. It should be good now – MikeL5799 Jul 27 '16 at 16:38
  • Starting with the obvious things to tick off... Is the min_gallery-show custom field truthy on the page you're testing? Is there a problem with whitespace at the start of index.php? – Andy Macaulay-Brook Jul 27 '16 at 16:48
0

I think you're misunderstanding the function get_page_template_slug

When you call $template = get_page_template_slug( $post_id ); it won't return index.php if you are using the default theme template. It only works when viewing Pages and will either return the slug of the Custom Page Template assigned to that Page or an empty string otherwise.

So if ( 'index.php' == $template ) will always be false.

Rather than trying to detect which template file is running, use WP's is_ functions.

true == is_front_page()

on your site's home page, for example.

There is a whole range of these functions to check the sort of page you are on. Have a read up on them in the documentation available online. If really feel the need to check whether you are using the index.php template then you could set a global flag variable at the start of that template and check it within your function, I suppose.

  • Thanks so how would I get it to call the default template as well? or would I have to assign another variable to index.php? – MikeL5799 Jul 27 '16 at 18:36
  • @MikeL5799 see my addition to the answer. – Andy Macaulay-Brook Jul 27 '16 at 19:12
  • I don't think is_front_page is going to work, being that this page just uses index.php, but isn't the front page. – MikeL5799 Jul 27 '16 at 19:23
  • I did say "for example." I've added some elaboration. – Andy Macaulay-Brook Jul 27 '16 at 19:32
  • Thanks so much so I did this: if ( is_page(35393) == true ) or should I have true first? – MikeL5799 Jul 27 '16 at 20:00
0

Ok, so I had to do some tweaking to get the meta to show in functions.php I added these lines: $pagemain = is_page();

and then:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.