I want to display the excerpt of 2 posts with title and post-date, the code is done but it is showing the date only for one post.
Here Is The Code.
<div class="col-md-4">
<div class="reel-focus-blog wow slideInRight animated">
<h1>Reel Focus Blog</h1>
<?php
$the_query = new WP_Query( 'posts_per_page=2' );
while ($the_query -> have_posts()) : $the_query ->
the_post(); ?>
<div class="reel-focus-blog-box">
<h3><?php the_title(); ?></h3>
<p class="datetime"><i><?php the_date();?></i></p>
<p><?php the_excerpt(__('(more…)')); ?>
<a class="read-more" href="">Read More</a></>
</div>
<?php
endwhile;
wp_reset_postdata();?>
</div>
</div>
Test with
get_the_date()
// The Args
$args = array(
'posts_per_page' => 2,
'order' => 'DESC',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
<div class="reel-focus-blog-box">
<h3><?php the_title(); ?></h3>
<p class="datetime"><i><?php echo get_the_date();?></i></p>
<p><?php the_excerpt(); ?>
<a class="read-more" href="">Read More</a></>
</div>
}
} else {
// no posts found
}
wp_reset_postdata();
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();
?>
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(); ?>
I'm trying to show 2 latest blog posts on my homepage.
However, I want them to appear in 2 separate boxes. This code only shows the same blog post twice. How do I get the second most recent blog post to show in my second box?
Any help would be much appreciated :)
<div class="row boxesl">
<div class="c6">
<?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(__('(more…)')); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="c6 last">
<?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(__('(more…)')); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
</div>
Try Below Code :
<div class="row boxesl">
<?php
$the_query = new WP_Query( array(
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'posts_per_page' => 2)
);
if($the_query->have_posts())
{
$cnt=1;
while ($the_query -> have_posts()) : $the_query -> the_post();
if($cnt==2)
$class=" last";
else
$class="";
?>
<div class="c6 <?php echo $class;?>">
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(__('(more…)')); ?></p>
</div>
<?php
$cnt++;
endwhile;
wp_reset_postdata();
}
?>
You can use offset parameter to arguments.
<div class="row boxesl">
<div class="c6">
<?php $the_query = new WP_Query( 'posts_per_page=1' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(__('(more…)')); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="c6 last">
<?php $the_query = new WP_Query( 'posts_per_page=1&offset=1' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(__('(more…)')); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
</div>
https://codex.wordpress.org/Class_Reference/WP_Query
I'm fairly new with using WP shortcodes, and I've run into a problem. I have tried to make a shortcode, that shows 6 of my blog posts through a loop, but it doesn't work. When it loads, it just smashes the page. The loop code works in practice, just not with the shortcode.
The code
function myshort() { ?>
<?php
$args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
<a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<div class="text">
<span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
<h3><?php the_title(); ?></h3>
<p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
</div>
</a>
</div>
<?php endwhile;
}
add_shortcode('doitman', 'myshort');
So, my question is, how do I write this the right way?
The result should be returned as a value,
you could try something like this :
function myshort() {
ob_start(); ?>
<?php
$args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
<a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<div class="text">
<span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
<h3><?php the_title(); ?></h3>
<p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
</div>
</a>
</div>
<?php endwhile;
return ob_get_clean();
}
add_shortcode('doitman', 'myshort');