nth-child with a PHP array - php

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

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

php template code - all properties page display issue

My page: http://kevinkang.ca/en_US/all-properties/ was broken yesterday.
But I can't find any problem on these. Can anyone help me to find out the problem?
<div class="grid-x all-properties-page">
<?php
$args = array( 'category__not_in' => array( 4, 8, 9, 10 ) , 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $i=0; ?>
<div class="medium-12 cell padding-top-page">
<div class="home-data description-title-price">
<div class="grid-container">
<div class="grid-x">
<div class="medium-6 cell">
<?php the_title(); ?>
</div>
<div class="medium-6 cell text-right">
<?php the_field('price'); ?>
</div>
</div>
</div>
</div>
<a href="<?php the_permalink(); ?>"><div class="grid-x">
<ul class="images3">
<?php
//Get the images ids from the post_metadata
$images = acf_photo_gallery('3-images', $post->ID);
$img3=0;
if( count($images) ):
//Cool, we got some data so now let's loop over it
foreach($images as $image):
$id = $image['id']; // The attachment id of the media
$title = $image['title']; //The title
$caption= $image['caption']; //The caption
$full_image_url= $image['full_image_url']; //Full size image url
$full_image_url = acf_photo_gallery_resize_image($full_image_url, 1562, 760); //Resized size to 262px width by 160px height image url
$thumbnail_image_url= $image['thumbnail_image_url']; //Get the thumbnail size image url 150px by 150px
$url= $image['url']; //Goto any link when clicked
$target= $image['target']; //Open normal or new tab
$alt = get_field('photo_gallery_alt', $id); //Get the alt which is a extra field (See below how to add extra fields)
$class = get_field('photo_gallery_class', $id);
$img3++;
?>
<?php if( !empty($url) ){ ?><a href="<?php echo $url; ?>" <?php echo ($target == 'true' )? 'target="_blank"': ''; ?>><?php } ?>
<li style="background-image: url(<?php echo $full_image_url; ?>);" class="img-list<?php echo $img3; ?>">
<?php if( !empty($url) ){ ?></a><?php } ?></li>
<?php endforeach; endif; ?>
</ul>
<?php foreach ( get_the_category() as $value) {
$i++; ?> <h3 class="cat<?php echo $i; ?> <?php echo $value->name; ?>"><?php echo $value->name;} ?></h3>
</div></a>
<div class="grid-container pr-address-bg">
<div class="address-cont price-address">
<?php the_field('price'); ?>
<?php the_field('address_first_line'); ?>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
I see you have a lot of problems in your HTML ( inside , multiple tags. First check your HTML with https://validator.w3.org/. It looks the main parts of the problems come from header.php. I would have a look.

Putting alternative text after every third post

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

Edit wordpress loop to display thumbnail and excerpt

I try to realize a wordpress loop to display the articles on my blog. I'm trying to create a design like this: www.freileben.net
My wordpress loop looks like this:
.thumbnail {
float: left;
margin-right: 50px;
}
#post {
margin-top: 120px;
padding-top: 15px;
}
<article id="post">
<div id="thumbnail">
<?php if ( function_exists( 'has_post_thumbnail') && has_post_thumbnail() ) { the_post_thumbnail(array(350,220), array( "class"=>"thumbnail")); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</article>
I don't know how to solve the problem because all of the images I use have got a different size and they are not in the same position.
Try this below code :
<article id="post">
<?php
// the query
$args = array('');
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<div id="thumbnail">
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail(array( "class"=>"thumbnail"));
}
?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php } } else { ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>

PHP code within IF statement

I'm after a bit of help as I'm having a bit of difficulty trying to put php code in an IF statement:
I have the following code:
<aside class="sidebar top">
<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php
$image = get_field('quote_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>
<?php endif; ?>
</aside>
I'm trying to put the above code in the below code, where it says "Testimonial off" but not in the "Testimonial On" section.
<?php
$values = get_field( "display_testimonial" );
if ( $values ) {
echo "Testimonial Off";
} else {
echo "Testimonial On";
}
?>
Every time I try I'm getting PHP errors, can anyone help me out?
I have tried to merge the two codes together but I can get the sidebar to show in the else statement now:
<?php
$values = get_field( "display_testimonial" );
if ( $values ) {
?>
<aside class="sidebar top">
<?php dynamic_sidebar( 'About-Sidebar' ); ?>
<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php
$image = get_field('quote_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>
<?php endif; ?>
</aside>
<?php
} else {
("<?php dynamic_sidebar( 'About-Sidebar' ); ?>");
}
?>
Thanks for your help
You have to be aware of correct opening and closing php tags:
<?php
$values = get_field( "display_testimonial" );
if ( $values ) {
// ADDED CLOSING PHP TAG
?>
<aside class="sidebar top">
<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php
$image = get_field('quote_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>
<?php endif; ?>
</aside>
<?php
// ADDED OPENING PHP TAG
} else {
echo "Testimonial On";
}
?>
You have error in else part. Replace :
"Testimonial On"
to
echo "Testimonial On";
I managed to get it to work by doing the below:
<?php
$values = get_field( "display_testimonial" );
if ( $values ) {
?>
<aside class="sidebar top">
<?php dynamic_sidebar( 'About-Sidebar' ); ?>
<?php if(get_field('quote-text')): ?>
<div id="sidebar-testimonials">
<div class="quote-image">
<?php
$image = get_field('quote_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<div id="quote-text">
"<?php the_field('quote-text'); ?>"
</div>
<span>
<?php the_field('quote-name'); ?>
</span>
</div>
<?php endif; ?>
</aside>
<?php
} else {
echo "<aside class='sidebar top'>";
dynamic_sidebar( 'About-Sidebar' );
echo "</aside>";
}
?>

Categories