Problem with pagination in Wordpress custom template - php

I created a new template in Wordpress to show the latest articles. After, I will sort them in other ways. My problem is I can not use the pagination correctly.
The next page and previous page are working ok, but the posts are not changed. The first three are the only that are shown.
Can you give me a clue about this ?
<?php
/*
Template Name: Latest Prizes
*/
get_header();
// The Query
query_posts( 'posts_per_page=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
posts_nav_link(' · ', 'previous page', 'next page');
// Reset Query
wp_reset_query();
get_footer(); ?>

From the Wordpress Codex on query_posts():
...
For example, on the homepage, you would normally see the latest 10 posts. If you want to show only 5 posts (and don't care about pagination), you can use query_posts() like so:
query_posts( 'posts_per_page=5' );
Change your query_posts() to this:
query_posts(array('posts_per_page' => 3, 'paged' => get_query_var('page')));

Related

wp_query pagination on archive.php

I'm using the following wp_query on a few pages in Wordpress, and as you can see I am trying to ensure the query is paginated. Everything is working successfully on a custom page template page-articles.php, however I'm unable to get the same results on the archive.php template.
The pagination link renders successfully (e.g mydomain.com/category/my-cat/page/2) however upon clicking the link does not work, it just throws a 404 error? How can these links not go anywhere?
I'm assuming there is some issue with using custom wp_query on the archive.php template?
Thanks!
Query
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC'
);
$articles = new WP_Query($args );
?>
Loop
<?php if ( $articles->have_posts() ) : ?>
<?php while ( $articles->have_posts() ) : $articles->the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Pagination
<nav>
<div class="prev"><?php echo get_previous_posts_link( 'Previous', $articles->max_num_pages ); ?></div>
<div class="next"><?php echo get_next_posts_link( 'Next', $articles->max_num_pages ); ?></div>
</nav>
After a bit of digging around, and with help from this article, the below solution solves the issue. Just place this in your functions.php file and thats it. The below implementation works for archives of custom post types, as well as categories.
/**
* Wordpress has a known bug with the posts_per_page value and overriding it using
* query_posts. The result is that although the number of allowed posts_per_page
* is abided by on the first page, subsequent pages give a 404 error and act as if
* there are no more custom post type posts to show and thus gives a 404 error.
*
* This fix is a nicer alternative to setting the blog pages show at most value in the
* WP Admin reading options screen to a low value like 1.
*
*/
function custom_posts_per_page( $query ) {
if ( $query->is_archive('cpt_name') || $query->is_category() ) {
set_query_var('posts_per_page', 1);
}
}
add_action( 'pre_get_posts', 'custom_posts_per_page' );
Yes, it is.
It is because main query of archive.php is kept unchanged while you are playing with WP_QUERY. Try use query_posts() in archive.php.
query_posts($args);
and then default loop (instead of $articles)
<?php if ( have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
Posts here!
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Filter the posts by a specific author

I want to display only one author's post say "abc_author" .
i have use the validation using if clause but THE PROBLEM is that i get pagination below the post where page 1 is blank (as an other author post is skipped) , page 2 (as an other author post is skipped) is blank and soon i get my desired post on page 6 . commenting the verbosa_pagination() hide the pagination navigation but the starting pages are blank and
I am a new to WordPress , i have also tries different plugin but they facilitate me to filter it on pages not post (though i can change the home page to a particular page but i want it on default blog post for few reasons )
<?php if ( have_posts() ) : ?>
<div id="content-masonry">
<?php /* Start the Loop */
while ( have_posts() ) : the_post();
if(get_the_author()=='abc_author')
{
get_template_part( 'content/content', get_post_format() );
}
endwhile; ?>
</div> <!-- content-masonry -->
<?php verbosa_pagination();
else :
get_template_part( 'content/content', 'notfound' );
endif;
Can you please try this code before the while loop?
<?php query_posts( array( 'author' => 'your author ID' ) ); ?>
Ref: https://developer.wordpress.org/reference/functions/query_posts/#usage

Remove Title Link for Specific Category in Wordpress

I'm running Wordpress 4.1. I have two blog pages on my site, and though I don't really know php, I've done some tinkering and figured out how to modify the page templates so each page only displays posts for a specific category. That bit of code looks like this:
<?php query_posts('cat=2'); ?>
That works fine. Page A displays posts from category 1, and Page B displays posts from category 2.
What I'd like to do is disable post title links for one specific category. In other words, Page A would display posts from category 1 (with standard clickable title links), and while Page B would display posts from category 2 (with non-clickable title links).
I'm an HTML/CSS guy, so really out of my depth here, but if there's a way to modify the loop to achieve this, I'd love to learn how. Thanks in advance for any help.
Yes, you can do this using the category.php theme file. When this page is hit, it loads a specific category requested and the posts that fall into that category.
Your theme and loop may look something like this:
<?php single_cat_title(); ?>
<?php echo category_description(); ?>
if (have_posts()) : while (have_posts()) : the_post();
/// display posts from specific category
endwhile; endif;
Or if you don't want to use that page which is designed for that, you can create your own loop:
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => 50 ) );
All together like this:
<?php
/* retrieve unlimited # of posts with an category slug of music */
query_posts( array ( 'category_name' => 'music', 'posts_per_page' => -1 ) );
// set $more to 0 in order to only get the first part of the post
global $more;
$more = 0;
// the Loop
while (have_posts()) : the_post();
the_content( 'Read the full post »' );
endwhile;
?>

Display all posts ordered by date

By default WordPress homepage shows latest posts. How can I display the latest posts on a page that is not the homepage ?
My first goal "GOAL A" is for the homepage to display a specific category called "popular posts" (instead of latest posts).
"GOAL B" is to have a link on the menu to ALL posts ordered by date, aka "the latest posts".
I have accomplished GOAL A with code below. How can I accomplish "GOAL B" ? I can make a category called "New" and make that a link on the menu, but how can I make it display all posts ordered by date ? Or is there a better method ?
.
"GOAL A" CODE: display specific category on homepage
function popular_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'popular' );
}
}
add_action( 'pre_get_posts', 'popular_category' );
I think the best here will be is to create a static front page with a blog page. This seems to be fit for what you are trying to do
Here is how:
STEP 1
You should delete the code in your question. This will not be necessary here
STEP 2
Make a copy of your page.php (or index.php) and rename it front-page.php. Open it up, and replace the loop with a custom query which will only display posts from the desired category. Unfortunately, pre_get_posts does not work on a static front page, so here you will have to make use of a custom query.
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
// the query
$the_query = new WP_Query( 'category_name=popular&posts_per_page=10&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Just remember, for paging on a static front page, you have to use page, not paged as you would for all other custom queries.
STEP 3
Make a copy of your index.php and rename it home.php. This will be your blog page template
STEP 4
You can now set your static front page and blog page in the back. You should have a read here about setting up a static front page and a blog page

Wordpress show posts code isn't showing any posts

I am using this code to show 5 posts (any post's would do)
<div id="featured-post-section">
<div id="post-list">1
<?php query_posts( 'posts_per_page=5' ); ?>2
As you can see, I've added a 1 and 2 numbers.... One before and one after ... I only get 1 2 ... No posts are showing at all.
What am I doing wrong?
That function only tells Wordpress how many posts to output. You still have to use the WP loop to actually perform the output.
<?php
// The Query
query_posts( 'posts_per_page=5' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();

Categories