wordpress hero invalid argument supplied for foreach() - php

I've noticed that the div image-16-8 from my hero.php file is not being displayed.
When I inspect element I get Invalid argument supplied for foreach() in
Here is the code:
<ul data-simple-slider>
<?php foreach ($images as $image): ?>
<li>
<div class="image-16-8" style="background-image: url(<?= $image->url; ?>); "></div>
</li>
<?php endforeach; ?>
</ul>
I've read a similiar question in which adding an if condition before foreach seems to fix the problem.
I'm not sure how to do this as I'm a beginner in php.
Any help is greatly appreciated!
Update Here is the full hero div -
<?php
function tbhHeroShortcode($atts)
{
$values = shortcode_atts(array(
'images' => '',
'first-line' => '',
'second-line' => '',
'video' => '',
'link' => '',
), $atts);
ob_start();
?>
<div class="hero">
<?
$images = decode_shortcode_data($values['images']);
if ($images): ?>
<ul data-simple-slider>
<?php foreach ($images as $image): ?>
<li>
<div class="image-16-8" style="background-image: url(<?= $image->url; ?>); "></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<div class="hero-content">
<div class="hero-content-first-line">
<h1 class="header"><?= decode_shortcode_data($values['first-line']) ?></h1>
</div>
<h1 class="italic-header"><?= decode_shortcode_data($values['second-line']) ?></h1>
<div class="hero-content-cta">
<a class="hollow-button" href="<?= decode_shortcode_data($values['link']) ?>">Learn More</a>
</div>
</div>
<?php if (count($images) > 1): ?>
<div class="hero-controls">
<i class="fa fa-chevron-left hero-controls__left" aria-hidden="true"></i>
<i class="fa fa-chevron-right hero-controls__right" aria-hidden="true"></i>
</div>
<?php endif; ?>
</div>
<?php
$component = ob_get_contents();
ob_end_clean();
return $component;
}
add_shortcode('tbhHero', 'tbhHeroShortcode');

