WordPress Pagination is not working - wordpress

The pagination for this wordpress theme I am coding, don't seem to be working, any one with a better ideal? thank you in advance.
Below is my full code, I am kind of confused, had tried using a plugin and calling out the shortcode wp-pagenavi but its not working either, I would appreciate any help.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 10, 'paged' => $paged);
$query = new WP_Query($args);
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post(); ?>
<div class="media">
<div class="media-left">
<div class="hovereffect">
<?php the_post_thumbnail();?>
<div class="overlay">
<h2>Share</h2>
<p class="icon-links">
<a href="#">
<span class="fa fa-twitter"></span>
</a>
<a href="#">
<span class="fa fa-facebook"></span>
</a>
<a href="#">
<span class="fa fa-instagram"></span>
</a>
</p>
</div>
</div>
</div>
<div class="media-body">
<h4 class="media-heading"><?php the_title()?></h4>
<p class="media-author"><b><?php the_author()?></b> - <?php echo get_the_date(); ?></p>
<?php the_content();?>
</div>
</div>
<?php endwhile;?>
<!-- pagination -->
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php else : ?>
<!-- No posts found -->
<?php endif; ?>

You have to use $query->max_num_pages in the second parameter of next_posts_link as you can see here
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );

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

Wordpress posts page is not showing posts list

Im trying to setup custom posts page. I was trying to use home.php, and custom page template. After that i choose in the setting part "Reading Settings" my blog page. Problem is that when i go on www.domain.tld/blog im getting single blog post page, not a list of blogs. When i switch blog template to some other url, then everything is fine but still is not using custom template. In that case wp are using home.php
home.php
<?php
/**
* Blog listing
*/
?>
<?php get_header() ?>
<div id="blog" class="page_wrapper">
<div class="hedaer_mini_part">
<?php get_template_part('parts/navigation'); ?>
<div class="container">
<hr>
<h1>Blog</h1>
<div class="search">
<?php echo get_search_form( ); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 0;
$query = new WP_Query(array('category_name' => 'blog', 'post_type' => 'post', 'paged' => $paged));
if ($query->have_posts() || $paged >= 1 && $paged <= $wp_query->max_num_pages) :
?>
</div>
</div>
</div>
<div class="blog_lista">
<div class="container">
<div class="news_list_wrapper">
<?php
// Start the loop.
while ($query->have_posts()) : $query->the_post();
?>
<div class="news_list_item ffs-able">
<a href="<?php the_permalink(); ?>">
<div class="row">
<div class="col-sm-5">
<div class="news_list_image">
<!-- <img src="" /> -->
<?php if(has_post_thumbnail()) { ?>
<img src="<?php the_post_thumbnail_url('list_size'); ?>" alt="<?php the_title() ?>" />
<?php } ?>
</div>
</div>
<div class="col-sm-7">
<div class="news_list_content">
<h3 class="title"><?php the_title() ?></h3>
<div class="body">
<?php echo wp_trim_words(get_the_content(), 90); ?>
</div>
<div class="date">
<?php the_time('j. F Y.'); ?>
</div>
</div>
</div>
</div>
</a>
</div>
<?php
// End the loop.
endwhile;
?>
</div>
<div class="news_pagination">
<hr>
<?php the_posts_pagination(array('mid_size' => 1, 'prev_text' => '<', 'next_text' => '>')); ?>
</div>
<?php
else :
echo 'There is no blog posts..';
endif;
?>
</div>
</div>
</div>
<?php get_footer(); ?>
You need to add these codes to index.php and not home.php. That might fix the issue.

Wordpress loop only showing one record in query post

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

Wordpress: No search results message

I've tried to separate search results by categories in 2 tabs. Everything works fine except showing the message "No results". This message is shown only when in both categories are nothing found. But when one tab has results and another hasn't - nothing is shown.
I'm looking for way to show "No results" for every tab. I mean, if nothing is found in cat 1 and some results found in cat 2 -> Show "No results" in Tab 1 and show results in Tab 2.
Any suggestions?
Code here:
<div id="tab-content1" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 3,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--text">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><?php the_excerpt(); ?> </span>
<span> </span>
</span>
</div>
<div class="post-item--thumbnail">
<div class="post-thumbnail">
<div class="backgroundImage_1hK9M post-thumbnail--image" style="background-image: url('<?php echo $url; ?>');"></div>
<span></span>
<span></span>
</div>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content1 -->
<div id="tab-content2" class="tab-content">
<ul class="posts--group v-category-games">
<?php
global $post;
rewind_posts();
$query = new WP_Query(array(
'posts_per_page' => -1,
'cat' => 4,
's' => get_search_query(),
));
if (have_posts()) :
while ($query->have_posts()) : $query->the_post();
?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<li>
<div class="post-item v-category-games v-with-image">
<div class="post-item--circle">
<?php the_field('digest_number'); ?>
</div>
<div class="post-item--text no-margin">
<a class="post-item--text--name" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-item--text--tagline">
<span><p><?php the_field('short_description'); ?></p> </span>
<span> </span>
</span>
</div>
</div>
</li>
<?php
endwhile;
?>
<?php
else :
echo "<div class='art_descr'><h2>No results in this category!</h2></div></center>";
endif;
wp_reset_postdata();
?>
</ul>
</div> <!-- #tab-content2 -->
</div>
You can use your if (have_posts()) : on each loop to determine if to display or not...
if (have_posts()) :
//while loop
else:
echo '<h1>no posts found</h1>';
endif;

How to display from a post_type the first post and then to skip it in the next array

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>

Resources