Putting alternative text after every third post - php

I am currently working on a WordPress site for the first time and having some issues figuring out how to achieve what I am aiming for. I would like for it to display a banner advertisement after every third post then continue from where it left off in my existing post.
Because I probably worded this poorly I will display an example image below.
Here's the current loop file I am using within my index.php file.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title = htmlentities(get_the_title ());
$str = explode ("&#8211;", $title);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
?>
<div class="album-meta" style="border-bottom: 1px solid #eee;">
<div class="cover">
<img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>">
</div>
<div class="metadata">
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
<p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
</a>
<p>Download
</div>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

You can do it like below:-
<?php $i = 0; // create a counter?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title = htmlentities(get_the_title ());
$str = explode ("&#8211;", $title);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
?>
<div class="album-meta" style="border-bottom: 1px solid #eee;">
<div class="cover">
<img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>">
</div>
<div class="metadata">
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
<p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
</a>
<p>Download
</div>
</div>
<?php if($i%3 ==0 && $i >0){ // check you reached to third div or not?>
<!-- write the html of advertisement div ------->
<?php $i++;} ?> <!-- increase counter -->
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Try this...
<?php
$counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$title = htmlentities(get_the_title ());
$str = explode ("&#8211;", $title);
$artist = preg_replace('#\[[a-zA-Z].*\]#','',$str[0]);
$song = preg_replace('#\[[a-zA-Z].*\]#','',$str[1]);
if($counter == 3){ ?>
********** Advertisement code write here ***********
<?php
}
?>
<div class="album-meta" style="border-bottom: 1px solid #eee;">
<div class="cover">
<img width="90px" height="90px" src="<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); echo ''.$feat_image.''; ?>" alt="<?php the_title(); ?>">
</div>
<div class="metadata">
<a href="<?php the_permalink(); ?>" style="text-decoration: none; color: #757575"><p><i style="font-size: 13.7px;"><?php print $song; ?></i></p>
<p><strong style="font-size: 15px;"><?php print $artist; ?></strong></p>
</a>
<p>Download
</div>
</div>
<?php
$counter++;
endwhile; else : ?>

Related

ACF Repeater field returns null value

I am trying to include a repeater field in a custom taxonomy template but it keeps returning No Posts to display. I know there are posts to display so I am baffled.
I have tried adding in wp_reset_postdata() but that didn't do anything, I am getting no errors.
Here is the repeater in question:
<?php if( have_rows('feature_repeater') ): ?>
<div class="related-posts">
<h2>You May Also Like...</h2>
<div>
<?php while( have_rows('feature_repeater') ): the_row();
// vars
$image = get_sub_field('collection_image');
$content = get_sub_field('collection_title');
$link = get_sub_field('collection_link');
?>
<div>
<?php if( $link ): ?>
<a href="<?php echo $link; ?>">
<?php endif; ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</div>
<?php endwhile; ?>
</div>
</div>
<?php else : ?>
No Posts to Display
<?php endif; ?>
and here is my template:
<?php
/**
* Template used for single posts and other post-types
* that don't have a specific template.
*
* #package Avada
* #subpackage Templates
*/
// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Direct script access denied.' );
}
?>
<?php get_header(); ?>
<section id="content" class="full-width" style="width: 100%;">
<div id="post-15912" class="fusion-archive-description post-15912 avada_portfolio type-avada_portfolio status-publish format-standard has-post-thumbnail hentry portfolio_category-dumfries-and-galloway inn-features-coastal-breaks inn-features-countryside-walks inn-features-cycling-holidays inn-features-dog-friendly-pubs inn-features-fine-dining inn-features-fishing-holidays inn-features-romantic-getaways">
<div class="post-content">
<?php
$portfolio_cat_slug = get_queried_object()->slug;
$portfolio_cat_name = get_queried_object()->name;
?>
<h1><?php echo $portfolio_cat_name; ?></h1>
<?php echo category_description(); ?>
<div class="fusion-aligncenter" style="margin-top: 20px;">Buy <?php echo $portfolio_cat_name; ?> Gift Vouchers</div>
</div>
<div class="fusion-posts-container fusion-blog-layout-grid fusion-blog-layout-grid-3 isotope fusion-blog-pagination " data-pages="2" style="position: relative; height: 1384.4px;">
<?php
$collection_post_args = array(
'post_type' => 'avada_portfolio', // Your Post type Name that You Registered
'posts_per_page' => 999,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'inn-features',
'field' => 'slug',
'terms' => $portfolio_cat_slug
)
)
);
$collection_post_qry = new WP_Query($collection_post_args);
if($collection_post_qry->have_posts()) :
while($collection_post_qry->have_posts()) :
$collection_post_qry->the_post();
?>
<article id="post-15912" class="fusion-post-grid post fusion-clearfix post-15912 avada_portfolio type-avada_portfolio status-publish format-standard has-post-thumbnail hentry portfolio_category-dumfries-and-galloway inn-features-coastal-breaks inn-features-countryside-walks inn-features-cycling-holidays inn-features-dog-friendly-pubs inn-features-fine-dining inn-features-fishing-holidays inn-features-romantic-getaways" style="position: absolute; left: 0px; top: 0px; padding: 10px;">
<div class="fusion-post-wrapper">
<div class="fusion-flexslider flexslider fusion-post-slideshow">
<ul class="slides">
<li class="flex-active-slide" style="width: 100%; float: left; margin-right: -100%; position: relative; opacity: 1; display: block; z-index: 2;">
<div class="fusion-image-wrapper" aria-haspopup="true">
<a href="<?php the_permalink(); ?>">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img width="384" height="173" alt="" data-src="<?php echo $url ?>" class="attachment-full size-full wp-post-image lazyloaded" src="<?php echo $url ?>" draggable="false">
</a>
</div>
</li>
</ul>
</div>
<div class="fusion-post-content-wrapper">
<div class="fusion-post-content post-content">
<h2 class="entry-title" itemprop="headline"><?php the_title(); ?></h2>
</div>
<div class="fusion-meta-info">
<div class="fusion-alignleft">
Read More
</div>
</div>
</div>
</div>
</article>
<?php
endwhile;
endif;
?>
</div>
</div>
<div class="fusion-aligncenter" style="margin-top: 20px;">Buy <?php echo $portfolio_cat_name; ?> Gift Vouchers</div>
<?php if( have_rows('feature_repeater') ): ?>
<div class="related-posts">
<h2>You May Also Like...</h2>
<div>
<?php while( have_rows('feature_repeater') ): the_row();
// vars
$image = get_sub_field('collection_image');
$content = get_sub_field('collection_title');
$link = get_sub_field('collection_link');
?>
<div>
<?php if( $link ): ?>
<a href="<?php echo $link; ?>">
<?php endif; ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
<?php if( $link ): ?>
</a>
<?php endif; ?>
<?php echo $content; ?>
</div>
<?php endwhile; ?>
</div>
</div>
<?php else : ?>
No Posts to Display
<?php endif; ?>
</section>
<?php do_action( 'avada_after_content' ); ?>
<?php get_footer(); ?>

