Add pagination on display post wordpress - wordpress

I am using the below code to list the posts but there are more than 200 posts so i want to add pagination at the end.
<?php
// the query
$the_query = new WP_Query(array(
'category_name' => 'Reports',
'post_status' => 'publish',
));
?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="col-md-12 event_col">
<div class="col-md-2 event_date">
<span><?php echo the_post_thumbnail(null, 'medium');?></span>
</div>
<div class="col-md-7 event_venue">
<div class="titl">
<?php the_title(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>

Related

Pagination in Wordpress after the 5 latest posts

How do I add pagination to this code? It currently shows latest articles.
Pagination can be after every 3 or 5 or 10 posts.
<?php
$the_query = new WP_Query( array(
'category_name' => $category_name,
'offset' => 1, //ignore the first 4 posts and show remaining
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="home__full">
<div class="row">
<div class="col-md-3">
<small><?php the_time('F jS, Y') ?> • <?php echo reading_time(); ?> • <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></small>
</div>
<div class="col-md-6">
<h3><?php echo substr(get_the_title(),0,95); ?></h3>
<div class="home__latest--excerpt"><p><?php echo substr(get_the_excerpt(),0,140); ?></p></div>
</div>
<div class="col-md-3">
<a href="<?php the_permalink() ?>">
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/blog-thumbnail.png" alt="<?php echo the_title(); ?>" class="wp-post-image" />
<?php } ?>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>

Problem showing post for the term of the current page Wordpress

I'm trying to list the posts related to the current taxonomy page but it shows me all the posts instead. When I output the current term name is correct so really I don't know why this happens. I'm pasting here the code if anyone can put me in the right direction.
<?php
$term = get_queried_object();
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term->term_id
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Try with:
<?php
$term = get_queried_object()->term_id;
// Define the query
$args1 = array(
'post_type' => 'properties-for-sale',
'taxonomy' => $term
);
$query = new WP_Query($args1);
if ($query->have_posts()) { ?>
<h2 class="region-listing-title"><span class="thin-span">Luxury Properties for sale in</span> <?php echo $term->name; ?></h2>
<div class="swiper myswiper swiper-h">
<div class="swiper-wrapper">
<?php while ($query->have_posts()) : $query->the_post(); ?>
<div class="swiper-slide">
<div class="favorite-button"><?php the_favorites_button($post_id, $site_id); ?></div>
<div class="swiper myswiper2 swiper-v">
<div class="swiper-wrapper">
<?php
$images = get_field('listing_gallery');
if ($images) : ?>
<?php foreach ($images as $image) : ?>
<div class="swiper-slide highlight-img" style="background-image: url(<?php echo $image['url']; ?>)"></div>
<?php endforeach; ?>
<?php endif;
?>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="listing-informations">
<div id="lp-region-sale" class="listing-information-price">
<span><?php the_field('property_price'); ?> €</span>
</div>
<div>
<?php the_title(); ?>
</div>
<div class="listing-informations__listing-details">
<div class="listing-information-label"><span></span><?php the_field('number_of_bedrooms'); ?> Beds</div>
<div class="listing-information-label"><span></span><?php the_field('number_of_bathrooms'); ?> Baths</div>
<div class="listing-information-label"><span></span><?php the_field('interior_square_meters'); ?> Int Sqm</div>
<div class="listing-information-label"><span></span><?php the_field('lot_square_meters'); ?> Lot Sqm</div>
</div>
</div>
</div>
<?php endwhile;
} ?>
</div>
</div>
<?php wp_reset_postdata();
?>
Just use tax query.
<?php
$term = get_queried_object()->term_id;
$args = array(
'post_type' => 'properties-for-sale',
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term->term_id
)
)
);
$query = new WP_Query( $args ); ?>

WP-Page Navi doesn't work

