pagination won't show in Wordpress - wordpress

I have this code to show all posts in the blog by 3 per page (for specific reasons I have to use Wp_Query). I can't figure out how to add pagination to this. Here is my code for the post listing:
<?php
$all_posts = new WP_Query(array(
'posts_per_page' => 3,
));
if ($all_posts->have_posts()): while ($all_posts->have_posts()) : $all_posts->the_post();
?>
<div class="row leftNestedRow whiteWrapper boxyShadow">
<div class="large-12 columns">
<h2><?php the_title(); ?></h2>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}?>
<p class="authorDate">Av <a>Fryk</a>, <?php the_time('Y-m-d'); ?></p>
<p><span class="homepageBlogExerpt"><?php echo get_the_excerpt(); ?></span>[Läs mer]</p>
<ul class="tags">
<?php
$links = array();
foreach(get_the_tags() as $this_tag) {
if ($this_tag->name != "featured"){
$links[] = '<li>'.$this_tag->name.'</li>';
}
}
$ai = 0;
while ($links[$ai]) {
echo $links[$ai];
$ai++;
}
?>
</ul>
</div>
</div>
<?php endwhile; ?>

Related

Worpress sort the posts

In wordpress Tag page(tag.pgp). I have a left menu like this.
<div class="well">
<div>
ALphabatically <br/>
</div>
<div >
By category <br/>
</div>
</div>
I have all the posts on right side in The loop.
<?php
while(have_posts()) {
the_post(); ?>
<div >
<h2><?php the_title(); ?></h2>
<div>
<p>Posted by <?php the_author_posts_link(); ?> on <?php the_time('n.j.y'); ?> in <?php echo get_the_category_list(', '); ?></p>
</div>
<div>
<?php the_excerpt(); ?>
<p>Continue reading »</p>
</div>
</div>
<?php }
echo paginate_links();
?>
How can I sort the post alphabetically and by category by clicking on the links on left.
<?php
$args_product = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby'=>'title',
'order'=>'ASC',
'product_cat' => $all_categories_data->slug
);
$loop_product = new WP_Query( $args_product );
while ( $loop_product->have_posts() ) : $loop_product->the_post();
?>
<div >
<h2><?php the_title(); ?></h2>
<div>
<p>Posted by <?php the_author_posts_link(); ?> on <?php the_time('n.j.y'); ?> in <?php echo get_the_category_list(', '); ?></p>
</div>
<div>
<?php the_excerpt(); ?>
<p>Continue reading »</p>
</div>
<div>
<span><?php echo $all_categories_data->name;?></span>
</div>
</div>
<?php endwhile;?>
Use pre_get_posts action hook provided by WordPress.
Then, in your functions.php add the following code below:
function custom_pre_get_posts($query) {
// validate
if(!is_admin() && $query->is_main_query()) {
if(is_archive()) {
$query->set('orderby', 'title'); // order posts by title
$query->set('order', 'ASC'); // and in ascending order
}
}
}
add_action('pre_get_posts', 'custom_pre_get_posts');
For futher information visit https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

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 paginate custom post type in wordpress of custom taxonomy

I want to implement pagination on custom post type of a custom taxonomy.
but it is not showing the pagination links only shows on default posts but not working. When I click on next link it redirect me to the index page.
Any help.(New to Wordpress)
Code
<?php $term = get_queried_object();
$taxonomy = get_taxonomy($term->taxonomy);
?>
<div class="panel-heading"><h4><?php echo 'محصولات : '.$term->name;?></h4></div>
<div class="panel-body">
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_type'=>'my_product', 'taxonomy'=>$taxonomy->name,
'posts_per_page'=> 1,
'term'=>$term->slug, 'paged'=>$paged);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()): $query->the_post(); ?>
<div class="thumbnail">
<?php if (has_post_thumbnail()) {
the_post_thumbnail('featured');
} ?>
<div class="caption caption-content">
<h3><?php the_title(); ?></h3>
<p class="text-muted">
<!-- <strong>نویسنده:</strong> <?php //the_author();?> -->
تاریخ: <?php the_date(); ?></p>
<p> <?php the_excerpt(); ?> </p>
<div>
<p class="price-box">
<i class="fa fa-circle"> </i>قیمت:
<?php if (the_field('price') == '') {
// echo "00.00";
} else {
the_field('price');
} ?>
</p>
</div>
</div>
<hr>
</div>
<?php endwhile;
}
else {
echo '<h3>هیچ موردی درین بخش یافت نشد.</h3>';
}
?>
<!-- pagination here -->
<p> <?php
if (function_exists("custom_pagination")):
custom_pagination($custom_query->max_num_pages,"",$paged);
endif;
?>
<?php wp_reset_postdata(); ?>
</p>
</div>

