Wordpress - How to add pagination to my code - php

<?php
$wpb_all_query = new WP_Query(array(
'post_type'=>'post', 'post_status'=>'publish',
'posts_per_page'=>-1
));
if ( $wpb_all_query->have_posts() ) : ?>
<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post();
$cats = get_the_category();
if ($cats[0]->cat_name !== 'Coaching' && $cats[0]->cat_name !== 'Jobs') { ?>
<div class="callout horizontal word-wrap"
data-category="
<?php
echo $cats[0]->cat_name;
?>">
<?php the_post_thumbnail() ?>
<h5><?php the_title(); ?></h5>
<?php the_content(); ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
What is the most simplist way to add pagination to these post in wordpress showing 5 posts per page?
I then want to use ajax to replace the pagination to update the posts that are shown.
I'm looking for an answer that also explains the posts_per_page as I thought this is what I would need to make my pagination .

With this args you can display posts.
$posts_per_page = get_option('posts_per_page');
$args = array('paged'=>$paged,
'posts_per_page'=>$posts_per_page,
'post_type'=> 'post',
'post_status'=>'publish'
);
query_posts($args);
Then use Wp pagination

Go to your WP-Admin => Setting => Reading
And set Blog pages show at most = number post you want show in one page
That's it!

Related

WordPress loop breaking page, loading blank page

I'm an apprentice WP developer and am trying to wrap up a project for work. I'm trying to loop in the first 6 or so posts on the homepage, but every time I add the loop the page breaks and loads empty (except for the header).
I'm using the HTML5 blank WordPress theme as the basis for the custom theme I'm developing and using ACF to create a custom page builder. The homepage is loading from page.php. Running WP 4.9.5.
<?php $args = array ( 'post_type' => 'post' );
$post_query = new WP_Query($args); ?>
<?php if($post_query->have_posts()): ?>
<?php while($post_query->have_posts() : $post_query->thepost(); ?>
<h2>THIS IS A TEST</h2>
<?php wp_reset_postdata(); endwhile; ?>
<?php else :?>
<p>Whoops. No posts.</p>
<?php endif; ?>
This is your problem: $post_query->thepost();. It should be: $post_query->the_post();. And you're missing a parenthesis after while($post_query->have_posts().
Also, wp_reset_postdata() should be called after the while loop.
Here's the updated code (with a couple of tweaks):
<?php
$args = array (
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if ( $post_query->have_posts() ):
while( $post_query->have_posts() ) : $post_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php
endwhile;
wp_reset_postdata();
?>
<?php
else : // No posts found.
?>
<p>Whoops. No posts.</p>
<?php
endif;

how to loop through posts in both front page and blog page?

i want to display some posts in home page according to post type but i can't access posts and post loop returns " Home " post only ,so what is the proper way to make this happen ?
my reading settings
Front page displays : A static page
Front page :home
Posts page :blogs
Keep your reading settings the way they are.
You can create a theme file called front-page.php and use this to control your home page.
Within your front-page.php you can use get_posts to retrieve posts and then loop through them
Example:
<?php get_header(); ?>
<?php
$args = array(
'numberposts' => 10, // number of posts to return
'post_type' => 'your-post-type' // change this to the post type you want to retrieve
);
$posts = get_posts( $args );
if ( $posts ) :
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<article <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>

Wordpress Shortcode to call latest posts

I created a custom page template to display the latest 12 posts with their respective title and excerpt, but I tought that It would be easier if I could call this with a shortcode.
This is the loop in "post-grid.php" which calls to those 3 things.
<section class="post-grid">
<?php
$grid = array('post_per_page' => 12);
$query = new WP_Query( $grid );
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="grid-posts">
<h2><?php the_title(); ?></h2><br>
<?php the_post_thumbnail('featured'); ?><br>
<?php the_excerpt() ?><br>
</div>
<?php endwhile; // end of the loop. ?>
</section>
How can I create a shortcode that executes that loop?
I know how to add a shortcode using
add_shortcode('postGrid', 'postGrid');
function postGrid()
{
//Code here
}
But im not sure how to implement the above as a function. I appreciate your help!
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'paged' => $page,
);
query_posts($args);?>
hope this will help you :)
Point related to add Post Thumbnail:
// check if the post has a Post Thumbnail assigned to it.
<?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_content(); ?>
Hope this help you :)
Since you aren't editing any code - you are creating your own - just put all that code in the call back function as is and it should work.
add_shortcode('postGrid', 'postGrid');
function postGrid()
{
<section class="post-grid">
<?php
$grid = array('post_per_page' => 12);
$query = new WP_Query( $grid );
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="grid-posts">
<h2><?php the_title(); ?></h2><br>
<?php the_post_thumbnail('featured'); ?><br>
<?php the_excerpt() ?><br>
</div>
<?php endwhile; // end of the loop. ?>
</section>
}

How to add posts in wordpress pages?

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

Wordpress add pagination for custom loop that show subpages

I have one page (not an article) with n subpages.
In the main page, I need to show max 3 titles of the subpages and insert a pagination for the other.
How can I do that?
This is my simple code now:
<?php
$parent_id = 14; //main page id
$pages = get_pages( array( 'sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id ) );
foreach ( $pages as $page ) : ?>
<div class="item">
<div class="item-title">
<h2><?php echo $page->post_title; ?></h2>
</div>
</div>
<?php endforeach; ?>
Thanks.
I solved by myself, the solution is to use wp_query() to create a new loop insted of using get_pages().
Here the new code for page title and contentwith pagination by Preeti Dua from Avigma Technology:
<?php
// Pagination variable
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// The Query
$the_query = new WP_Query( array( 'post_parent' => 782, 'post_type' => 'page', 'paged' => $paged) );
// The Loop
if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
global $post;
$thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */
?>
<div id="side-post-title">
<?php the_title(); ?>
</div>
<div id="side-post-excerpt">
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink( $page->ID ); ?>"> <div id="read-more">
<img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>
</div>
<?php endwhile; endif; ?>
<nav class="navigation">
<div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div>
<div style="float:right;"> <?php previous_posts_link('Show newer') ?></div>
</nav>
not sure,
but try following plugin
http://wordpress.org/extend/plugins/wp-pagenavi/
If you'd like to add subPage title, description or even thumbnail in pagination button you can use the ACP free Wordpress plugin: http://wordpress.org/plugins/advanced-content-pagination/

Categories