I would like some help In creating a loop for my WP template.
I have the same div block repeated a few times, the class numbers are different and that's about it.
based of the sample blocks bellow can someone give me a example of how i could wrap this in a loop and change the class numbers ?
code block
If you see bellow some of the class names just differ with the number
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_1') ?>"
alt="<?php the_field('section_3_image_1_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_1'); ?>"
title="<?php the_field('title_1'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_2') ?>"
alt="<?php the_field('section_3_image_2_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_two', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_2'); ?>"
title="<?php the_field('title_2'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_3') ?>"
alt="<?php the_field('section_3_image_3_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_3', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_3'); ?>"
title="<?php the_field('title_3'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<?pho $i=1; if( have_rows('parent_field') ):
while ( have_rows('parent_field') ) : the_row(); ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_' . $i) ?>"
alt="<?php echo get_field('section_3_image_' . $i . '_alt') ?>">
<h3 style="min-height: 150px"><?php echo get_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php echo get_field('link_' . $i); ?>"
title="<?php echo get_field('title_' . $i); ?>"
class="btn btn-lg btn-secondary">
<?php echo get_field('link_text'); ?>
</a>
</p>
</div>
<?php $i++; endwhile; endif; ?>
if you are working with while loop this will work fine.
This ACF field data and your while will work fine as this.
You can do with this code:
<?pho for($i = 0; $i < YOUR_MAX_VALUE; $i++): ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_' . ($i+1)) ?>"
alt="<?php the_field('section_3_image_' . ($i+1) . '_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_' . ($i+1)); ?>"
title="<?php the_field('title_' . ($i+1)); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<?php endfor; ?>
Remember to set YOUR_MAX_VALUE to the value you need.
if you are working with for loop this will work fine.
<?pho for($i = 0; $i < YOUR_VALUE.length; $i++): ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php
the_field('section_3_image_' . ($i+1)) ?>"
alt="<?php the_field('section_3_image_' . ($i+1) . '_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_' . ($i+1)); ?>"
title="<?php the_field('title_' . ($i+1)); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link'); ?>
</a>
</p>
</div>
Related
The images are not showing in front-end. ACF settings screenshot
<?php
if( have_rows('how_it_works_functions') ):
$counter = 0;
while( have_rows('how_it_works_functions') ) : the_row();
$counter++;
?>
<div class="step<?php echo $counter; ?>">
<div class="row section-content">
<?php if($counter == 2) { ?>
<div class="col-md-8 image">
<img class="skip-lazy" src="<?php get_sub_field('how_it_works_functions_image')['url']; ?>" alt="<?php get_sub_field('how_it_works_functions_image')['alt']; ?>">
</div>
<div class="col-md-4 text">
<h3 data-aos="fade-left" data-aos-offset="300" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_name'); ?></h3>
<p data-aos="fade-up" data-aos-offset="100" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_description'); ?></p>
</div>
<?php } else { ?>
<div class="col-md-4 text">
<h3 data-aos="fade-right" data-aos-offset="300" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_name'); ?></h3>
<p data-aos="fade-up" data-aos-offset="100" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_description'); ?></p>
</div>
<div class="col-md-8 image">
<img class="skip-lazy" src="<?php get_sub_field('how_it_works_functions_image')['url']; ?>" alt="<?php get_sub_field('how_it_works_functions_image')['alt']; ?>">
</div>
<?php } ?>
</div>
</div>
<?php
endwhile;
endif;
?>
I already check the var_dump and it is getting the correct url but I still have broken image in front-end.
You need to echo that field.
Like this based on your code:
<img class="skip-lazy" src="<?php echo get_sub_field( 'how_it_works_functions_image' )['url']; ?>" alt="<?php echo get_sub_field( 'how_it_works_functions_image' )['alt']; ?>">
Alternative effective way:
<?php
$image = get_sub_field( 'how_it_works_functions_image' );
?>
<img class="skip-lazy" src="<?php echo esc_attr( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>">
Try out the above solution and let me know if it works or not.
As title implies this code shows all the questions that i add to the loop/accordion but no matter which one i click on it only opens and closes the first and i cant tell why.
<div class="container">
<div class="row">
<div id="accordion" role="tablist" aria-multiselectable="false" class="py-4">
<?php
$counter = 0;
$loop = get_field('questions');
foreach($loop as $row) : ?>
<div class="card card-no-border card-no-shadow">
<div class="card-header" role="tab" id="heading<?php echo $counter++ ?>">
<h5 class="mb-0">
<a class="body2 uppercase bold" data-toggle="collapse" data-parent="#accordion"
href="#collapse<?php the_ID(); ?>"
aria-expanded="<?php echo $first; ?>" aria-controls="collapse<?php the_ID(); ?>">
<i class="fa fa-chevron-right" aria-hidden="true"></i>
<span style='padding-right: 20px;'></span>
<?php echo $row['question_title']?>
</a>
</h5>
</div>
<div id="collapse<?php the_ID(); ?>" class="collapse<?php if ($first) {
echo "show";
} ?>" role="tabpanel"
aria-labelledby="heading<?php the_ID(); ?>">
<div class="card-block body2">
<?php echo $row['answer'] ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
I would say you need to look at your id's
href="#collapse<?php the_ID(); ?>"
id="collapse<?php the_ID(); ?>"
the_id function is outputting the current page id not the id of each loop/question
You could use your counter instead
id="collapse<?php echo $counter; ?>"
I am using magento rwd theme with a extension - for selected product widget.
The problem I am not able to increase the size of the image.
the relevant code is like this:
<?php
$products = $this->getProductCollection();?>
<?php if ($products && $products->getSize()): ?>
<div class="block block-products selected-products">
<div class="block-title">
<strong><span><?php echo $this->getBlockName() ?></span></strong>
</div>
<div class="block-content">
<div class="products-block">
<?php foreach ($products->getItems() as $product): ?>
<?php /** #var $product Mage_Catalog_Model_Product */ ?>
<div class="item">
<a class="product-image"
href="<?php echo $product->getProductUrl() ?>"
title="<?php echo $this->stripTags($product->getName(), null, true) ?>">
<img
src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(300,400) ?>"
alt="<?php echo $this->stripTags($product->getName(), null, true) ?>" />
</a>
<div class="product-details">
<p class="product-name">
<a href="<?php echo $product->getProductUrl() ?>"
title="<?php echo $this->stripTags($product->getName(), null, true) ?>)">
<?php echo $this->helper('catalog/output')->productAttribute($product, $product->getName(), 'name') ?>
</a>
</p>
<?php if ($product->getIsSalable()): ?>
<div class="product-price">
<?php echo $this->getPriceHtml($product, true) ?>
</div>
<button type="button"
title="<?php echo $this->__('Add to Cart') ?>"
class="button btn-cart"
onclick="setLocation('<?php echo $this->getAddToCartUrl($product) ?>')">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
I have set the resize to 300x400, but I am getting it - 118x157
The website link for reference -
Change your Belief for Success
You have to set constrainOnly to true on the image helper before resizing:
$this->helper('catalog/image')
->init($product, 'image')
->constrainOnly(true)
->resize(300,400);
From the PHPDoc of the method:
Guarantee, that image picture will not be bigger, than it was.
Applicable before calling resize()
It is false by default
The query and the fetching are working properly, but it's too repetitive, I tried (applying some tutorials on for loops) but can't seem to figure out.
<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulos[0]['id']; ?>&gen=<?= $articulos[0]['genero']; ?>&id=<?= $articulos[0]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[0]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[0]['id']; ?>&gen=<?= $articulos[0]['genero']; ?>&id=<?= $articulos[0]['id'] ?>">
<?= $articulos[0]['titulo']; ?>
</a>
</a>
</div>
</div>
<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[1]['id']; ?>&gen=<?= $articulos[1]['genero']; ?>&id=<?= $articulos[1]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[1]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[1]['id']; ?>&gen=<?= $articulos[1]['genero']; ?>&id=<?= $articulos[1]['id'] ?>">
<?= $articulos[1]['titulo']; ?>
</a>
</a>
</div>
</div>
<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[2]['id']; ?>&gen=<?= $articulos[2]['genero']; ?>&id=<?= $articulos[2]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[2]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[2]['id']; ?>&gen=<?= $articulos[2]['genero']; ?>&id=<?= $articulos[2]['id'] ?>">
<?= $articulos[2]['titulo']; ?>
</a>
</a>
</div>
</div>
<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[3]['id']; ?>&gen=<?= $articulos[3]['genero']; ?>&id=<?= $articulos[3]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[3]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[3]['id']; ?>&gen=<?= $articulos[3]['genero']; ?>&id=<?= $articulos[3]['id'] ?>">
<?= $articulos[3]['titulo']; ?>
</a>
</a>
</div>
</div>
It's working OK but I want to make it lightweight.
Thank You for any suggestions.
It's kind of easy really:
<?php foreach ($articulos as $articulo) { ?>
<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulo['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<?= $articulo['titulo']; ?>
</a>
</a>
</div>
</div>
<?php } ?>
Thanks to Farkie the rest was easy, To limit the amount of results this is how the whole thing ended up.
<?php
$i="1";
foreach ($articulos as $articulo) { ?>
<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulo['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
<?= $articulo['titulo']; ?>
</a>
</a>
</div>
</div>
<?php
if ($i++ == 4) break;
} ?>
Thank You so much.
I've made a shortcode function that returns some Advanced Custom Fields depending on which repeater field is chosen. It displays correctly and in the right order however any content typed underneath the shortcode is moved outside of its <article> container element.
Here is the code:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
</div>
</div>
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');
Can anyone help solve this mystery? Many thanks.
You have two extra closing <div>'s in your code.
Try this:
function boxes_shortcode($atts, $content = null) {
ob_start(); ?>
<div id="boxes">
<div class="box-gutter"></div>
<div class="box-sizer"></div>
<?php while( have_rows('box_repeater') ): the_row();
$image_id = get_sub_field('link_image');
$image_size = 'box-thumbnail';
$image_array = wp_get_attachment_image_src($image_id, $image_size);
$linkImage = $image_array[0]; ?>
<?php if(get_sub_field('box_type') == "box-link"): ?>
<div class="box">
<div class="link-box">
<img src="<?php echo $linkImage; ?>" alt="<?php echo $linkImage['alt']; ?>" />
<a class="caption-wrapper" href="http://www.google.com">
<span id="link-box-content" style="background:<?php the_sub_field('link_background_colour'); ?>">
<h6 style="color:<?php the_sub_field('link_title_colour'); ?>"><?php the_sub_field('link_title'); ?></h6>
<h4 style="color:<?php the_sub_field('link_text_colour'); ?>"><?php the_sub_field('link_text'); ?></h4>
<p style="color:<?php the_sub_field('link_text_colour'); ?>" href="<?php the_sub_field('link_url'); ?>"><?php the_sub_field('link_url_text'); ?> ></p>
</span>
</a>
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-quote"): ?>
<div class="box">
<div class="quote-box">
<div class="quotation-mark"></div>
<h4><?php the_sub_field('quote'); ?></h2>
<p><?php the_sub_field('quote_source'); ?></p>
<?php the_sub_field('quote_link_text'); ?> >
</div>
</div>
<?php endif;
if(get_sub_field('box_type') == "box-twitter"): ?>
<div class="box">
<div class="twitter">
<a class="twitter-timeline" href="<?php the_sub_field('twitter_url'); ?>" data-widget-id="<?php the_sub_field('twitter_widget_id'); ?>" data-chrome="noheader noscrollbar nofooter noborders transparent" data-tweet-limit="<?php the_sub_field('number_of_tweets'); ?>"></a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<a class="twitter-badge" href="<?php the_sub_field('twitter_url'); ?>"></a>
<div class="twitter-bottom">See more ></div>
</div>
</div>
<?php endif;
endwhile;?>
</div>
<!--/div>
</div-->
<?php
/* Get the buffered content into a var */
$sc = ob_get_contents();
ob_end_clean();
return $sc;
}
add_shortcode('boxes', 'boxes_shortcode');