I'm trying to pull the latest posts from the category "local-news" but I'm only seeing one of the two posts that exist for this category. I'm not sure if it's my if statement and/or while loop that's causing the issue. This is my first time developing a WP theme and using PHP so I'm a little confused as to how I could fix this. I'm not sure how to close my if statement or while loop either without causing a critical error or syntax error.
<div class="container">
<div class="row mt-4">
<?php
$args = array(
'category_name' => 'local-news',
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 6
);
$q = new WP_Query($args);
$q -> the_post();
?>
if ( $q->have_posts() ) : {
while ( $q->have_posts() ) : the_post() {
<div class="card-body pb-0">
<div class="latest_news_cont">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink() ?>">
<h5>
<?php the_title(); ?>
</h5>
</a>
<?php the_excerpt(); ?>
<p style="text-align:center;">Read more</p>
<br>
</div>
</div>
<div class="card-body pb-0">
<div class="latest_news_cont">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink() ?>">
<h5>
<?php the_title(); ?>
</h5>
</a>
<?php the_excerpt(); ?>
<p style="text-align:center;">Read more</p>
<br>
</div>
</div>
</div>
</div>
Below I have fixed the initial issues I noticed:
<?php
$args = array(
'category_name' => 'local-news',
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 6
);
$q = new WP_Query($args);
if ( $q->have_posts() ) :
while ( $q->have_posts() ) :
$q->the_post();
?>
<div class="card-body pb-0">
<div class="latest_news_cont">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink(); ?>">
<h5>
<?php the_title(); ?>
</h5>
</a>
<?php the_excerpt(); ?>
<p style="text-align:center;">Read more</p>
<br>
</div>
</div>
<div class="card-body pb-0">
<div class="latest_news_cont">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink(); ?>">
<h5>
<?php the_title(); ?>
</h5>
</a>
<?php the_excerpt(); ?>
<p style="text-align:center;">Read more</p>
<br>
</div>
</div>
<?php
endwhile;
endif;
I figured it out, had a lot of syntax issues and open php tags.
<?php
// the query
$the_query = new WP_Query(array(
'category_name' => 'local-news',
'post_status' => 'publish',
'posts_per_page' => 5,
));
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query >the_post(); ?>
<div class="card-body pb-0">
<div class="latest_news_cont">
<a href="<?php the_permalink() ?>">
<?php the_post_thumbnail(); ?>
</a>
<a href="<?php the_permalink() ?>">
<h5>
<?php the_title(); ?>
</h5>
</a>
<?php the_excerpt(); ?>
<p style="text-align:center;">Read more</p>
<br>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
Related
Normally in wordpress permalink will open to a single page. but this time the goal is to open the custom post type permalink in modal window. I have lots of custom post type, and I only like to apply the modal in the doctor custom post type .
how could I achieve this? here is my code:
<div class="slider-wrapper">
<div class="center-doc">
<!--start connect to db-->
<?php $args = array(
'post_type' => 'doctor',
'posts_per_page' => -1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
);
$posts = get_posts( $args );
foreach ( $posts as $post ): setup_postdata( $post );
?>
<!--end connect to db-->
<div class="slider-box">
<a href="<?php the_permalink(); ?>"> <!-- PERMALINK TO OPEN MODAL-->
<div class="feature-img"> <?php if (has_post_thumbnail()) : ?> <?php the_post_thumbnail(); ?>
<?php else : ?> <img src="<?php echo get_template_directory_uri(); ?>/img/default-img.png" alt="<?php the_title(); ?>">
<?php endif; ?><!--/pro pic-->
</div>
<h6><?php the_title(); ?></h6>
<div class="content"><?php the_excerpt(); ?></div>
</a>
</div>
<!-- MODAL WINDOW TO OPEN WHEN PERMALINK CLICKED-->
<div id="modal" class="modal-window">
<div>
<i class="fa fa-times-circle" aria-hidden="true"></i>
<div class="detail-wraper">
<div class="detail-info">
<h3><?php the_title(); ?></h3>
<div class="details">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
I am using Wordpress 5.9.3 version.
in Wordpress, I am trying to display my most recent posts in a list. I have being able to list the links, but cant do the same with post thumbnail, title, date and category. What am I doing wrong?
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo the_date();?></p>
<h3 class="article_title"><?php echo get_the_title( $post_id ); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
<?php } ?>
Another way to do it, using WP_Query
<?php
// args query
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'order' => 'DESC',
// display only posts in specifics categories (slug)
'category_name' => 'cat-a, cat-b'
);
// custom query
$recent_posts = new WP_Query($args);
// check that we have results
if($recent_posts->have_posts()) : ?>
<ul class="article_list">
<?php
// start loop
while ($recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="regular">
<a href="<?php echo get_permalink(); ?>">
<div class="text">
<p class="category"><?php echo the_category(); ?></p>
<h3 class="article_title"><?php echo get_the_title(); ?></h3>
<p class="date"><?php echo the_date();?></p>
</div>
<div class="mask">
<img src="<?php the_post_thumbnail_url();?>" alt="" class="art_img">
</div>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif;
// reset query
wp_reset_postdata();
?>
Try below code its working for me.
<?php $recent_posts = get_posts('numberposts=5');
if($recent_posts) { ?>
<ul class="article_list">
<?php foreach( $recent_posts as $recent ) { ?>
<li class="regular">
<a href="<?php echo get_permalink($recent->ID); ?>">
<div class="text">
<p class="category"><?php echo get_the_category( $recent->ID );?></p>
<h3 class="article_title"><?php echo get_the_title( $recent->ID); ?></h3>
<p class="date"><?php echo get_the_date( $recent->ID );?></p>
</div>
<div class="mask">
<img src="<?php echo get_the_post_thumbnail_url($recent->ID,'full'); ?>" alt="" class="art_img">
</div>
</a>
</li>
<?php } ?>
</ul>
I'm trying to query some related posts and wrap them in a row div every 3 posts. I've checked all the questions regarding this but still my query renders a mess.
This is what I have so far:
<?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 6,
'post__not_in' => array($post->ID) ) );
$counter=0;
if( $related ) foreach( $related as $post ) {
setup_postdata($post); $counter++;
?>
<div class="row">
<article class="third-width">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<?php the_post_thumbnail('post-parrilla'); ?>
</a>
<?php exclude_post_categories("8"); ?>
<div class="clear"></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<div class="post-title">
<span><?php the_title(); ?></span>
</div>
</a>
</article>
<?php if ($counter%3==0):?>
</div><div class="row">
<?php endif;?>
<?php } wp_reset_postdata(); ?>
Thank you in advance
Use below code
<?php
$related = get_posts( array(
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 6,
'post__not_in' => array($post->ID) ) );
if( $related )
{
$counter = 0;
?>
<div class="row">
<?php
foreach( $related as $post ) {
setup_postdata($post);
if ($counter%3==0){
?>
</div><div class="row">
<?php
}
?>
<article class="third-width">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<?php the_post_thumbnail('post-parrilla'); ?>
</a>
<?php exclude_post_categories("8"); ?>
<div class="clear"></div>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="third-link">
<div class="post-title">
<span><?php the_title(); ?></span>
</div>
</a>
</article>
<?php
$counter++;
}
?>
</div>
<?php
}
wp_reset_postdata();
?>
I have a Wordpress Site or its Blog section I did a WP_Query post.
The loop is within a custom template its showing only one record while I have few post entries.
I think I have done correctly...
Please do let me why it is not looping the records.
<?php get_header();
?>
<div id="body">
<div class="container">
<div class="row-fluid">
<div class="span9">
<?php
$arr = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish'
);
$query = new WP_Query($arr);
if(count($query->posts) > 0):
$i = 0;
?>
<?php while($query->have_posts()): $i++;?>
<?php $query->the_post();
if(has_post_thumbnail(get_the_ID())){
$url_thumbnail = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
$url_thumbnail_news = get_the_post_thumbnail(get_the_ID(), 'thumb_700x260', array('alt' => trim(get_the_title())));
} else {
$url_thumbnail = '<image src="'.get_template_directory_uri().'/images/no-thumbnail.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
$url_thumbnail_news = '<image src="'.get_template_directory_uri().'/images/no-thumbnail-news.jpg" title="No Thumbnail" alt="Free Nile Theme - Wordpress Theme from ThemeLead" />';
}
?>
<?php if($i==1):?>
<div class="drop-shadow lifted"> <a class="nile-thumnail" href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>"><?php echo $url_thumbnail;?></a> </div>
<div class="the_post">
<h2 class="bj-title"><a href="<?php echo get_permalink(get_the_ID())?>" title="<?php the_title()?>">
<?php the_title();?>
</a></h2>
<div class="bj-date">
<?php the_date();?>
<?php the_time();?>
</div>
<div class="bj-des">
<?php the_excerpt();?>
</div>
<a href="<?php the_permalink();?>" title="<?php the_title()?>" class="read-more">
<?php _e('Read more', APP_TD);?>
</a> </div>
<?php endif;?>
<?php endwhile;?>
<?php endif;?>
<?php // Reset Query
wp_reset_query(); ?>
<div class="navigation">
<div class="alignleft">
<?php previous_posts_link('« Previous') ?>
</div>
<div class="alignright">
<?php next_posts_link('More »') ?>
</div>
</div>
</div>
<div class="span3"> <?php echo get_default_sidebar();?> </div>
</div>
</div>
</div>
<?php get_footer();?>
Try removing the 'if($i==1)' and corresponding 'endif;' line
I'm trying to display posts from a post_type in a certain way: display the first post type withing a query then in the next query to skip that first post and show the second post and so on.
I've tried to do this with offset, the first post shows but the next one doesn't show. Is there a more elegant way to achive this?
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'offset' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'offset' => 2);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
for starters - its healthy to use the wp_reset_postdata when you have multiple wp_queries on one page.
in terms of your question. the first post type doesnt need an offset and your second one should offset 1 (minus the first)
<div class="tableCell">
<?php
$args = array( 'post_type' => 'service', 'posts_per_page' => 1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>
<div class="tableCell">
<?php
$args2 = array( 'post_type' => 'service', 'offset' => 1);
$loop2 = new WP_Query( $args2 );
while ( $loop2->have_posts() ) : $loop2->the_post(); ?>
<div class="flip">
<a href="#">
<div class="flip-front">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
<div class="flip-back">
<div class="imgHolder">
<?php the_post_thumbnail(); ?>
</div>
<h3><?php the_title(); ?></h3>
</div>
</a>
</div>
<?php endwhile; wp_reset_postdata(); ?>
</div>