how to add pagination in wordpress - wordpress

I have posts in different custom taxonomies. I need to display posts for one taxonomy in a page. Now I'm getting the all the posts in all taxonomies in a single page. I have a foreach loop for taxonomy and inside the loop all the posts corresponding to that taxonomy is displaying through post method.I have never used pagination before. How can I add pagination in this case?
I am enclosing my code below
<div class="main">
<section class="brands-sec product-sec">
<div class="container">
<?php
$siteurl = home_url('/');
$tax = 'product'; // slug of taxonomy
$terms = get_terms($tax);
foreach ($terms as $term) {
$id = $term->term_id;
$slug = $term->slug;
$description = $term->description;
$image_url = z_taxonomy_image_url( $id, NULL, TRUE ); // category image display
$link = "<a href='$siteurl?$tax=$slug' ><h1> $term->name </h1></a>";
echo '<img src="' . $image_url . '">'; ?>
<div class="col-md-8 pull-right col-sm-10 pull-right col-xs-12 brnd prdct">
<img src="<?php echo $image_url ; ?>" class="img-responsive pdt-logo" alt="loyd"/>
<div class="brand-logos pdt">
<p><?php echo $description ; ?></p>
<h4>Product</h4>
<?php $args = array("posts_per_page" => "-1", "product"=>$slug ,"post_type" => "products" );
$posts = get_posts($args);
foreach($posts as $data){
$thumb = wp_get_attachment_url( get_post_thumbnail_id($data ->ID) );
$custom_fonts = get_post_custom($data->ID);$custom_fonts_key= $custom_fonts['category'];$custom_fonts_array = $custom_fonts_key[0]; ?>
<div class="row mb40">
<div class="col-md-4 col-sm-4 col-xs-12 brand-single product-single">
<img src="<?php echo $thumb; ?>" class="img-responsive" alt="allegro products"/>
<h5><?php echo $data->post_title; ?></h5>
<p><?php echo $data->post_content; ?></p>
</div>
<div class="col-md-8 col-sm-8 col-xs-12 brand-single product-detail">
<ul class="products">
<?php
$attachments = new Attachments( 'my_attachments',$data->ID ); /* pass the instance name */ ?>
<?php if( $attachments->exist() ) : ?>
<?php while( $attachments->get() ) : ?>
<li><img src="<?php echo $attachments->url(); ?>" class="img-responsive" alt="allegro products"/></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php } ?>
</div><!--end brand-logos-->
</div><!--end brnd-->
<?php } ?>
</div>
Here product is custom taxonomy . In that there are more than one terms say loyd,Nycofee,.... In Both these terms there are more than one posts. I need to display posts in loyd in one page and ncofee in next page. How to add pagination in this case?

Try this:
You have the post_per_page to be -1
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array("posts_per_page" => "-1", "product"=>$slug ,"post_type" => "products" );
change it to the number you would like. and add to the end
'paged' => $paged
before Ending the PHP here
</div>
<?php } ?>
</div><!--end brand-logos-->
Add
<?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $args )); ?>
<?php wp_reset_postdata(); ?>
Here is the basic pagination for wordpress
<?php if ( have_posts() ) : ?>
<!-- Add the pagination functions here. -->
<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post(); ?>
<!-- the rest of your theme's main loop -->
<?php endwhile; ?>
<!-- End of the main loop -->
<!-- Add the pagination functions here. -->
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Soruce: https://codex.wordpress.org/Pagination "Example Loop with Pagination"
If it still show the full list.
Try this: http://callmenick.com/post/custom-wordpress-loop-with-pagination

Related

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>

Page title and content in wrong order (wordpress/bootstrap/php)

