Wordpress - Nesting WP_Query loop duplicate content - php

I have a nested wordpress loop to generate a two columns layout with the content of two different categories. What is happening is that the content is duplicated, but all the posts are displayed. I've tried using wp_reset_query() and wp_reset_postdata() but with no success. Is there a fix?
<?php
$main = new WP_Query(
array(
'post_type' => 'post',
'category_name' => 'Notizie varie',
'posts_per_page' => 6
)
);
//var_dump( $main );
if( $main->have_posts() ):
while( $main->have_posts() ): $main->the_post();
?>
<div class="col-8 p-4 mt-3">
<img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
<h4 class="card-title"><?php the_title(); ?></h4>
<p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p>
<small class="text-muted"><?php echo get_the_date( get_the_ID() ) ?></small>
<small class="text-muted"><?php the_author() ?></small>
<a class="" href="<?php the_permalink(); ?>"><?php _e('Leggi', ''); ?></a>
</div>
<?php
$side = new WP_Query(
array(
'post_type' => 'post',
'category_name' => 'Secondo piano',
'posts_per_page' => 6
)
);
if( $side->have_posts() ):
while( $side->have_posts() ): $side->the_post();
?>
<div class="col-4 p-4 mt-3">
<img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
<h4 class="card-title"><?php the_title(); ?></h4>
<p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p>
<small class="text-muted"><?php echo get_the_date( get_the_ID() ) ?></small>
<small class="text-muted"><?php the_author(); ?></small>
<a class="" href="<?php the_permalink(); ?>"><?php _e('Leggi', ''); ?></a>
</div>
<?php
endwhile;
wp_reset_query();
endif;
endwhile;
endif;
wp_reset_postdata();

Related

how to add pagination in child posts list