check if the $images is_iterable
if(is_iterable($images)) {
// foreach gets indented here
}
Also see the manual page for is_iterable http://php.net/manual/en/function.is-iterable.php
A nice polyfill for PHP5 users (let's hope they are not on <5.4)
<?php
if(!function_exists('is_iterable')) {
function is_iterable($var) {
return ( is_array( $var ) || ( $var instanceof Traversable ) );
}
}

Related

PHP remove a condition in a foreach

I'm trying to edit a WordPress plugin which sorts the streams from the platform.
I just want to show any stream from the ID.
This is the link to the page controller (bootstrap)
<?php
$streamHeading = get_option('pixiehuge-stream-heading', 'Our streams');
$streamsection = get_option('pixiehuge-stream-section-enable');
$streams = pixiehuge_streams();
global $sectionNum;
?>
<?php if($streamsection && !empty($streams)): ?>
<section id="streams"<?php echo (!empty($sectionNum) && $sectionNum == 1) ? ' class="firstWithBg"' : '' ?>>
<?php
$sList = [
'twitch' => pixiehuge_streams(false, 'twitch'),
'youtube' => pixiehuge_streams(false, 'youtube'),
'mixer' => pixiehuge_streams(false, 'mixer'),
];
$active = false;
?>
<div class="container">
<div class="section-header">
<article class="topbar">
<h3>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="7px" height="8px"><path fill-rule="evenodd" fill="rgb(57, 191, 253)" d="M-0.000,0.435 C-0.000,0.805 -0.000,7.292 -0.000,7.546 C-0.000,7.877 0.338,8.123 0.672,7.930 C0.940,7.775 6.293,4.649 6.750,4.381 C7.050,4.205 7.045,3.786 6.750,3.611 C6.421,3.415 1.048,0.272 0.658,0.054 C0.373,-0.106 -0.000,0.071 -0.000,0.435 Z"></path></svg>
<?php echo esc_attr($streamHeading); ?>
</h3>
</article>
<!-- /TOP-BAR -->
<article class="bottombar">
<ul>
<?php foreach($sList as $id => $items): ?>
<?php if(!empty($items)):?>
<li<?php echo !$active ? ' class="active"' : '' ?>>
<?php echo esc_attr($id) ?>
</li>
<?php
if(!$active) {
$active = $id;
}
endif; // If not empty ?>
<?php endforeach; // Get categories ?>
</ul>
</article>
<!-- /BOTTOM-BAR -->
</div>
<!-- /SECTION-HEADER -->
<div class="tab-content content">
<?php foreach($sList as $id => $items): ?>
<?php if(!empty($items)): $i = 1; ?>
<div id="<?php echo esc_attr($id) ?>" class="list<?php echo ($active == $id) ? ' active' : '' ?>">
<?php foreach($items as $item): ?>
<?php if($i == 1): ?>
<div class="left">
<article class="streamBox large" style="background-image: url('<?php echo esc_url($item['thumbnail']) ?>');">
<a href="<?php echo esc_url(get_home_url(null, 'stream/' . $item['slug'])) ?>" class="playBtn">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10px" height="12px"><path fill-rule="evenodd" fill="rgb(255, 255, 255)" d="M10.000,6.000 C10.000,6.412 9.789,6.773 9.473,6.968 L9.474,6.968 L1.629,11.835 C1.626,11.837 1.623,11.839 1.620,11.841 L1.616,11.843 L1.616,11.843 C1.457,11.942 1.270,12.000 1.071,12.000 C0.480,12.000 -0.000,11.496 -0.000,10.875 L-0.000,10.875 L-0.000,1.124 L-0.000,1.124 C-0.000,0.503 0.480,-0.000 1.071,-0.000 C1.270,-0.000 1.457,0.057 1.616,0.156 L1.616,0.156 L1.620,0.159 C1.623,0.160 1.626,0.162 1.629,0.164 L9.474,5.031 L9.473,5.031 C9.789,5.227 10.000,5.587 10.000,6.000 Z"></path></svg>
</a>
<div class="details">
<span class="stream <?php echo esc_attr($id) ?>"><?php echo esc_attr($id) ?></span>
<?php echo esc_attr($item['title']) ?>
<h6><?php echo esc_attr($item['author']) ?></h6>
</div>
</article>
</div>
<!-- /LEFT -->
<?php else: // Large thumbnail ?>
<?php if($i == 2): ?>
<div class="right">
<?php endif; ?>
<?php if($i == 4): ?>
<div class="small">
<?php endif; ?>
<article class="streamBox" style="background-image: url('<?php echo esc_url($item['thumbnail']) ?>');">
<div class="details on-hover">
<?php echo esc_attr($item['title']) ?>
<a href="<?php echo esc_url(get_home_url(null, 'stream/' . $item['slug'])) ?>" class="cta-btn">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="9px" height="8px"><path fill-rule="evenodd" fill="rgb(255, 255, 255)" d="M4.688,0.182 C4.437,0.442 4.437,0.865 4.688,1.126 L6.805,3.326 L0.643,3.326 C0.288,3.326 -0.000,3.625 -0.000,3.993 C-0.000,4.362 0.288,4.661 0.643,4.661 L6.805,4.661 L4.688,6.861 C4.437,7.122 4.437,7.544 4.688,7.805 C4.939,8.066 5.346,8.066 5.597,7.805 L8.811,4.466 C8.928,4.345 9.000,4.178 9.000,3.993 C9.000,3.809 8.928,3.642 8.811,3.521 L5.597,0.182 C5.346,-0.079 4.939,-0.079 4.688,0.182 Z"></path>
</svg>
</a>
</div>
</article>
<?php if($i == count($items) || $i == 3): ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php $i++; endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<!-- /CONTENT -->
</div>
<!-- /CONTAINER -->
</section>
<?php endif; ?>
I don't like PHP Object so I'm quite lost. I know I have to remove $sList Lines 12 > 16 since I don't want to sort it from Youtube...
But then I don't know how to edit the foreach line 51 foreach($sList as $id => $items) to do a foreach elements ID with no conditions anymore. Just a foreach of any elements in the TABLE.
I tried to do that from scratch:
line 51
<div class="tab-content content">
<?php foreach($stream as $streams){ ?>
<p><?php echo $stream['title']; ?></p>
<?php } ?>
</div>
But it didn't work... And I can't tell you the error
EDIT
This is working but I can't order it by ID DESC and limit it to 4 results...
<div class="tab-content content">
<?php foreach($streams as $key => $stream): ?>
<p><?php echo $stream['title']; ?></p>
<?php endforeach; ?>
</div>
Thank you for your help
This is working "fine"
<div class="tab-content content">
<?php foreach($streams as $key => $stream): ?>
<p><?php echo $stream['title']; ?></p>
<?php endforeach; ?>
</div>

How to split Contao variable into several HTML Elements

I´ve got this template from my Contao CMS:
<?php $helper = new \ContaoBootstrap\Navbar\Helper\NavigationHelper($this); ?>
<?php foreach ($this->items as $item) : ?>
<?php
$currentLevelStringParts = explode('_', $this->level);
$currentLevelNumber = $currentLevelStringParts[1];
?>
<?php $itemHelper = $helper->getItemHelper($item); ?>
<li class="<?php echo str_replace(array('_'), array('-'), $itemHelper->getItemClass());
if (!empty($item['subitems'])) {echo ' subnav';} ?>
">
<<?= str_replace(array('strong'), array('a'), $itemHelper->getTag()); ?>
<?= str_replace(array('dropdown-item', 'dropdown-toggle', 'nav-link', 'data-toggle="dropdown"'), array(' ', ' ', ' ', ' '), $itemHelper) ?>>
<?php if ($item['isActive']) : ?>
<?= $item['link']?>
<?php else : ?>
<span itemprop="name"><?= $item['link']?></span>
<?php endif; ?>
</<?= str_replace(array('strong'), array('a'), $itemHelper->getTag()); ?>>
<?php if (!empty($item['subitems'])) { ?>
<span class="subnav-pull-down hidden-lg-up <?php if ($item['isTrail']) : ?>active<?php endif; ?>"></span>
<div class="subnav-container <?php if ($item['isTrail']) : ?>open<?php endif; ?>">
<div class="relative">
<ul class="nav sub-nav level-<?php echo (int)$currentLevelNumber+1 ?>">
<?= $item['subitems'] ?>
<div class="clear"></div>
</ul>
</div>
</div>
<?php } ?>
</li>
<?php endforeach; ?>
My problem is, I need a HTML structure like:
<ul>
<li><a></a></li>
<li><a></a></li>
<li>
<ul>
<li><a></a></li>
</ul>
</ul>
To change link () and description for my navigation. I tried to work the the bootstrap API but I´m kind of new to PHP and Bootstrap.
I hope you guys can help me.

Wordpress Facebook Album Plugin

I am currently using a Wordpress plugin called Facebook Page Album and below is the example code for showing the Facebook page albums list and I'm trying to convert it into Wordpress short code but I'm not sure how I should go about it as I'm not to familiar with Wordpress but I can only seem to use the Return statement so how would I go about using the following code with a short code.
<?php
$list = facebook_page_albums_get_album_list();
?>
<ol class="album-list">
<?php foreach ($list as $item) : ?>
<?php
if ($item['type'] != 'normal' || $item['name'] == 'Cover Photos') continue;
?>
<li class="album">
<?php if ($thumb = $item['cover_photo_data']):?>
<div class="album-thumb">
<a href="<?php echo add_query_arg('id', $item['id']);?>">
<img src="<?php echo $thumb['picture'];?>"/>
</a>
</div>
<?php endif; ?>
<div class="album-info">
<h5><?php echo $item['name'];?></h5>
<div class="counts">
<div class="photos-count"><?php echo $item['count'];?></div>
<?php if (!empty($thumb['comments'])) :?><div class="comments-count"><?php echo count($thumb['comments']['data']);?></div><?php endif;?>
<?php if (!empty($thumb['likes'])) :?><div class="likes-count"><?php echo count($thumb['likes']['data']);?></div><?php endif;?>
</div>
</div>
</li>
<?php endforeach;?>
</ol>

Trying to pass images from a model/helper file to a slideshow in CodeIgniter using a an array and foreach

I have a controller pages.php:
class Pages extends CI_Controller {
public function index($page = 'home')
{
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->lang->load('common/menu.php');
$this->lang->load('pages/'.$page.'.php');
$data = array();
$this->load->helper('slideshow');
$data['slideshow'] = get_image_array();
$this->load->view('templates/common/header', $data);
$this->load->view('modules/slideshow', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/common/footer', $data);
}
}
My slideshow_helper file:
function get_image_array(){
$images = array(img("slideshow/desktop/home1.jpg", "one"),img("slideshow/desktop/home2.jpg", "two"),img("slideshow/desktop/home3.jpg", "three"));
$captions = array("first image", "second image", "third image");
return array_combine($images, $captions);
}
And the slideshow view:
<div class="container">
<div class="row">
<div class="col-xs-12">
<div id="carousel-desktop" class="carousel slide visible-md visible-lg">
<ol class="carousel-indicators">
<li data-target="#carousel-desktop" data-slide-to="0" class="active"></li>
<li data-target="#carousel-desktop" data-slide-to="1"></li>
<li data-target="#carousel-desktop" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<?php $i = 1; ?>
<?php foreach ($image as $imageSrc => $caption ):?>
<?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<?php echo $image; if($caption != ""){ ?>
<div class="carousel-caption">
<?php echo $caption;?>
</div>
<?php } ?>
</div>
<?php $i++; ?>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#carousel-desktop" data-slide="prev">
<span class="icon-chevron-sign-left"></span>
</a>
<a class="right carousel-control" href="#carousel-desktop" data-slide="next">
<span class="icon-chevron-sign-right"></span>
</a>
</div>
<br />
</div>
</div>
</div>
<br />
</div>
Unfortunately this does not work am I missing something? I get the following error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: image
Filename: pages/home.php
-AND-
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: pages/home.php
Or is it completely wrong? I should point out that this site will not have access to a database.
When I have the array in the view file it works.
What I am trying to do is have a different model/helper file for each page as each page of this controller will have a slideshow but each with different images. So rather than recreating the view part countless times I want to simply have a new array for each page. I was thinking of using something like:
$this->load->helper('pages/'.$page);
-or-
$this->load->model('pages/'.$page.'.php');
so that each page has its own model/helper filer but obviously as I cannot get the first bit working I have no idea if this second bit will work.
Any help will be very much appreciated
You store images in $data['slideshow']; index, right? So simply use $slideshow in your view:
<?php foreach ($slideshow as $imageSrc => $caption ): ?>

PHP while loop, display in pairs of two

In Wordpress, I am creating a slider to slide through some content that the user specify. I am using ACF (advanced custom fields) if anyone is familiar with that, however what I am trying to accomplish is to show two content items at a time, while sliding through the jQuery slider.
Here is my loop:
<?php while(has_sub_field('popular_topic')): ?>
<li>
<div class="slide">
<img src="<?php the_sub_field('popular_topic_image'); ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
echo '<p>'.$newContent.'</p>';
?>
<a class="more-arrow" href="<?php the_sub_field('popular_topic_link'); ?>">Read More</a>
</div>
</div>
</li>
<?php endwhile; ?>
This is currently working, however it only shows one slide. I want it to show two slides and a time. Is there something I can do with a counter? Does this make sense?
my syntax might be a little off but maybe give this a shot?
<?php $i = 0; ?>
<?php while(has_sub_field('popular_topic')): ?>
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
$contentVar[$i] = array (
'img' => the_sub_field('popular_topic_image'),
'title' => $newContent,
'link' => the_sub_field('popular_topic_link')
);
$i++;
?>
<?php endwhile; ?>
<?php $j = 0; ?>
<?php while ($j < $i) : ?>
<li>
<div class="slide">
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php $j++; ?>
<?php if ($j <= $i) : ?>
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php endif; ?>
</div>
</li>
<?php $j++; ?>
<?php endwhile; ?>

Categories