I'm having problem with my Wordpress 4.7.8 localhost custom-theme webpage.
As hard as I try, I keep getting "Title" where content should be and the content at the place of the title
Here is an image
Here is how it looks in wordpress admin dashboard:
...and image the view in wordpress
any Ideas where I might be going wrong?
Try this code.
Search bar in duplicate (wordpress/php/bootstrap)
This code taken from your previous question.
this code for displaying title <?php the_title(); ?> and this code displaying content <?php the_content(); ?>. you can replace this code wherever you want.
<?php get_header(); ?>
<div class="row">
<div class="col-xs-12 col-sm-8">
<div class="row text-center no-margin">
<?php
$currentPage = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3,'post_type'=>'post', 'paged' => $currentPage);
new WP_Query($args);
if( have_posts() ): $i = 0;
while( have_posts() ): the_post(); ?>
<?php
if($i==0): $column = 12; $class = '';
elseif($i > 0 && $i <= 2): $column = 6; $class = ' second-row-padding';
elseif($i > 2): $column = 4; $class = ' third-row-padding';
endif;
?>
<div class="col-xs-<?php echo $column; echo $class; ?> blog-item">
<?php if( has_post_thumbnail() ):
$urlImg = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ) );
endif; ?>
<div class="blog-element" style="background-image: url(<?php echo $urlImg; ?>);">
<!--<?php the_title( sprintf('<h1 class="entry-title">', esc_url( get_permalink() ) ),'</h1>' ); ?> -->
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<small><?php the_category(' '); ?></small>
</div>
</div>
<?php $i++; endwhile; ?>
<div class="col-xs-6 text-left">
<?php next_posts_link('« Older Posts'); ?>
</div>
<div class="col-xs-6 text-right">
<?php previous_posts_link('Newer Posts »'); ?>
</div>
<?php endif;
wp_reset_query();
?>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<?php get_sidebar(); ?>
</div>
</div>
You might need to edit content-single.php file to change the post content order. Your post content is loading first before post title.
Check if ,
the_content() or get_the_content()
is showing before ,
get_the_title() or the_title()

For some reason wp_reset_postdata isn't working for me.

Hoping someone can help me with this one - I've been able to debug a lot of code over the years with insight from this site.
I'm trying to run two wordpress queries on one page, but for some reason I can't clear out the data from the first query even after using wp_reset_postdata. What am I doing wrong? Should I be trying to query in some other way?
I also tried wp_reset_query, with no success.
This is in a custom theme - do I need something in functions.php to make this work? (I had to that once ages ago for post-thumbnails)
Here's the code:
$args1 = array( 'category' => 'locations' );
$myposts1 = get_posts( $args1 );
foreach ( $myposts1 as $post ) : setup_postdata( $post ); ?>
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2 class="post-title">
<?php the_title(); ?>
</h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<p class="post-title-meta">
<?php the_time('F jS, Y') ?>
</p>
<!-- Display the Post's Content in a div box. -->
<div class="post-entry">
<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
$post_thumbnail_id = get_post_thumbnail_id();
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="post-image">
<img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto; margin-bottom:40px;">
</div>
<?php } ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
</div><!-- post entry-->
</div> <!-- post -->
<?php endforeach;
wp_reset_postdata();
?>
</div> <!-- #col-md-5 -->
<!-- end of Locations -->
<!-- second column-->
<div class="col-md-5">
<h2>Latest </h2>
<?php
$args2 = array( 'category' => 'secondcategory' );
$myposts2 = get_posts( $args2 );
foreach ( $myposts2 as $post ) : setup_postdata( $post ); ?>
<div class="post">
<!-- Display the Title as a link to the Post's permalink. -->
<h2 class="post-title">
<?php the_title(); ?>
</h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<p class="post-title-meta">
<?php the_time('F jS, Y') ?>
</p>
<!-- Display the Post's Content in a div box. -->
<div class="post-entry">
<?php if ( ( function_exists('has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
$post_thumbnail_id = get_post_thumbnail_id();
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?>
<div class="post-image">
<img title="image title" alt="thumb image" class="wp-post-image" src="<?php echo $post_thumbnail_url; ?>" style="width:100%; height:auto; margin-bottom:40px;">
</div>
<?php } ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
</div><!-- post entry-->
</div> <!-- post -->
<?php endforeach;
wp_reset_postdata();
?>
</div> <!-- #col-md-5 -->
<!-- end of secondcatagory -->

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 won't show in 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; ?>

Resources