How do I display the second and third most recent posts from a category in WordPress

I am displaying previews of the three most recent news articles on my homepage. The most recent post will be displayed in a different format to the second and third most recent posts.
I am currently displaying all three the same with the following code
<?php query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
How can I add another loop which will separate the second and third most recent posts from the most recent post?
I did not want to use postID as the posts will change.
The two arguments in the WP_Query function below combine to retrieve the second most recent post, then the HTML below displays that post.
<div class="video-message">
<p>
<ul>
<!-- // Define our WP Query Parameters -->
<?php
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 1 ) );
?>
<!-- // Start our WP Query -->
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<!-- // Display the Post Title with Hyperlink -->
<p><?php the_title(); ?></p>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
</p>
</div>
</div>
To display the third most recent post would require changing the offset value to 2, so that the program skips over the two most recent posts.
$the_query = new WP_Query( array( 'posts_per_page' => 1,'offset' => 2 ) );
This method is discussed in the Pagination Parameters section of the WordPress Code Reference.
Untested but you could try:
<?php
$count = 0;
query_posts('cat=2 && showposts=3');
if (have_posts()) : while (have_posts()) : the_post();
if($count == 0)
{
?>
<div class="col-xs-12 col-sm-4">
<div class="column">
<div class="news-article">
<p class="news-date"><?php the_time( get_option( 'date_format' ) ); ?></p>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('full', array( 'class' => 'img-responsive img-rounded news-img' )); } ?>
<p class="news-headline"><?php the_title(); ?></p>
</a>
<p><?php the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">
<p class="pull-right">Read more...</p>
</a>
<span class="clearfix"></span>
</div>
</div>
</div>
<?php
$count = 1;
}
else
{
//Some other layout here
}
endwhile;
endif;
?>
The above will check if $count is 0 and if it is, then do the layout and the count will then equal to 1. So the next time around $count won't be 0, so it will run what is in the else (which will be your layout).
I use WP_Query like this to show all posts from the fourth most recent one:
<?php
$query4 = new WP_Query( 'posts_per_page=4&offset=3' );
?>

Pagination don't work in custom page template

I created a custom page template like category.php but I can't get the pagination working.
I just filter post from one category.
Here is my code:
<?php
/* Template Name: News */
?>
<?php get_header(); ?>
<div class="col-lg-9 col-md-8 content">
<div class="box">
<h1 class="title"><?php the_title(); ?></h1>
<div class="box-int">
<article>
<?php // Display blog posts on any page # http://m0n.co/l
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('showposts=5' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php
if (has_post_thumbnail()) {
the_post_thumbnail();
}
?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<br><br>
<?php endwhile; ?>
<?php if ($paged > 1) { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Old'); ?></div>
<div class="next"><?php previous_posts_link('Newx »'); ?></div>
</nav>
<?php } else { ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('« Old'); ?></div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</article>
</div>
</div>
</div>
<div class="col-lg-3 col-md-4">
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
What's wrong here? I click in Page 2, but the post are always the same.
Your query is a bit of a mess here with some syntax errors as well.showposts died with the dinosaurs, it is long time depreciated. You should use posts_per_page. 'showposts=5' . '&paged='.$paged is also a mess. You query shouls simplify look like this
$args = array(
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post(); ?>
When querying the loop with WP_Query, you need to assign $max_pages parameter to the next_posts_link().
So you would need to change <?php next_posts_link('« Old'); ?> to <?php next_posts_link('« Old', $the_query->max_num_pages ); ?>
Also go and read this question on this subject. If I missed something, this question will most certainly help you out a lot.

Resources