I want to create a loop for wordpress that returns each two posts inside its own div and alternating columns every new row (see example)... Im not experimented in php enough to make this happen. I dont manage to get it working appropiatly. And see how to make the last div to bee 100% width if it does not have another column.
I would appreciate your support to make this happen since I tried many things and still no luck. (im using visual composer bootstrap classes, it does work but not as expected.This is the example I want to create
This is my code:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'DATE'
);
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $i++; $imagen = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<?php if(($i % 2) == 2) : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
</div>
</div>
<?php else : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endif; endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
[EDIT]Try this:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php
$float_class = '';
while ( $the_query->have_posts() ) :
$the_query->the_post();
if ( $the_query->current_post &&
$the_query->current_post % 2 === 0 ) {
$float_class = $float_class ? '' : 'vc_pull-right';
}
$imagen = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der <?php echo $float_class; ?>">
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url('<?php echo esc_url( $imagen ); ?>') no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endwhile; // end have_posts() loop ?>
</div><!-- .vc_row -->
<?php endif; // end have_posts() check ?>
<?php wp_reset_query(); ?>
Related
I have a simple loop on a wordpress homepage that pulls in 3 random articles. It's intermittently rendering the cards inside the other cards instead of 3 separate cards. I feel like it's a timing issue being caused from pulling them in randomly. Is there a way to force them to render separately?
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '3'
);
$loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="card" data-url="<?php echo the_permalink(); ?>">
<div class="card-body">
<?php the_post_thumbnail('homepage-thumbs', array( 'class' => 'aligncenter' )); ?>
<h3><?php the_title(); ?></h3>
<span><i class="fal fa-user"></i> by <?php the_author(); ?></span>
<p></p>
<p><?php echo substr(get_the_content(), 0, 128); ?>...</p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Your code works in my test. However, I would use the wp_trim_words to create your excerpt rather than what you did, which could cut a word in the middle.
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'rand',
'posts_per_page' => '3'
);
$loop = new WP_Query($args); ?>
<?php if ( $loop->have_posts() ) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="card" data-url="<?php echo the_permalink(); ?>">
<div class="card-body">
<?php the_post_thumbnail('homepage-thumbs', array( 'class' => 'aligncenter' )); ?>
<h3><?php the_title(); ?></h3>
<span><i class="fal fa-user"></i> by <?php the_author(); ?></span>
<p></p>
<p><?php
$theContent = get_the_content();
// strip out any shortcodes from the excerpt
$theContent = strip_shortcodes( $theContent );
// wp_trim_words($content, number_of_words, read_more_text)
echo wp_trim_words( $theContent, 30 , '...' );?></p>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
Try to add wp_reset_postdata after your loop, like
<?php endwhile; wp_reset_postdata(); ?>
I simply have a single loop that's pulling through a CPT, but I would like to hide the whole container div of the loop, if it has no posts...
I'm trying various iterations of this if statement surrounding the container:
<?php if( have_posts() ): ?>
<?php endif; ?>
But I can't seem to get it to work properly... The full code blog is here:
<?php if( have_posts() ): ?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title'); ?></h2>
<div class="row justify-content-center">
<?php
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999
// 'orderby' => 'title',
// 'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-md-12">
<a href="<?php the_permalink(); ?>">
<p><?php the_title() ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
<?php endif; ?>
Can anyone point out what I'm doing wrong? I'm sure I'm missing something simple! Thanks for looking!! :)
#rank's answer is nearly correct -- it needs the object instance before the_post() again:
<?php
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999, // If you're wanting to show all posts, use -1 here.
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title'); ?></h2>
<div class="row justify-content-center">
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="col-md-12">
<p><?php the_title(); ?></p>
</div>
<?php
endwhile;
?>
</div>
</div>
<?php
endif;
wp_reset_postdata();
?>
You need to put the html of the container after the if have_posts(), where you check if there are any posts. This way the whole container is not displayed when the if is not true.
But you are using a ACF field of the post where you are showing this list of vacancies. So we save the id into a variable to be able to view the value inside of your custom query called the_query (this is not a self-explanatory / good name). Why not call it vacancies_query to have a more readable code. Putting it together it should look like this:
<?php
$current_post = get_the_ID();
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999
);
$vacancies_query = new WP_Query( $args );
if ( $vacancies_query->have_posts() ) : ?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title', $current_post); ?></h2>
<div class="row justify-content-center">
<?php while ( $vacancies_query->have_posts() ) : the_post(); ?>
<div class="col-md-12">
<a href="<?php the_permalink(); ?>">
<p><?php the_title() ?></p>
</a>
</div>
<?php endwhile; ?>
</div> <!-- row -->
</div> <!-- container -->
<?php else :
/* nothing to see here */
endif;
wp_reset_postdata();
?>
In this site (given the link below), I show a series of articles. I would like for each of these articles to write a number below. For example: if there are 5 items, each of them will have 1 - 2 - 3 - 4 - 5 at the bottom
I tried to do in multiple ways, with jquery, with a javascript loop but in this case it gives me an error because the variable is rewritten for each article.
<?php /** * Template name: 01. Pagina master */ ?>
<?php get_header(); ?>
<div class="col-sm-12 works" id="content">
<div class="row grid">
<!-- MAIN-->
<main id="main">
<section id="fs-container"
style="background-image:url(http://www.domuspoint.it/wp-content/uploads/2019/08/pergole_domus_point_-8.jpg);">
<?php
$loop = new WP_Query( array( 'post_type'=> 'work', 'posts_per_page' => -1, 'post_parent' => 0 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
?>
<div class="media-project-hover" data-background="<?php echo $backgroundImg[0]; ?>">
<div class="bg-overlay"></div>
<div class="media__body">
<h5 class="media__sub-title title-sub">SOLE</h5>
<h3 class="media__title">
<?php the_title(); ?>
</h3>
<a class="link" href="<?php the_permalink(); ?>">vedi</a>
</div>
<span class="media__number" id="bello">5</span>
</div>
<?php
endwhile; wp_reset_query();
?>
</section>
</main>
<!-- END MAIN-->
</div>
</div>
<?php get_footer(); ?>
https://pastebin.com/KbdzNiZF
What about using a counter, and incrementing it after each loop iteration?
<?php
$loop = new WP_Query( array( 'post_type'=> 'work', 'posts_per_page' => -1, 'post_parent' => 0 ) );
counter = 1; //the counter
while ( $loop->have_posts() ) :
$loop->the_post();
$backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );
?>
<span class="media__number" id="bello"><?= $counter ?></span>
<?php
$counter += 1; //increment counter
endwhile; wp_reset_query();
?>
Hi I've made a custom post type 'work_fields' that calls in information from yet another custom post type 'members' into the post, and now I'm trying to make a PAGE TEMPLATE that shows a list of the titles of custom post type 'work_fields', and when you click a title, the whole post('work_fields') will show up on a div called 'single-post-container' below the titles. right now I've got everything working fine, but I want to display a post in the div 'single-post-container' when the page loads. (as of now, just the titles of the posts are displayed and there is nothing in the div). How do I get the div to display the most recent post of custom post type 'work_fields' on page load? This is the code for the custom page template.
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array('post_type' => 'work_fields',);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
//THIS IS WHERE THE POST CONTENTS SHOWS BUT I WANT THE MOST RECENT POST TO BE HERE ON PAGE LOAD, BEFORE I CLICK ANY OTHER POST//
</div>
Thank you! Your help is much appreciated!
Just use the WP_query twice by getting recent posts in the arguments,
$args2 = array('post_type' => 'work_fields', 'orderby' => 'ID', 'order'=> 'DESC' , 'posts_per_page' => 5);
$query2 = new WP_Query( $args2 );
?><div id="single-post-container"><?php
// The Loop
if ( $query2->have_posts() ) {
echo '<ul>';
while ( $query2->have_posts() ) {
$query2->the_post();
echo '<li>' . get_the_content() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
}
?></div><?php
Put the div outside loop. It will show the content of recent 5 posts.
If everything above the div single-post-container is working fine then for this specific div you can load most recent post by using code below. Be sure to reset previous post data using wp_reset_postdata()
Codex Documentation. https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
<?php
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'suppress_filters' => true
);
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
So I just recreated the post type markup on my page. I'm sure there's a better way to do this but for times sake I had to at least make it work. I also tried using a jquery onclick function and just click the first title after everything loads, but there was an error that just kept pushing all of the titles so I pretty much gave up.
here's the code
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
</div>
</div>
<div class="row halfsection">
<div class="small-12 medium-10 large-offset-1 columns">
<div class="category_container">
<?php
$args = array(
'post_type' => 'work_fields',
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php echo get_the_title(); ?></a></p>
<?php endwhile; endif; ?>
</div>
</div>
</div>
<div class="row">
<div class="small-12 medium-10 large-offset-1 columns">
<hr>
</div>
</div>
<div id="single-post-container">
<?php
$args2 = array(
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'work_fields',
'post_status' => 'publish',
'posts_per_page' => 1,
);
$query2 = new WP_Query( $args2 );
if ( $query2->have_posts() ) : ?>
<?php $post = get_post($_POST['id']); ?>
<div id="single-post post-<?php the_ID(); ?>">
<?php while ( $query2->have_posts() ) : $query2->the_post(); ?>
<div class="row section">
<div class="small-12 medium-7 large-offset-1 columns">
<h2><?php the_title(); ?></h2>
<h3>소개</h3>
<p class="halfsection"><?php the_field('work_fields_intro'); ?></p>
<h3>주요서비스</h3>
<p class="halfsection"><?php the_field('work_fields_service'); ?></p>
<h3>주요실적</h3>
<p class="halfsection"><?php the_field('work_fields_accomplishment'); ?></p>
</div>
<?php endwhile; endif; ?>
<div class="small-6 medium-3 large-2 columns large-offset-1 end">
<?php
$posts = get_field('team_member');
if( $posts ): ?>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<div class="member_container halfsection">
<div class="member"><?php the_post_thumbnail(); ?></div>
<p class="member_name"><?php the_title(); ?></p>
<ul class="members_info">
<li><?php the_field('members_position'); ?></li>
<li><?php the_field('members_e-mail'); ?></li>
<li><?php the_field('members_phone'); ?></li>
</ul>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
I am running two loops with a custom post type of Portfolio (ID of 3). The first loop is for Featured and the second is for the rest. I plan on having more than 3 Featured posts in random order. I would like to have the Featured ones that aren't displaying in the first loop to show up in my second loop. How can I set this up so there are no duplicate posts?
<?php
/*
Template Name: Portfolio
*/
get_header(); ?>
<div class="section-bg">
<div class="portfolio">
<div class="featured-title">
<h1>featured</h1>
</div> <!-- end #featured-title -->
<div class="featured-gallery">
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => 3, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
</a>
</div> <!-- end .featured -->
<?php endwhile; ?>
</div> <!-- end .featured-gallery -->
<div class="clearfix"></div>
</div> <!-- end .portfolio -->
</div> <!-- end #section-bg -->
<div class="clearfix"></div>
<div class="section-bg">
<div class="portfolio-gallery">
<?php
$args = array( 'post_type' => 'portfolio', 'orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
</a>
</div> <!-- end .featured -->
<?php endwhile; ?>
<div class="clearfix"></div>
</div> <!-- end .portfolio-gallery -->
<div class="clearfix"></div>
</div> <!-- end #section-bg -->
<?php get_footer(); ?>
If possible, could the answer outline how to implement it into my existing code? Thank you. :)
On your second loop, exclude the cat with ID 3. Use a negative value for it
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => -3, 'orderby' => 'rand' );
See the codex for reference
I found the solution on http://webmasters-blog.com/how-to-avoid-duplicate-posts-in-multiple-loops/
<?php
/*
Template Name: Portfolio
*/
get_header(); ?>
<div class="section-bg">
<div class="portfolio">
<div class="featured-title">
<h1>featured</h1>
</div> <!-- end #featured-title -->
<div class="featured-gallery">
<?php
$ids = array();
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 3, 'cat' => 3, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
</a>
</div> <!-- end .featured -->
<?php $ids[]= $post->ID; endwhile; ?>
</div> <!-- end .featured-gallery -->
<div class="clearfix"></div>
</div> <!-- end .portfolio -->
</div> <!-- end #section-bg -->
<div class="clearfix"></div>
<div class="section-bg">
<div class="portfolio-gallery">
<?php
$args = array( 'post_type' => 'portfolio', 'orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
if (!in_array($post->ID, $ids)) {
?>
<div class="featured peek">
<a href="<?php the_permalink(); ?>">
<h1>
<?php
$thetitle = $post->post_title;
$getlength = strlen($thetitle);
$thelength = 40;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo '...'; ?>
</h1>
<div class="contact-divider"></div>
<p><?php the_tags('',' / '); ?></p>
<?php the_post_thumbnail('thumbnail', array('class' => 'cover')); ?>
</a>
</div> <!-- end .featured -->
<?php } endwhile; ?>
<div class="clearfix"></div>
</div> <!-- end .portfolio-gallery -->
<div class="clearfix"></div>
</div> <!-- end #section-bg -->
<?php get_footer(); ?>
If there's a cleaner way to go about doing it, I'd still appreciate feedback!
Of course, there is a thing you can be improved:
$args = array( 'post_type' => 'portfolio', 'orderby' => 'rand', 'post__not_in' => $ids);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
And... adding a filter for the_title() in your functions.php would be fine too, to do the substr thing, so you can simple use the_title().