Page navigation won't show up for the Homepage - php

I use PHP Code for post to place PHP into a template. Everything works fine, but the page navigation won't show up.
Here's a simple loop from my site. It was designed in local host, so I can't give you the address.
Here is my code:
<?php $lastest= new WP_Query ( array
(
'post_type' => post,
'posts_per_page' => 10,
'ignore_sticky_posts' => 1
)
);
?>
<div id="content-wrap">
<?php while($lastest->have_posts()) : $lastest->the_post(); ?>
<div id="lastest-new-wrap">
<div id="thumbnail-wrap">
<div id="thumbnail-size"><?php the_post_thumbnail(); ?></div>
</div>
<h1 id="title"><?php the_title(); ?></h1>
<div id="post-desc"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
</div>
<?php wp_reset_postdata(); ?>
Thx you for reply !
But the text link don't show . I mean Older post and New post
I'm using this php code to call it :
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
Note : First time , i use "Your latest posts" at Homepage . It show good but Paged is not right !
Second time , i use a Static Page for Homepage . Then the text link don't show up ! Do use know why ? I use PHP Code for Post to insert PHP Code in Page . Then I Design one unique Page named "HomePage" . Design this Page with
"WPBakery Visual Composer"
If u know how to fix it plz help me ! Thx so much

You are using custom query, so you need to add $paged parameter like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
<?php $lastest= new WP_Query ( array
(
'post_type' => post,
'posts_per_page' => 10,
'ignore_sticky_posts' => 1,
'paged' => $paged
)
);
?>

Related

How to display last published post?

I have a task to modify the code in one website so that it shows the latest post on the top of the page. The rest of the page is blog posts and this part of the code is fine. I'm just starting to learn PHP and have no clue what I need to do.
Here is the website so you can have a better understanding of what I need - https://yourbestbrace.staging.wpengine.com/
The post that is currently on top is not the last one published.
Also, the piece of the code that needs to be changed is placed in the header.php. Here it is ( full code screenshot since it wouldn't display the code properly, image part is missing - https://prnt.sc/oy6r8q):
$the_query = new WP_Query( array(
'posts_per_page' => 1,
'meta_key' => 'show_as_first',
'meta_value' => 1
));
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1 class="new-home-title">Products Reviews of the Best Braces & Supports for You</h1>
<div class="header-post">
<div class="h-post-detail">
<p class="sub-title-home">Featured Review</p>
<h2> <?php the_title(); ?></h2>
<p><?php echo wp_trim_words( get_the_excerpt(), 22 ); ?></p>
Read More
</div>
<?php endif;

Wordpress - How to add pagination to my code

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

Wordpress Pagination wp_query

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?

wordpress custom post type pagination -- previous posts link showing when no previous posts exist

Lots of questions about custom post types and pagination, but as far as I can find, no-one else with this problem:
Post type created -- check
new query for custom archive page -- check
page one totally loads correctly -- check
page two shows posts it should -- check. BUT: it also still has the previous posts link.
which point to page three, even though there are no posts to display
The problem: post navigation still shows on page two, allowing for a click to page 3 (4, 5, 6 etc) -- where there are no posts. No 404, just a blank page, as though the loop is still looping through an infinite sea of nothingness.
Code:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array(
'post_type' => 'portfolio',
'posts_per_page' => 10,
'paged'=>$paged
) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<article >
/*stuff in here */
</article>
<?php endwhile; ?>
<div id="post-navigation">
<div class="nav-previous"><?php next_posts_link(__( 'Previous Projects' )) ?></div>
<div class="nav-next"><?php previous_posts_link(__( 'Next Projects' )) ?></div>
</div><!-- #post-navigation -->
Other info: I'm displaying this with archive-portfolio.php -- no blank page to muddy up permalinks.
On page 1, there's no option for next post -- so that half seems to be working, but previous just lets me go back in time forever, where there are no posts to display.
Many thanks.
Ok -- asked too soon. Here's the code that fixes the problem. If anyone can tell me why, that would be awesome. Otherwise it's all cargo cult....
<?php if (have_posts()) : ?>
<?php query_posts('post_type=portfolio&posts_per_page=10&caller_get_posts=1&paged='. $paged ); ?>
<?php while (have_posts()) : the_post(); ?>
<article >
/*stuff in here */
</article>
<?php endwhile; ?>
<div id="post-navigation">
<div class="nav-previous"><?php next_posts_link(__( 'Previous Projects' )) ?></div>
<div class="nav-next"><?php previous_posts_link(__( 'Next Projects' )) ?></div>
</div><!-- #post-navigation -->
<?php else : endif; ?>
Try this:
You can refer from here on wordpress codex:
--
Thanks

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

Categories