Query related posts, Wrap in Row every 3 posts - wordpress

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

Related

How to pull latest posts from a specific category

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

I can not show my product feature image

<?php
$params = array(
'posts_per_page' => 9,
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<div class="col-sm-4 nk">
<div class="course">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?>" />
</a>
<h3> <?php the_title(); ?></h3>
<h5><?php the_author(); ?></h5>
<p><?php echo $product->get_rating_html();?></p>
<h4><?php echo $product->get_price_html();?></h4>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
First of all,
replace this code
<img src="<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?>" />
with
<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?>
And you need to add <?php global $product; ?> exactly before it use.
The total correct code is
<?php
$params = array(
'posts_per_page' => 9,
'post_type' => 'product'
);
$wc_query = new WP_Query($params);
if ($wc_query->have_posts()) :
while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php global $product;?>
<div class="col-sm-4 nk">
<div class="course">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?>
</a>
<h3> <?php the_title(); ?></h3>
<h5><?php the_author(); ?></h5>
<p><?php echo $product->get_rating_html();?></p>
<h4><?php echo $product->get_price_html();?></h4>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products' ); ?>
</p>
<?php endif; ?>
you are doing mistake whenever you use the_post_thumbnail() write with echo the_post_thumbnail();
so instead of the_post_thumbnail() use echo the_post_thumbnail()

Getting multiple copies of images when using WP_query to display custom post

I'm using WP-query to display my 3 most recent 'events' which are a custom post type.
Visually it looks like its working fine, but after doing some debugging I noticed that the same image is being loaded 4 times for each post.
<div class="container">
<?php
$args = array(
'post_type' => 'projects',
'posts_per_page' => '2'
);
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
?>
<div class="row small-up-1 medium-up-2 large-up-<?php echo $count ?>" data-equalizer="news" data-equalize-on-stack="false" data-equalize-on="medium">
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="column">
<div class="card">
<div class="card-divider" data-equalizer-watch="news">
<h4> <?php the_title(); ?></h4>
<?php
// FLEXIBLE CONTENT NAME
if( have_rows('main_sections') ):
// FLEXIBLE CONTENT NAME
while ( have_rows('main_sections') ) : the_row(); ?>
<?php // LAYOUT NAME
if( get_row_layout() == 'standard_details' ): ?>
<?php if( get_sub_field('date_of_event') ):
$field_name1 = "date_of_event";
$field1 = get_sub_field_object($field_name1);
?>
<p><?php echo $field1['value']; ?></p>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php the_excerpt(__('(more…)')); ?>
</div>
<?php if ( 'video' == get_post_format() ) { ?>
<div class="video-icon">
<i class="fa fa-play fa-3x" aria-hidden="true"></i>
<?php } ?>
<a href="<?php the_permalink() ?>">
<img data-interchange="[<?php echo the_post_thumbnail_url('recent-news'); ?>, small]" alt="<?php the_title(); ?>" />
</a>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>

How to add a widget after post content

Right now in my theme for my home page I have a row of 1 post, a row of 3 posts, 3 posts, and then back to a row of 1 post. After this loop I would like to add a new widget. I want to have a few different widgets like popular videos, popular posts, etc. Does anyone have any idea how I can do this?
Here is an example of what I want it to look like. I want a new widget for every 14 posts.
Here is are some examples of the different widgets I want to add (source - http://www.whowhatwear.com) -
Here is my front-page.php
<?php
/*
* Template Name: learningwordpress
*/
get_header();
get_template_part ('inc/carousel');
$i = 0;
$args = array(
'posts_per_page' => 14,
'paged' => 1
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
if( $i %2 == 1 ) {
$the_query->the_post(); ?>
<article class="post col-md-4">
<?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php $the_query->the_post(); ?>
<article class="post col-md-4">
<?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php $the_query->the_post(); ?>
<article class="post col-md-4">
<?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php $the_query->the_post(); ?>
<article class="post col-md-4">
<?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php $the_query->the_post(); ?>
<article class="post col-md-4">
<?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php $the_query->the_post(); ?>
<article class="post col-md-4">
<?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php
}
else {
$the_query->the_post(); ?>
<article class="post col-md-12">
<?php the_post_thumbnail('large-thumbnail'); ?>
<h2><?php the_title(); ?></h2>
<p>
<?php echo get_the_excerpt(); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
</p>
</article>
<?php
}
?>
<?php
$i++;
}
}
else {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
get_footer();
Add following code in your theme functions.php file to create widget
register_sidebar( array(
'name' => 'After content',
'id' => 'after-content',
'description' => '',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
Add following code where you want to display widget area
if ( is_active_sidebar( 'after-content' ) ) :
dynamic_sidebar( 'after-content' );
endif;

Display the trash posts instead of published posts?

I want to display the posts of post_status="trash" but not post_status="publish". In short, the posts in trash should get displayed on the site. For how long the trash posts would be available in trash? Any help would be appreciated.
index.php
<?php
// show all coupons and setup pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => APP_POST_TYPE,
'ignore_sticky_posts' => 1,
'paged' => $paged
));
?>
<?php get_template_part('loop', 'coupon'); ?>
loop-coupon.php
<?php while (have_posts()) : the_post(); ?>
<?php appthemes_before_post(); ?>
<div <?php post_class('item'); ?> id="post-<?php echo $post->ID; ?>">
<div class="item-holder">
<div class="item-frame">
<div class="store-holder">
<div class="store-image">
<img height="89" width="110" src="<?php echo clpr_store_image($post->ID, APP_TAX_STORE, 'term_id', '110', null); ?>" alt="" />
</div>
<div class="store-name">
<?php echo get_the_term_list($post->ID, APP_TAX_STORE, ' ', ', ', ''); ?>
</div>
</div>
<?php clpr_vote_box_badge($post->ID, $the_trans); ?>
<div class="item-panel">
<?php clpr_coupon_code_box(); ?>
<div class="clear"></div>
<?php appthemes_before_post_title(); ?>
<h1><?php if (mb_strlen(get_the_title()) >= 87) echo mb_substr(get_the_title(), 0, 87).'...'; else the_title(); ?></h1>
<?php appthemes_after_post_title(); ?>
<?php appthemes_before_post_content(); ?>
<p class="desc"><?php echo mb_substr(strip_tags($post->post_content), 0, 200).'... ';?><a class="more" href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'View the %s coupon page', 'appthemes' ), the_title_attribute( 'echo=0' ) ); ?>"><?php _e('more ››', 'appthemes') ?></a></p>
<?php appthemes_after_post_content(); ?>
</div> <!-- #item-panel -->
</div> <!-- #item-frame -->
</div>
</div>
<?php appthemes_after_post(); ?>
<?php appthemes_after_endwhile(); ?>
<?php appthemes_loop_else(); ?>
<h3><?php _e('Sorry, no coupons found', 'appthemes'); ?></h3>
`
Try adding this before your loop
query_posts($query_string. '&post_status=trash');

Resources