0

I'm beginner in php and wordpress and I'm trying convert html template to wordpress theme and i face this issue :

Warning: Trying to access array offset on value of type bool in E:\xampp\htdocs\consulty\wp-content\themes\consulty\template-parts\content-gallery.php on line 13

I got the php code from the link above the code for creating gallery post format in index.php page

<?php 
/**
 * -- Gallery Post Format
 * https://stackoverflow.com/questions/48535908/customizing-wordpress-gallery-post-format
 */

if ( has_post_format( 'gallery' ) ) {

?>
<div id="post-<?php the_ID(); ?>" <?php post_class('gallery'); ?>>
    <?php
        $gallery = get_post_gallery( $post, false );
        $att_ids = wp_parse_id_list( $gallery['ids'] ); //***This is line 13***
    ?>
    <div class="inner-box">
        
        <div id="post-gallery-<?php the_ID(); ?>"
            <?php post_class('news-carousel owl-carousel owl-theme owl-dots-none'); ?>>

            <?php 
            for ( $i = 0; $i < count( $gallery['src'] ); $i++ ) { 
                ?>
                <figure class="image-box">

                    <img class="" src="<?php echo esc_url( $gallery['src'][ $i ] ); ?>" />
                    
                    <span class="category"><?php the_category( ', ' ); ?></span>
                </figure>
                <?php } ?>

        </div><!-- /.news-carousel owl-carousel owl-theme owl-dots-none -->

        <div class="lower-content">
            <ul class="post-info clearfix">
                <li>
                    <i class="far fa-user"></i><a
                        href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a>
                </li>
                <li><i class="far fa-calendar-alt"></i><?php the_time('F jS, Y') ?></li>
                <li><i class="far fa-comment-alt"></i>
                    <?php //comments_popup_link(); 
                            comments_popup_link( 'No Comment', '1 Comment', '% Commentston', '', 'Comments Off' );
                            ?>
                </li>
                <li>
                    <i class="far fa-folder"></i>
                    <?php
                                                $categories     =   get_the_category();
                                                $separator      =   ', ';
                                                $output         =   '';

                                                if ( $categories ) {
                                                    foreach ( $categories as $category) {
                                                        $output .=  '<a href="' . get_category_link( $category->term_id ) . '">' . $category->cat_name . '</a>' . $separator;
                                                    }
                                                    echo trim($output, $separator);
                                                }
                                            ?>
                </li>
            </ul>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p>
                <?php the_excerpt(); ?>
            </p>
            <div class="read-more-link">
                <a href="<?php the_permalink(); ?>">
                    <i class="fas fa-arrow-right"></i>
                    <span><?php _e('Read More'); ?></span>
                </a>
            </div>
        </div><!-- /.lower-content -->
    </div><!-- /.inner-box -->
</div>
<?php } ?>

What this mean and how to fix it?

Thank you

Hatem Frere
  • 65
  • 1
  • 10
  • It is possible that you are trying to access a value that does not exist in your array, have you checked print_r($gallery) to see if the data is available or not? – Krunal Bhimajiyani Sep 25 '22 at 10:00
  • Thank you for response, I did what you advice me but nothing happens, The problem still found .. same error and the pagination and sidebar disappear – Hatem Frere Sep 25 '22 at 10:54
  • `if(isset($gallery['ids'])) { $att_ids = wp_parse_id_list( $gallery['ids'] ); }` try this for line-13. – Krunal Bhimajiyani Sep 25 '22 at 12:53
  • `$gallery` is false. Don't access a key and pass to a function before checking it. `$att_ids = isset($gallery['ids']) ? wp_parse_id_list($gallery['ids']) : [];`. – Markus Zeller Sep 25 '22 at 17:21
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-warning-undefined-arr) – Markus Zeller Sep 25 '22 at 17:26
  • @MarkusZeller, Unfortunately, nothing happened, same problem but the error line at the bottom has disappeared `
    > `
    – Hatem Frere Sep 26 '22 at 07:45
  • @KrunalBhimajiyani , Unfortunately, nothing happened, same problem but the error line at the bottom has disappeared – Hatem Frere Sep 26 '22 at 07:48
  • Of course nothing happens, when the `$gallery` is empty. – Markus Zeller Sep 26 '22 at 08:30
  • @MarkusZeller, So, what do you see as a solution to this problem? Please give me a code based on my code – Hatem Frere Sep 26 '22 at 08:42
  • @MarkusZeller, So, what do you see as a solution to this problem? Please give me a code based on my code,, Please be patient with me because I am a newbie in php and wordpress theme development. thank you in advance – Hatem Frere Sep 26 '22 at 08:51
  • @HatemFrere, `$gallery = get_post_gallery( $post, false );` Here, $post = your post id. Is your post-id coming or not? And where do you define the `$post` variable? – Krunal Bhimajiyani Sep 26 '22 at 09:35
  • @KrunalBhimajiyani,I got this php code from the above link in my code and I modified it based on my html code.. and it was working but this error occurred later so the error appears on the page where the gallery post is in the blog ... you can check HTML template Link in the question above , Please be patient with me – Hatem Frere Sep 26 '22 at 09:49

0 Answers0