Split a PHP while Loop into seperete divs

I am trying to output all client logos, but limit each section <section> to 16 logos. At the moment, the HTML outputted isn't correct but for the life of me I cannot figure our where my code is wrong.
Any help would be greatly appreciated:
<?php $logototal = count( get_sub_field('bp_client_logos') ); //Get a total count of the logos ?>
<?php
$count = "0";
if( have_rows('bp_client_logos') ) :
$i = 1;
while ( have_rows('bp_client_logos') ) : the_row(); ?>
<?php
$clientindex = $i;
if( $clientindex == "1" || ($clientindex-1) %16 == "0" ) :
?>
<div class="section general-section section-<?php echo $title; ?> section-count-<?php echo $count; ?>" data-anchor="section-<?php echo $count; ?>" style="<?php echo $bg_background; ?>">
<div class="row small-up-4 medium-up-8 large-up-8 align-middle">
<?php endif; ?>
<div class="column client-logo-grid">
<?php
$image = get_sub_field('bp_client_logo');
// Vars
$url = $image['url'];
$thistitle = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
$size = 'ClientLogo' ;
$thumb = $image['sizes'][ $size ];
?>
<?php if ( get_sub_field('bp_client_link') ) : ?>
<a target="_blank" href="<?php the_sub_field('bp_client_link'); ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
</a>
<?php else: ?>
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" />
<?php endif; ?>
</div><!-- .column -->
<?php if( ($clientindex) %16 == "0") : $count = $count+1; ?>
<?php if($logototal > ($count*16)) : ?>
<div class="show-for-medium more-down-icon">
<span class="more-text">More</span> <span class="more-arrow"><img src="<?php echo get_template_directory_uri(); ?>/assets/images/arrow-down.png" alt="arrow-down-icon" /></span>
</div>
<?php endif; ?>
</div><!-- .row -->
</div><!-- .section -->
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>

Add a dynamic counter number inside ancher tag

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; ?>

nth-child with a PHP array