I created a custom post type called Programs, Each program contains episodes
Episodes will be Child Post
Some programs contain more than a hundred episodes, so I want to add pagination to my child posts list
I've had a basic attempt at this using $paged = (get_query_var('page')) ? get_query_var('page') : 1; and the paged argument in the WP_Query, but my next_posts_link doesn't work. my code to show child post
<?php
$args = array(
'post_type' => 'programs',
'posts_per_page' => 60,
'post_parent' => $post->ID,
'order' => 'ASC',
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="penci-border-arrow penci-homepage-title penci-magazine-title style-5 pcalign-right pciconp-right pcicon-right" style="border-color: #3E68B0">
<h3 class="inner-arrow" style="background:#3E68B0;border-color: #3E68B0">
الحلقات </h3>
</div>
<ul class="penci-wrapper-data penci-grid">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
$episode_number = get_field('episode_number', get_the_ID());
?>
<li class="list-post pclist-layout">
<article id="post-<?php the_ID(); ?>" class="item hentry">
<div class="thumbnail child-thumbnail">
<a class="penci-image-holder penci-lazy" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" style="background-image: url(<?php echo $featured_img_url?>);">
</a>
</div>
<div class="content-list-right content-list-center child-content">
<div class="header-list-style">
<div class="episode-details">
<span class=""> <a class="url fn n" href="<?php the_permalink(); ?>"><?php echo $episode_number ?></a></span>
<span style="margin:0 20px"><a>|</a></span>
<span class="featc-date"><time class="entry-date published" datetime="2021-10-31T13:26:29+00:00">2021-10-31</time>
</span>
<span class='program-viewcn'><?php pvc_stats_update( get_the_ID(), 1 ); ?></span>
</div>
<h2 class="grid-title entry-title">
<?php the_title(); ?>
</h2>
</div>
<div class="item-content entry-content">
<p>
<?php the_excerpt();?>
</p>
</div>
</div>
</article>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

how to add pagination to child post

I created custom post type and child post , i want to add pagination to child post , I've had a basic attempt at this using $paged = (get_query_var('page')) ? get_query_var('page') : 1; and the paged argument in the WP_Query, but my next_posts_link doesn't work.
my code to show child post
<?php
$args = array(
'post_type' => 'programs',
'posts_per_page' => 60,
'post_parent' => $post->ID,
'order' => 'ASC',
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<div class="penci-border-arrow penci-homepage-title penci-magazine-title style-5 pcalign-right pciconp-right pcicon-right" style="border-color: #3E68B0">
<h3 class="inner-arrow" style="background:#3E68B0;border-color: #3E68B0">
الحلقات </h3>
</div>
<ul class="penci-wrapper-data penci-grid">
<?php while ( $parent->have_posts() ) : $parent->the_post();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
$episode_number = get_field('episode_number', get_the_ID());
?>
<li class="list-post pclist-layout">
<article id="post-<?php the_ID(); ?>" class="item hentry">
<div class="thumbnail child-thumbnail">
<a class="penci-image-holder penci-lazy" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" style="background-image: url(<?php echo $featured_img_url?>);">
</a>
</div>
<div class="content-list-right content-list-center child-content">
<div class="header-list-style">
<div class="episode-details">
<span class=""> <a class="url fn n" href="<?php the_permalink(); ?>"><?php echo $episode_number ?></a></span>
<span style="margin:0 20px"><a>|</a></span>
<span class="featc-date"><time class="entry-date published" datetime="2021-10-31T13:26:29+00:00">2021-10-31</time>
</span>
<span class='program-viewcn'><?php pvc_stats_update( get_the_ID(), 1 ); ?></span>
</div>
<h2 class="grid-title entry-title">
<?php the_title(); ?>
</h2>
</div>
<div class="item-content entry-content">
<p>
<?php the_excerpt();?>
</p>
</div>
</div>
</article>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

Pagination WordPress on the home page

I'm having an issue trying to get pagination to work on the homepage of a site I'm working on.
Here is the code I'm using, simplified of course:
How to do pagination for this?
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; endif; ?>
<?php $args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3); ?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
First of all, you must setup properly your query:
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
And second, you need to add pagination links out of the loop
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
Whole code can looks like this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array('cat' => '3, 7, 10, 12', 'posts_per_page'=> 3, 'paged' => $paged);
?>
<?php query_posts($args); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
</div>
<?php endif; ?>
Problem has been solved:
<?php
global $paged, $wp_query, $wp;
$args1 = array('cat' => '3, 7, 10, 12');
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=3&cat=3,7,10,12');
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<article class="col-md-12">
<a href="<?php the_permalink(); ?>" >
<div class="image"><img class="img-responsive" src="<?php echo the_post_thumbnail_url( 'medium' ); ?>"></div></a>
<div class="entry entry-table">
<div class="title">
<?php the_title(); ?></h3>
<span class="cateogry<?php echo get_the_category( $id )[0]->cat_ID; ?>"><?php echo get_the_category( $id )[0]->name; ?></span>
<p><?php echo get_excerpt(228, 'content'); ?>
<a class="linkmore" href="<?php the_permalink() ?>">Czytaj dalej ...</a>
</p>
</div>
</div>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php $wp_query = null; $wp_query = $temp;?>

How to show 3 most recent posts in a special tiles on home page using wordpress?

I have these tile elements on the top of my homepage:
<div id="tile-holder" class="group">
<div class="tile1">
<div class="tile-textbox" style="color: #22284f;">Just Some Title</div>
</div>
<div class="tile2">
<img class="post-image" src="images/sample-large-2.jpg"/>
<div class="tile-datebox">
<img src="images/video-icon.png" >
<p>2013/2/25</p>
<div class="tile-info"><h1>Title 1</h1></div>
</div>
</div>
<div class="tile3">
<img class="post-image" src="images/sample-mid-2.jpg"/>
<div class="tile-datebox">
<img src="images/image-icon.png" >
<p>2013/5/15</p>
<div class="tile-info"><h1>Title 2</h1></div>
</div>
</div>
<div class="tile4">
<img class="post-image" src="images/sample-mid-3.jpg"/>
<div class="tile-datebox">
<img src="images/text-icon.png" >
<p>2013/6/17</p>
<div class="tile-info"><h1>Title 3</h1></div>
</div>
</div>
<div class="tile5">
<div class="tile-textbox" style="color: #ffffff;">Another Title</div>
</div>
</div> <!-- END Tile Holder -->
and I want to show 3 most recent posts in tile 2, 3 and 4; just the title, date and an image of that that gets the url from custom fields that I defined, I tried using :
.
.
.
<?php query_posts("post_per_page=3"); the_post(); ?>
<div class="tile2">
<img class="post-image" src="<?php echo get_post_meta($post->ID, 'post_image', true) ?>"/>
<div class="tile-datebox">
<img src="<?php echo get_post_meta($post->ID, 'post_icon', true) ?>" >
<p><?php the_time('F jS, Y') ?></p>
<div class="tile-info"><h1><?php the_title(); ?></h1></div>
</div>
</div>
<div class="tile3">
<img class="post-image" src="<?php echo get_post_meta($post->ID, 'post_image', true) ?>"/>
<div class="tile-datebox">
<img src="<?php echo get_post_meta($post->ID, 'post_icon', true) ?>" >
<p><?php the_time('F jS, Y') ?></p>
<div class="tile-info"><h1><?php the_title(); ?></h1></div>
</div>
</div>
<div class="tile4">
<img class="post-image" src="<?php echo get_post_meta($post->ID, 'post_image', true) ?>"/>
<div class="tile-datebox">
<img src="<?php echo get_post_meta($post->ID, 'post_icon', true) ?>" >
<p><?php the_time('F jS, Y') ?></p>
<div class="tile-info"><h1><?php the_title(); ?></h1></div>
</div>
</div>
<?php wp_reset_query(); ?>
.
.
.
but it just gets one post and shows it there, what's wrong? what did I missed? please help, I'm new in the world of wordpress!
Here is one way to do it. If you're new you should read up on these essential topics: The Loop and WP_Query.
<?php
$query = new WP_Query( 'posts_per_page=3' );
// The Loop
if ( $query->have_posts() ) {
$i = 1;
while ( $query->have_posts() ) {
$query->the_post();
$i++;
?>
<div class="tile<?php echo $i ?>">
<img class="post-image" src="<?php echo get_post_meta($post->ID, 'post_image', true) ?>"/>
<div class="tile-datebox">
<img src="<?php echo get_post_meta($post->ID, 'post_icon', true) ?>" >
<p><?php the_time('F jS, Y') ?></p>
<div class="tile-info"><h1><?php the_title(); ?></h1></div>
</div>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
<?php $args = array(
'numberposts' => 3,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
//write loop here
?>

Display content on first post of the month within a blogroll- Wordpress

What i am trying to do is create a timeline. What i want to happen is have the date echo in at the top of the first post of each month. So it will act like a separator to know now that the posts below are in this month.
Currently i am just querying all the posts in a category. Now what i want to happen is an if else statement. If its the first post of the the month, echo some text.
<?php $cat=get_field( 'time_taxonomy');?>
<?php global $post; $i=0; $args=array( 'numberposts'=>-1, 'offset'=> 0, 'category' => $cat ); $myposts = get_posts( $args ); foreach( $myposts as $post ) : if ($i==1) { } else {}; setup_postdata($post); $i++; ?>
<div class="ss-container">
<div class="none"><?php echo date("F"); ?></div>
<div class="ss-row">
<div class="ss-left">
<h2 id="month"><?php echo date("F"); ?></h2>
</div>
<div class="ss-right">
<h2><?php echo date("Y"); ?></h2>
</div>
</div>
<div class="ss-row ss-medium">
<!-- Pulling in the featured post imgae and title-->
<?php $image=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'timeline' ); ?>
<div class="ss-left">
<a href="<?php the_permalink(); ?>" class="ss-circle">
<img class="ss-circle-1" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</a>
</div>
<div class="ss-right">
<h3>
<span><?php echo date("F j, Y | g:i a"); ?></span>
<a href="#">
<?php the_title(); ?>
</a>
</h3>
</div>
</div>
</div>
<?php endforeach; ?>
<?php $cat=get_field( 'time_taxonomy');?>
<?php global $post; $i=0; $args=array( 'numberposts'=>-1, 'offset'=> 0, 'category' => $cat ); $myposts = get_posts( $args ); $prev_month = -1; foreach( $myposts as $post ) :
$post_month = date("m",strtotime($post["post_date"]));
if($post_month != $prev_month) {
// First post of the new month
} else {
// Still the same month
}
$prev_month = $post_month;
setup_postdata($post); $i++; ?>
<div class="ss-container">
<div class="none"><?php echo date("F"); ?></div>
<div class="ss-row">
<div class="ss-left">
<h2 id="month"><?php echo date("F"); ?></h2>
</div>
<div class="ss-right">
<h2><?php echo date("Y"); ?></h2>
</div>
</div>
<div class="ss-row ss-medium">
<!-- Pulling in the featured post imgae and title-->
<?php $image=wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'timeline' ); ?>
<div class="ss-left">
<a href="<?php the_permalink(); ?>" class="ss-circle">
<img class="ss-circle-1" src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</a>
</div>
<div class="ss-right">
<h3>
<span><?php echo date("F j, Y | g:i a"); ?></span>
<a href="#">
<?php the_title(); ?>
</a>
</h3>
</div>
</div>
</div>
<?php endforeach; ?>

Categories