truongwp/gallery-meta-box

Foreach error

Closed this issue · 18 comments

Hello, excellent plug in, but it gives me foreach error Invalid argument supplied for foreach() in.
I have the 4.8.3 wp.

Hi @Phaidonas,

Can you give me the full error message?

Thanks.

yeah, the error is Warning: Invalid argument supplied for foreach() in E:\xampp\htd
and it gives no images.
The code that the error is:

<div class="entry-content">
		<?php
			the_content( sprintf(
				wp_kses(
					/* translators: %s: Name of current post. Only visible to screen readers */
					__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'vrettosstudio_2017' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				get_the_title()
			) );

			wp_link_pages( array(
				'before' => '<div class="page-links">' . esc_html__( 'Pages:', '_2017' ),
				'after'  => '</div>',
			) );
		?>


		<?php foreach ($images as $image) {   //<<<<<HERE
			echo wp_get_attachment_link($image, 'large'); 
			// echo wp_get_attachment_image($image, 'large');
		}?>

	</div><!-- .entry-content -->

Please make sure that you have this line:

$images = get_post_meta( get_the_ID(), 'truongwp_gallery', true );

If yes, please add var_dump( $images ) before the foreach loop to inspect $images variable.

where should be this line?
i have used the master, not the release.

You can place it before the foreach line, wrap it in php tags, in the code you gave me

yes i place it, but now the error is Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in

The code now is

		<?php


$images = get_post_meta( get_the_ID(), 'truongwp_gallery', true );
var_dump( $images )
		 foreach ($images as $image) {
			echo wp_get_attachment_link($image, 'large');
			// echo wp_get_attachment_image($image, 'large');
		}?>

Please add ; after var_dump( $images )

You can show the full content of that file. I can check it for you.

still no,
the error goes like that:

string(0) "" 
Warning: Invalid argument supplied for foreach() in

I have put images in the post
it is the content.php from underscore

Here is the file:

<?php
/**
 * Template part for displaying posts
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package _2017
 */

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<?php
		if ( is_singular() ) :
			the_title( '<h1 class="entry-title">', '</h1>' );
		else :
			the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
		endif;

		if ( 'post' === get_post_type() ) : ?>
		<div class="entry-meta">
			<?php _2017_posted_on(); ?>
		</div><!-- .entry-meta -->
		<?php
		endif; ?>
	</header><!-- .entry-header -->

	<div class="entry-content">
		<?php
			the_content( sprintf(
				wp_kses(
					/* translators: %s: Name of current post. Only visible to screen readers */
					__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'vrettosstudio_2017' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				get_the_title()
			) );

			wp_link_pages( array(
				'before' => '<div class="page-links">' . esc_html__( 'Pages:', ' _2017' ),
				'after'  => '</div>',
			) );
		?>

		<?php


$images = get_post_meta( get_the_ID(), 'truongwp_gallery', true );
var_dump( $images );
		 foreach ($images as $image) {
			echo wp_get_attachment_link($image, 'large');
			// echo wp_get_attachment_image($image, 'large');
		}?>

	</div><!-- .entry-content -->

	<footer class="entry-footer">
		<?php vrettosstudio_2017_entry_footer(); ?>
	</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->

Please try this and add some images to your post to check:

<?php
/**
 * Template part for displaying posts
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package _2017
 */

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
		<?php
		if ( is_singular() ) :
			the_title( '<h1 class="entry-title">', '</h1>' );
		else :
			the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
		endif;

		if ( 'post' === get_post_type() ) : ?>
		<div class="entry-meta">
			<?php _2017_posted_on(); ?>
		</div><!-- .entry-meta -->
		<?php
		endif; ?>
	</header><!-- .entry-header -->

	<div class="entry-content">
		<?php
			the_content( sprintf(
				wp_kses(
					/* translators: %s: Name of current post. Only visible to screen readers */
					__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'vrettosstudio_2017' ),
					array(
						'span' => array(
							'class' => array(),
						),
					)
				),
				get_the_title()
			) );

			wp_link_pages( array(
				'before' => '<div class="page-links">' . esc_html__( 'Pages:', ' _2017' ),
				'after'  => '</div>',
			) );
		?>

		<?php
		$images = get_post_meta( get_the_ID(), 'truongwp_gallery', true );
		if ( $images ) {
			foreach ( $images as $image ) {
				echo wp_get_attachment_link( $image, 'large' );
				// echo wp_get_attachment_image( $image, 'large' );
			}
		}
		?>

	</div><!-- .entry-content -->

	<footer class="entry-footer">
		<?php vrettosstudio_2017_entry_footer(); ?>
	</footer><!-- .entry-footer -->
</article><!-- #post-<?php the_ID(); ?> -->

I have tried that before, no there is no error, but neither images. yes i have images in my posts.

Have you customize some hook? Eg: gallery_meta_box_meta_key

I have put this code to functions.php

require_once 'gallery-metabox/gallery.php';

and i removed from the gallery.php the page and custom-post-type

  function add_gallery_metabox($post_type) {
    $types = array('post', 'page', 'custom-post-type');

    if (in_array($post_type, $types)) {
      add_meta_box(
        'gallery-metabox',
        'Gallery',
        'gallery_meta_callback',
        $post_type,
        'normal',
        'high'
      );
    }
  }

Something is strange here. You only need to include gallery-meta-box.php file and it will show in post by default.

https://github.com/truongwp/gallery-meta-box/blob/master/README.md

I think you are wrong. The one you mentioned here: https://github.com/zulfnore/gallery-metabox/blob/master/gallery.php

yeah, i realise it, sorry, i will try yours.Thank you for you patience and effort.

@Phaidonas No problem. Glad to help you :D