I'm using ACF to post WP posts on a page. But I to be able to control certain CSS properties using nth:child selectors. Heres the PHP which displays every thing fine. Problem is (I'm assuming) that the "foreach" item is making the nth:child not work, because the are not siblings?
<div class="row">
<?php
$posts = get_field('projects');
if ( $posts ) {
foreach ( $posts as $post ): ?>
<div class="col-xl-6 no-space">
<?php
setup_postdata($post); ?>
<div class="project__wrap">
<div class="project__project"><?php echo get_the_post_thumbnail(); ?></div>
<div class="project__description__list col-xl-6"><div class="project-content"><h2><?php echo get_the_title() ; ?></h2><p><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 15 ) ); ?></p></div>
<div class="heart"><?php
$image = get_field('heart_icon');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</div> <!-- .project__description__list -->
</div> <!-- .project__wrap -->
<?php wp_reset_postdata(); ?>
</div>
<?php endforeach;
}
?>
div.project-content:nth-child(odd) {
top:0;
left:0;
}
div.project-content:nth-child(even) {
bottom:0;
right:0;
}
The problem in your case is, project-content is always the first child of it's container (project__description__list). What you want is this for .col-xl-6.no-space. But this would be very ambiguous, so I would suggest, to add another class to this div - i.e. project-row-item.
<div class="col-xl-6 no-space project-row-item">
doing so, you could change your css like this
.project-row-item:nth-child(odd) div.project-content {
top:0;
left:0;
}
.project-row-item:nth-child(even) div.project-content {
bottom:0;
right:0;
}
<?php $posts = get_field('projects');
if ( $posts ) {
$i=0; $k; $class='';
foreach ( $posts as $post ):
$k= $i%2;
if($k==0){ $class='even'; } else { $class='odd'; }?>
<div class="col-xl-6 no-space">
<?php setup_postdata($post); ?>
<div class="project__wrap">
<div class="project__project"><?php echo get_the_post_thumbnail(); ?></div>
<div class="project__description__list col-xl-6"><div class="project-content <?php echo $class; ?>"><h2><?php echo get_the_title() ; ?></h2><p><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 15 ) ); ?></p></div>
<div class="heart"><?php $image = get_field('heart_icon');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</div> <!-- .project__description__list -->
</div> <!-- .project__wrap -->
<?php wp_reset_postdata(); ?>
</div>
<?php $i++; endforeach; } ?>
Now you could change your style as :
div.project-content.odd {
top:0;
left:0;
}
div.project-content.even {
bottom:0;
right:0;
}

Wordpress - issue with the lightbox plugin inside a loop

i'm having problems with the Lightbox Plus plugin. I need to create a bunch of elements in my homepage, and Lightboxes linked to them to show more content. Now, I managed to create all the elements needed, but the Lightboxes generated are all the same, all with contents related to the first of the elements to be specific. If inserted in the loop shouldnt each of them be related to his "starting element"? This is the code i'm using:
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$id = get_the_ID();
$big_image = get_field( "big_cocktail" );
$thumb_image = get_field( "thumbnail_cocktail" );;
$titolo_box = get_the_title();
$sottotitolo_box = get_field( "sottotitolo_cocktail" );
$h2_css = "margin-bottom: 5px !important;";
$sottotitolo_css = "width:100% !important; text-align:center !important; padding-top: 5px !important; margin: 0 auto !important; margin-top:20px !important;";
?>
<!--element-->
<div class="element <?php echo $category -> slug; ?>" data-category="<?php echo $category -> slug; ?>">
<a class="lbp-inline-link-1" href="#" style="text-decoration:none;">
<img alt="" class="imgwork" src="<?php echo $thumb_image; ?>" heigth="100px"/>
</a>
<!-- THIS IS THE DIV FOR THE LIGHTBOX -->
<div style="display:none">
<div id="lbp-inline-href-1" style="background: #fff; height:100%; background-image:url('<?php echo $big_image; ?>');">
<h2 style=""><?php echo $titolo_box; ?></h2>
</div>
</div>
<!-- END DIV-->
<div class="worksarrow">
<a class="lbp-inline-link-1" href="#" style="text-decoration:none;">
<img alt="" src="<?php echo get_template_directory_uri(); ?>/img/section-works/arrow.png" />
</a>
</div>
<a class="lbp-inline-link-1" href="#" style="text-decoration:none;">
<h2 style=""><?php echo $titolo_box; ?></h2>
</a>
<div class="sottotitolo_portfolio" style="<?php echo $sottotitolo_css; ?>">
<?php echo $sottotitolo_box; ?> </div>
<div class="worksbottom"></div>
</div>
<!--element-->
<?php endwhile;
}
?>
I've finally solved this. He was creating all the right popups but each of them needed a specific class and ID, so it just became:
< div id="lbp-inline-href-< ?php echo $index; ?>" style="background: #fff; height:100%; background-image:url('< ?php echo $big_image; ?>');">
< ?php $index++; ? >

Categories