How to solve this problem ? I really don't have any idea.
Plugin generate only this html :
<div class="wp-pagenavi">
<span class="pages">Page 1 of 1</span><span class="current">1</span>
</div>
This is my archive.php
<?php $loop = new WP_Query(array( 'orderby' => 'ASC', )); ?>
<?php if($loop->have_posts() ) : ?>
<?php while($loop->have_posts() ) : $loop->the_post(); ?>
<article class="article">
<div class="time"><?php the_time('Y-m-d'); ?></div>
<div class="h2"><?php the_title(); ?></div>
<div class="article__descript">
<?php $content = the_content();
$test =substr($content, 0, 20);
echo $test; ?>
</div>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); wp_reset_query(); ?>
<?php else : ?>
<?php endif; ?>
Try this. I have updated your code-
You have not passed the parameter paged in query arguments.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(array('post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 5,
'orderby' => 'date',
'post_status'=>'publish'
));
?>
<?php if($loop->have_posts() ) : ?>
<?php while($loop->have_posts() ) : $loop->the_post(); ?>
<article class="article">
<div class="time"><?php the_time('Y-m-d'); ?></div>
<div class="h2"><?php the_title(); ?></div>
<div class="article__descript">
<?php $content = the_content();
$test =substr($content, 0, 20);
echo $test; ?>
</div>
</article>
<?php endwhile; ?>
<?php wp_pagenavi(); wp_reset_query(); ?>
<?php else : ?>
<?php endif; ?>

How can I display posts side by side?

I created the WordPress loop below to display posts from a certain category in a row side by side. I'm having trouble because they are displayed on top of each other. I'm using Bootstrap 4
<?php
$args = array(
'category_name' => 'featured',
'posts_per_page' => 4
);
$the_query = new WP_Query($args);
?>
<?php if($the_query->have_posts()): while($the_query->have_posts()): $the_query->the_post(); ?>
<div class="row featured-row">
<div class="col-md-3">
<?php the_post_thumbnail( 'fimage', array('class' => 'img-fluid') ); ?>
<h5><?php the_title(); ?></h5>
<em>Posted on - <?php echo get_the_date(); ?></em>
<em>Written by - <?php the_author(); ?></em>
</div>
<?php endwhile; ?>
<?php endif; ?>
<hr>
</div>
<?php wp_reset_postdata(); ?>
Try these :
<?php
$args = array(
'category_name' => 'featured',
'posts_per_page' => 4
);
$the_query = new WP_Query($args);
?>
<?php if($the_query->have_posts()): ?>
<div class="row featured-row">
<?php while($the_query->have_posts()): $the_query->the_post(); ?>
<div class="col-md-3">
<?php the_post_thumbnail( 'fimage', array('class' => 'img-fluid') ); ?>
<h5><?php the_title(); ?></h5>
<em>Posted on - <?php echo get_the_date(); ?></em>
<em>Written by - <?php the_author(); ?></em>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Put your div with class row outside the loop. Use below code
<?php
$args = array(
'category_name' => 'featured',
'posts_per_page' => 4
);
$the_query = new WP_Query($args);
if($the_query->have_posts()):
echo ' <div class="row featured-row">';
while($the_query->have_posts()): $the_query->the_post(); ?>
<div class="col-md-3">
<?php the_post_thumbnail( 'fimage', array('class' => 'img-fluid') ); ?>
<h5><?php the_title(); ?></h5>
<em>Posted on - <?php echo get_the_date(); ?></em>
<em>Written by - <?php the_author(); ?></em>
</div>
<?php endwhile;
echo '</div>';
endif; ?>
<hr>
<?php wp_reset_postdata(); ?>

How to get post into homepage?

Hi all I am a new of wordpress,I use theme wordpress foundation, When I post sth, in my homepage can not see post ,and I try to write code in my homepage to get post but still can not see post,How can I do ,and here is my code:
page.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<div id="page-gina">
<?php
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
</div>
</div>
<?php get_footer(); ?>
Help me please !!!
<?php
/*Template Name: homepage template
*/
get_header();
?>
<?php
$args = array(
'cat' => '3',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<?php get_footer(); ?>
Note: In dashboard,
i)Create page called Home,
ii)Same page right side template list shown assign homepage template,
ii)In Settings->Reading->Assign front page.

Resources