I have a section of a website that uses a lot of custom fields for images. I also have a slider that uses those fields so for every image there is a line of code using that custom field. I tried using a loop but something is not right as it doesn't get any of the fields then.
This is how it is right now:
<! -- Variables -->
<?php $image1 = get_field('img-1'); ?>
<?php $image2 = get_field('img-2'); ?>
<?php $image3 = get_field('img-3'); ?>
<?php $image4 = get_field('img-4'); ?>
<?php $image5 = get_field('img-5'); ?>
<?php $image6 = get_field('img-6'); ?>
<?php $image7 = get_field('img-7'); ?>
<?php $image8 = get_field('img-8'); ?>
<?php $image9 = get_field('img-9'); ?>
<?php $image10 = get_field('img-10'); ?>
<!-- Slider -->
<div id="slider-constructora" class="flexslider">
<ul class="slides"> <?php if( $image1 ): ?>
<li><img class="img-responsive" src="<?php echo
<?php if( $image1 ): ?>
<li><img class="img-responsive" src="<?php echo $image1['url']; ?>"></li>
<?php endif; ?>
<?php if( $image2 ): ?>
<li><img class="img-responsive" src="<?php echo $image2['url']; ?>"></li>
<?php endif; ?>
<?php if( $image3 ): ?>
<li><img class="img-responsive" src="<?php echo $image3['url']; ?>"></li>
<?php endif; ?>
<?php if( $image4 ): ?>
<li><img class="img-responsive" src="<?php echo $image4['url']; ?>"></li>
<?php endif; ?>
<?php if( $image5 ): ?>
<li><img class="img-responsive" src="<?php echo $image5['url']; ?>"></li>
<?php endif; ?>
<?php if( $image6 ): ?>
<li><img class="img-responsive" src="<?php echo $image6['url']; ?>"></li>
<?php endif; ?>
<?php if( $image7 ): ?>
<li><img class="img-responsive" src="<?php echo $image7['url']; ?>"></li>
<?php endif; ?>
<?php if( $image8 ): ?>
<li><img class="img-responsive" src="<?php echo $image8['url']; ?>"></li>
<?php endif; ?>
<?php if( $image9 ): ?>
<li><img class="img-responsive" src="<?php echo $image9['url']; ?>"></li>
<?php endif; ?>
<?php if( $image10 ): ?>
<li><img class="img-responsive" src="<?php echo $image10['url']; ?>"></li>
<?php endif; ?>
</ul>
</div><!-- slider -->
This is what I have tried to use inside of the slider without success, I use it on another website and it does work, but not in this case:
<?php
for( $iteration = 1; $iteration <= 150; $iteration++){
if ( get_field("image".$iteration ) ){ ?>
<li><img class="img-responsive" src="<?php echo get_field("image".$iteration)['url'] ?>"></li>
<?php } ?>
<?php } ?>
It looks like you have a syntax error. Note this part of the code:
<li><img class="img-responsive" src="<?php echo
<?php if( $image1 ): ?>
You did not finish your echo or close your PHP tag before opening a new PHP tag and starting an IF statement. This will throw a fatal error.
I suggest enabling WP_DEBUG so that it is easier to catch these types of errors. To do this change or add define( 'WP_DEBUG', true ); so that it is true. Debugging in WordPress (Codex)
Related
I have some code here that outputs the following:
Essentially I want to use same page anchor tags so a user can click on the small logo and be taken to the larger logo and info.
As it's a Wordpress site, I have used the ACF repeater field to achieve this. This repeater field enables the user in the back end to add more clients, for each client they can add an image a company name and the paragraph text.
Then I have just repeated the repeater field above and shown only the images but made them much smaller.
As you will see in the code below, I have assigned around each smaller photo and then this: <a name="anchor1"></a> just above every larger photo..
But I need a way of the numbers counting up so when they come out they aren't all anchor1 they become anchor2, anchor3 and so on.
Any ideas?
<div class="container client-page-logos-small" >
<div class="row">
<h3>Click company to see more</h3>
<?php if( have_rows('client_page_logos', 123456) ): ?>
<ul class="client-page-logos-small">
<?php while( have_rows('client_page_logos', 123456) ): the_row();
// vars
$logo = get_sub_field('client_page_logo');
?>
<a href="#anchor1">
<li class="client-page-logos-small">
<img src="<?php echo $logo['url']; ?>" alt="<?php echo $logo['alt'] ?>" />
</li>
</a>
<?php endwhile; ?>
</ul>
<div style="clear: both;"></div>
<?php endif; ?>
<hr>
</div>
</div>
<div class="container client-page-logos" >
<div class="row">
<?php if( have_rows('client_page_logos', 123456) ): ?>
<ul class="client-page-logos">
<?php while( have_rows('client_page_logos', 123456) ): the_row();
// vars
$logo = get_sub_field('client_page_logo');
$name = get_sub_field('client_name');
$text = get_sub_field('client_text');
?>
<li class="client-page-logos">
<a name="anchor1"></a>
<img src="<?php echo $logo['url']; ?>" alt="<?php echo $logo['alt'] ?>" />
<h3><?php echo $name; ?></h3>
<p><?php echo $text; ?></p>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</div>
</div>
You need to add counter like below:-
<?php
$i = 1;
while( have_rows('client_page_logos', 123456) ): the_row();
// vars
$logo = get_sub_field('client_page_logo');
?>
<a href="#anchor<?php echo $i;?>">
<li class="client-page-logos-small">
<img src="<?php echo $logo['url']; ?>" alt="<?php echo $logo['alt'] ?>" />
</li>
</a>
<?php $i++;endwhile; ?>
And
<?php
$j = 1;
while( have_rows('client_page_logos', 123456) ): the_row();
// vars
$logo = get_sub_field('client_page_logo');
$name = get_sub_field('client_name');
$text = get_sub_field('client_text');
?>
<li class="client-page-logos">
<a name="anchor<?php echo $j;?>"></a>
<img src="<?php echo $logo['url']; ?>" alt="<?php echo $logo['alt'] ?>" />
<h3><?php echo $name; ?></h3>
<p><?php echo $text; ?></p>
</li>
<?php $j++ ;endwhile; ?>
I have three separate fields in acf one for each image named slide_one, slide_two and slide_three. Now when I try to get the images to display using the code below nothing is being output. I am doing it this why and not using a loop like you usually as I'm wanting to have a separate text overlay for each image that I will have sliding in separately. This is the first step and it won't work. What an I doing wrong?
<div class="slider">
<ul class="slides">
<?php
$image_one = get_field('slide_one');
if( !empty($image_one ): ?>
<li>
<img src="<?php echo $image_one['url']; ?>" alt="<?php echo $image_one['alt']; ?>" />
</li>
<?php endif; ?>
<?php
$image_two = get_field('slide_two');
if( !empty($image_two ): ?>
<li>
<img src="<?php echo $image_two['url']; ?>" alt="<?php echo $image_two['alt']; ?>" />
</li>
<?php endif; ?>
<?php
$image_three = get_field('slide_three');
if( !empty($image_three ): ?>
<li>
<img src="<?php echo $image_three['url']; ?>" alt="<?php echo $image_three['alt']; ?>" />
</li>
<?php endif; ?>
</ul>
</div>
I am struggling to retrieve the intro image of articles, within a custom module that echoes article titles with the selected tags.
But it seems as if $item->images doesn't recall the image info. My code is the following:
Can anyone help me?
<?php
defined('_JEXEC') or die;
$images = json_decode($item->images);
?>
<?php JLoader::register('TagsHelperRoute', JPATH_BASE . '/components/com_tags/helpers/route.php'); ?>
<div class="tagsselected<?php echo $moduleclass_sfx; ?>">
<?php if ($list) : ?>
<ul>
<?php foreach ($list as $i => $item) : ?>
<li>
<?php $item->route = new JHelperRoute; ?>
<a href="<?php echo JRoute::_(TagsHelperRoute::getItemRoute($item->content_item_id, $item->core_alias, $item->core_catid, $item->core_language, $item->type_alias, $item->router)); ?>">
<?php if (!empty($item->core_title)) :
echo htmlspecialchars($item->core_title);
endif; ?>
</a>
<img src="<?php echo $images->image_intro; ?>" alt="<?php echo htmlspecialchars($item->title); ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<span><?php echo JText::_('MOD_TAGS_SIMILAR_NO_MATCHING_TAGS'); ?></span>
<?php endif; ?>
</div>
Try to get Article images in for loop
<?php foreach ($list as $i => $item) :
// images for each article
$images = json_decode($item->images); ?>
<li>
// your code/ other stuff
// display image
<img src="<?php echo $images->image_intro; ?>" alt="<?php echo htmlspecialchars($item->title); ?>" />
</li>
<?php endforeach; ?>
<li class="tag_title_custom">
<?php $item->route = new JHelperRoute; ?>
<a href="<?php echo JRoute::_(TagsHelperRoute::getItemRoute($item->content_item_id, $item->core_alias, $item->core_catid, $item->core_language, $item->type_alias, $item->router)); ?>">
<?php if (!empty($item->core_title)) :
echo htmlspecialchars($item->core_title);
endif; ?>
<?php $images = json_decode($item->core_images);?>
<img src="<?php echo htmlspecialchars($images->image_intro);?>" alt="<?php echo htmlspecialchars($images->image_intro_alt); ?>">
</a>
</li>
I am trying to build a Flexible Content which contains 4 possible Layouts (a description, a hero image, a gallery 1col and a gallery 2col). So far I can get the Description and Hero Image subfields to display just fine but the Gallery subfield wont show up on the page. Don't know what I'm doing wrong. Here's the code:
<?php
while(the_flexible_field("flexible_content")): ?>
<?php if(get_row_layout() == "description"): // layout: Description ?>
<div class="proy-desc">
<h2><?php the_sub_field("text"); ?></h2>
</div>
<?php elseif(get_row_layout() == "hero_image"): // layout: Hero Image ?>
<div class="proy-img">
<?php
$image = get_sub_field('image');
elseif( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php ?>
</div>
<?php elseif(get_row_layout() == "gallery_50p"): // layout: Gallery 50p ?>
<div class="gallery">
<?php $images = get_sub_field('gallery_image_50p');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php ?>
</div>
<?php endif; ?>
<?php elseif(get_row_layout() == "gallery_100p"): // layout: Gallery 50p ?>
<div class="gallery">
<?php $images = get_sub_field('gallery_image_100p');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php ?>
</div>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
You did not close your if statement. See below:
<?php elseif(get_row_layout() == "gallery_50p"): // layout: Gallery 50p ?>
<div class="gallery">
<?php $images = get_sub_field('gallery_image_50p');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php ?>
</div>
<?php endif; ?>
Change your section to this:
<?php elseif(get_row_layout() == "gallery_50p"): // layout: Gallery 50p ?>
<div class="gallery">
<?php $images = get_sub_field('gallery_image_50p');
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
Your endif; was in the wrong spot. Everything else looks right, so give this a try.
Can someone explain me how to add an incrementing to class?
I would like to have:
div class="slide1"
div class="slide2"
div class="slide3"
...
Here's the code:
<?php if( have_rows('repeater_field_name') ): ?>
<ul class="slides">
<?php while( have_rows('afbeeldingen') ): the_row();
// vars
$image = get_sub_field('afbeelding');
?>
<li class="slide<?php echo $i; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Use the increment operator...
<?php $i = 0; /* define $i ... */ while( have_rows('afbeeldingen') ): the_row();
// vars
$image = get_sub_field('afbeelding');
?>
<li class="slide<?php echo $++i; // increment it! ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</li>
<?php endwhile; ?>
You are not incrementing your counter.
<?php if( have_rows('repeater_field_name') ): ?>
<ul class="slides">
<?php while( have_rows('afbeeldingen') ): the_row();
// vars
$image = get_sub_field('afbeelding');
?>
<li class="slide<?php echo $i++; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
You need to define $i outside the while loop, something like $i = 0;. Then inside the loop increment $i after each loop iteration with something like $i++.