I use custom post type for my journal post but I want to add navigation for previous and next page. So If I have more than 5 post on my journal page, I will page next link or the number 2,3,4, etc... If there is no more post, it should display previous. Right now, my journal page in on the index.php .
And in my wordpress reading settings, I use a static page for my front page displays. My front page is front-page.php and my posts page is journal page. Blog pages show at most 5 posts. Syndication feeds show the most recent 5posts.
How do I add next and previous navigation using my custom post type "journals"?
<?php
get_header();
?>
<!-- journal -->
<section class="container-wrap">
<?php
$args = array('post_type' => 'journals');
$query = new WP_Query($args);
while($query -> have_posts()) : $query -> the_post();
?>
<article class="post-wrap">
<header>
<a href="<?php the_permalink(); ?>" class="post-title">
<h1 class="post-title"><?php the_title(); ?></h1>
</a>
<span class="post-date"><?php echo(types_render_field('date', array('format' => 'm.d.Y') )); ?></span>
</header>
</article>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</section>
<!-- /journal -->
<?php
get_footer();
?>
Use the paginate_links hook in combination with a custom WP_Query array. Make sure to specify the paged array parameter for the query. This sets up the query to give back paged results.
<?php
// 1- Setup paging parameter
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
// 2- Setup WP_query variable to get last 12 posts
$args = array(
'posts_per_page' => 12,
'post_type' => 'journals',
'orderby' => 'most_recent',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
// 3- Setup new loop
if($the_query->have_posts()) :
while($the_query->have_posts()) :
$the_query->the_post();
// 4- Output parameters you want here
echo '<div class="col-md-4">';
echo '<h4>' . the_title() . '</h4>';
echo '' . the_post_thumbnail() . '';
echo the_excerpt();
echo '</div>';
// 5- close up loop
endwhile;
endif;
// 6- Output paginate_links just below post loop
echo paginate_links( array(
'base' => str_replace( 999999, '%#%', esc_url( get_pagenum_link( 999999 ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
// 7- reset post data query
wp_reset_postdata();
?>
Try this my code it's working on my site http://www.thehiddenwhy.com/blog/ please this link How to create pagination in page code in wordpress?
Related
Replacement of a poorly worded, (deleted) question from yesterday.
I have a pre-built website that needs Wordpress added to it consisting of two pages.
Paginated list of blog posts, 5 per page, (with excerpts). Using the post title as the link to full post.
Full post permalink, (clicked through from page one).
I have part one done and working using the following loop..
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><?php the_title(); ?></h2>
<!-- Display the date and a link to other posts by this posts author.-->
<small><?php the_time('F jS, Y'); ?> by <?php the_author(); ?></small>
<!-- Display the Post as Excerpt. -->
<div class="blogpost">
<?php the_excerpt($more_link_text); ?>
</div>
<hr>
<?php
endwhile;
$pagetotal = $loop->max_num_pages;
if ($pagetotal > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $pagetotal,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
}
wp_reset_postdata(); ?>
The above works great, including the pagination aspect, I am now however stuck as to what I need to do to with this code to get the Permalinks to display as just the full post, (currently it just reloads the loop list).
I assume that both the list page and the permalink page use index.php?
Permalinks are set up as http://www.mywebsite.com/blog/this-is-a-post in Wordpress setting and the relevant .htaccess is up.
Thanks in advance for any light or links shed on this..
When you say you want to display the "Permalink page", are you just referring to displaying the full blog post, no listing? If so, you'll need to create a file called single.php and include the Loop (minimally).
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
You can get more information on creating this file and how it impacts the display of your content here:
https://codex.wordpress.org/The_Loop_in_Action#Different_Single_Post_Format
I have a custom post type where I don't want the posts to be visible individually, so I set,
'publicly_queryable' => false,
'rewrite' => array('slug' => 'custom-page-slug'),
I then have the following query on the page:
<?php
$paged = 2;
$args = array( 'post_type' => 'sample_post_type', 'posts_per_page' => 2, 'paged' => $paged, order => "DESC" );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<h2>' . esc_html( get_the_title() ) . '</h2>';
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
My issue is, when I try to use get_next_posts_link() and get_previous_posts_link(), they simply link to the same page (they don't go to page/2 or page/1).
Here is my code for the prev and next links:
<?php if ($loop->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="clearfix prev-next-arrows">
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_next_posts_link( 'Next Page »', $query->max_num_pages ); // display older posts link ?>
</div>
<div class="prev-posts-link">
<?php echo get_previous_posts_link( '« Previous Page' ); // display newer posts link ?>
</div>
</nav>
</div>
<?php } ?>
Anyone know what would be wrong with my current setup?
Thanks
I think instead of echo get_next_posts_link(...) and echo get_previous_posts_link(...) you should use next_posts_link(...) and previous_posts_link(...) (without the echo and without the get_ part, at least that's how it works in my websites.
Also, before you define the arguments, insert this line (instead of $paged = 2;):
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
I figured it out. I had the following in my custom post type args:
'rewrite' => array('slug' => 'post-type-slug')
It was the same slug as the page I had the loop on
I am creating my custom theme and there's Custom Post Type too.
However, pagination for the Custom Post Type is not working. I tried all the possible solution from Stack Overflow but all in vain.
Here's the code:
<?php global $wp_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 3,
'post_type' => 'services',
'orderby' => 'date',
'order' => 'DESC',
'nopaging' => false,
'paged'=>$paged
);
$the_query = new WP_Query( $args ); ?>
<div class="service-content clearfix">
<ul class="clearfix">
<?php if ( $the_query->have_posts() ) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php $word_count = strlen( wp_strip_all_tags($post->post_content));
$id = get_the_ID();?>
<li class="col-sm-4 wow fadeInDown animated" data-wow-delay="300ms" data-wow-duration="500ms">
<figure class="image">
<?php the_post_thumbnail( 'medium' ); ?>
</figure>
<?php if($word_count<269){ ?>
<h3><?php echo $post->post_title; ?></h3>
<p><?php echo $post->post_content; ?></p>
<?php } else{ ?>
<h3><?php echo $post->post_title; ?></h3>
<?php echo $post->post_content; ?>
<?php } ?>
</li>
<?php endwhile;
next_posts_link();
previous_posts_link();?>
<?php wp_reset_query(); ?>
<?php endif; ?>
</ul>
</div>
Here, posts_per_page is working but Pagination not working , any help?
Please use bellow codes as your need..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$next = $paged+1;
$prev= $paged-1;
<a href="<?php echo '/page/'.$next; ?>" >NEXT</a>
<a href="<?php echo '/page/'.$prev; ?>" >PREV</a>
Just use paginate_links function
to display the pagination based on your query and paging
$paginate_args = array(
'base' => '%_%',
'format' => '?paged=%#%',
'total' => $the_query ->max_num_pages,
'current' => $paged,
'prev_text' => __('«'),
'next_text' => __('»'),
);
echo paginate_links( $paginate_args );
Make sure the base and format are correct. based on your permalink structure
One of the problems with pagination in Wordpress is that the Posts per page value is ignored and the Blog pages show at most setting in the Reading page of the Wordpress settings is taken as the actual value. So, you are trying to show 3 per page and on the first page that works. However, when you go to the second page, Wordpress is loading a different offset so your code will not work.
This issue has been explained extremely well over on the Wordpress Stack Exchange so I won't repeat that user's answer. You can read more here:
https://wordpress.stackexchange.com/questions/30757/change-posts-per-page-count
That should sort out the problem for you.
I have a custom page which lists ALL of the posts no matter the category, I have hit a brick wall with the pagination! for some reason the pagination ins't showing up!
Here is my code
<?php // Template Name: News Feed ?>
<?php get_header(); ?>
<div id="content">
<div class="container">
<div class="row">
<div class="col-md-3">
<div id="left-sidebar">
<?php get_sidebar('posts') ?>
</div>
</div>
<div class="col-md-6">
<?php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
);
$my_query = new WP_Query($args);
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="post">
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php previous_posts_link('Previous Entries'); ?></div>
<div class="alignright"><?php next_posts_link('Next Entries'); ?></div>
</div>
</div>
<div class="col-md-3">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
Your links will not show up because next_posts_link() is set to the $max_num_pages parameter of the main query ($wp_query->max_num_pages) by default. On pages, this will always be 1 and by default these links don't display when there is only one page
Also, you are not paginating your query, so even if you gt your links working, you will see the same posts being repeated.
You should add the paged parameter to your query like this
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
// Rest of you arguments
);
and then you need to alter the $max_pages parameter in next_posts_link() like this
next_posts_link( 'Next entries', $my_query->max_num_pages );
First try to add 'posts_per_page' parameter into the $args, so the loop will know how much posts to display in the page.
Second,From WordPress Codex :
If the pagination is broken on a static front page you have to add the "paged" >parameter this way:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts('posts_per_page=3&paged=' . $paged);
?>
That means Just add this code before your args (without the "query_posts") and then but the args like this :
$args = array(
'post_type' => 'post',
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
I am recommending to explore the codex once again, maybe there is something there that can help you.
http://codex.wordpress.org/Pagination#Example_Loop_with_Pagination
You have created custom template so you should try with the following link first:
https://wordpress.org/support/topic/pagination-not-working-on-custom-template-1
Also, I request you to go through this link and it explain everything in details
By default, in any given context, WordPress uses the main query to determine pagination. The main query object is stored in the $wp_query global, which is also used to output the main query loop:
if ( have_posts() ) : while ( have_posts() ) : the_post();
When you use a custom query, you create an entirely separate query object:
$custom_query = new WP_Query( $custom_query_args );
And that query is output via an entirely separate loop:
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
But pagination template tags, including previous_posts_link(), next_posts_link(), posts_nav_link(), and paginate_links(), base their output on the main query object, $wp_query. That main query may or may not be paginated. If the current context is a custom page template, for example, the main $wp_query object will consist of only a single post - that of the ID of the page to which the custom page template is assigned.
If the current context is an archive index of some sort, the main $wp_query may consist of enough posts to cause pagination, which leads to the next part of the problem: for the main $wp_query object, WordPress will pass a paged parameter to the query, based on the paged URL query variable. When the query is fetched, that paged parameter will be used to determine which set of paginated posts to return. If a displayed pagination link is clicked, and the next page loaded, your custom query won't have any way to know that the pagination has changed.
https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
I hope this will work for you
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Would you please try above code?
I want to show some posts in the homepage of my wordpress site..How will I do it??or is there any plugin that can help me do it?or is there shortcodes that could pull out and display those post in my homepage?
If you want to do it the easy way, you can use a plugin like Display Posts Shortcode.
Or, if you want to do it manually, you can use get_posts().
Here's an example you could use:
<?php
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<?php the_title(); ?></h2>
<?php endforeach;
}
}
?>
I hope this helps!
The implemetation varies greatly by theme.
Check if your wordpress theme has a file called index.php.
If you have this file for your current theme, this is the file responsible for displaying your home page. And this is where you will have to put the code snippets to display posts.
Presuming that you know a bit of html and PHP you will have to decide the suitable place within index.php to add the code suggested above by Amal Murali.
If you want to show a specific category post on Homepage you can use category slug or category name. Like the below to show and use the wp_pagenavi() plugin to show pagination and present it.
<?php
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'CATEGORY NAME ',
'posts_per_page' => 5,
'paged' => $paged,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
'query' => $arr_posts,
)
);
endif;
?>