I have two custom post types from the archive.php One is archive-slug.php and the other is category-slug.php. The pagination works on the archive-slug.php but the same code on the category-slug.php won't even show. I'm somewhat new to Wordpress and php so i'm sure i'm missing something here, I just don't know what?
<?php
// Custom Post Type
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'available_dogs',
'category_name' => 'adopted-dogs',
'posts_per_page'=> 9,
'paged'=> $paged
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="dog-info-box col-lg-4 col-sm-6 col-xs-12">...</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
<?php
// Get the pagination
fusion_pagination( $pages = '', $range = 2 );
?>
<?php if( $sidebar_exists == true ): ?>
<?php wp_reset_query(); ?>
As I want to inform you that when you create a file "category-slug.php : it's an default template.No need to add the query posts.
It will display the posts of the category whose slug is added in file like category-slug.php.Here is the modifying code.
if (have_posts() ) :while ( have_posts() ) : the_post();
the_content();//content is going on.
endwhile;//pagination functionfusion_pagination( $pages = '', $range = 2 );else : _e( 'Sorry, no posts matched your criteria.' ); endif;
Precaution :1:Pagination code should be placed just before of the while loop.2:In your code pagination code have been place after the endif that's why it is not working.
Thanking you.
I used this code and got it working. It's not pretty but it will do for now. For what ever reason when I try to use it after the loop it doesn't work. When I use it after the endif it works.
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
</div>
</nav>
<?php } ?>
Please follow this steps.
1: Please make sure, your category have the posts.
2: If your category slug is "test" then your file name should be " category-test.php . if any confusion then please let me know.
3: See the code and write in file.
if(have_posts()) :while(have_posts()): the_post();the_title();the_content();endwhile;//pagination of wp
previous_posts_link( '« Newer Entries' );
next_posts_link( 'Older Entries »', 0 );endif;
Note:1:Please place this code in file and run if it will running successfully, then we will add the designing part.
2: If you can share the website URL then I can also give suitable solution because then I can seen the category slug etc .
Please let me know if still needs problem.
Thanking.
I finally fixed a variation of this same issue.
This issue was occurring with category archive pagination.
Initially thought it was within the Loop - turns out the theme I was working on had these filters in a file, custom.php, which manually rewrote the permalink structure.
add_filter('category_link', 'themename_change_archive_link', 100, 2);
add_action('init', 'themename_archive_rewrite', 50);
Pagination didn't like that due to what looks like a conflict with a recent WooCommerce update, so I removed these filters, used the Custom Permalink plugin to rewrite the category permalinks, and it worked! Now I have a custom permalink structure AND functioning category pagination.
Related
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; ?>
Been researching a pagination issue I am having with WordPress. My page-foobar.php file is:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$foo_query = new WP_Query( $foobar_args );
if ( $foo_query->have_posts() ) : ?>
<?php while ( $foo_query->have_posts() ) : $foo_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
I added:
<nav>
<?php previous_posts_link('« Newer',$foo_query->max_num_pages); ?>
<?php next_posts_link('Older »',$foo_query->max_num_pages); ?>
</nav>
after referencing this answer after the endif;:
The link shows in the url as site/foobar/page/2/ but displays the 404 page. After some further research I ran across an article that mentioned the page-foobar.php and posttype-foobar.php names can clash so to rename one of them. I renamed posttype-foobar2.php, changed my functions.php, tested and still get sent to the 404 page with the correct URL. Another article mentioned that you must use wp_query() so I changed my page-foobar.php to:
<?php
// the query
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'ignore_sticky_posts' => true, );
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
// code
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
pagination:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
I did test this by adding this before <?php wp_reset_postdata(); ?> and after <?php endwhile; ?>:
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
tested and I still get sent to a 404 page. When I change next_posts_link() to <?php var_dump(next_posts_link('« Older Entries', $wp_query->max_num_pages)); ?> it returns NULL. If I change next_posts_link() to get_next_posts_link() I get string(99). What am I doing wrong with my pagination?
Further Referenced:
Wordpress pagination (next_posts_link) on custom wp_query not showing
next_posts_link 404 error
WP_Query and next_posts_link
How can I properly get my pagination to go to the next page?
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $foobar_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no foobar posts at this time.', 'theme' ); ?></p>
<?php endif; ?>
After doing some further research I ran across the solution to my problem and I hope it can help someone else. The answer submitted by Sindhu does have merit in respect to the addition of:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
and the modification to the arguments:
$foobar_args = array( 'post_type' => 'foobar', 'posts_per_page' => 4, 'paged' => $paged);
Codex for get_query_var(). After making this additional change it still didn't work so I started testing my Permalinks. When I ran the code under Default the pagination worked. When I changed to Post name and saved twice it still didn't work. After browsing I decided to search for custom post type giving 404 error and I ran across "Fixing Custom Post Type 404 Errors In WordPress" which mentioned I could be creating a double slug. After review I was in fact doing so and WordPress was clashing with the double slug. I decided to changed my posttype-foobar.php to posttype-foobar_t.php and updated my functions.php file. Instead of modifying the database on my localhost to fix the slug issue I decided to install a clean installation with the modified changes and it works.
I would also like to point out some of my findings in regards to a large majority of posts mentioned you have to call the query as $wp_query = new WP_Query( $foobar_args );.
This is my second question, and I promise as soon as I feel like I can help others, I'm gonna return the favours to the community!
I'm building a one page Wordpress site with different sections, working from _S theme and learning as I go.
I already have all my different pages being pulled into the front-page, but I would like the different sections to have different layouts and elements. To be specific - in one section I would like content to be pulled in along side an iFrame that float next to each other. To do this I know that I need to call a custom page template that specifies two floating div containers (I'm building using Bootstrap).
In the front-page.php, I've written this:
<?php
if ( get_option( 'show_on_front' ) == 'posts' ) {
get_template_part( 'index' );
} elseif ( 'page' == get_option( 'show_on_front' ) ) { ?>
<?php get_header(); ?>
<section class="home">
<div class="entry-content">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$the_query = new WP_Query($args);
?>
<?php
while($the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if (is_page(9)) : ?>
<?php get_template_part('play','page');?>
<?php else : ?>
<?php get_template_part('content', 'page');?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</section>
<?php
get_footer();
}
?>
Which works perfectly in bringing in my different page sections, but I can't see how to call a different for the page (called 'play' or with the ID of 9).
I have already created a template called play-page
Any ideas?
Thanks masses
Harry
Let's assume you want to display 3 sections (about, work, contact) on the custom front page, you can create per page template for each section, like:
section-about.php
section-work.php
section-contact.php
In page-about.php like this:
<div class="section-about">
<?php the_content(); ?>
</div>
And use section-work, section-contact for other 2 pages.
Then you create 3 pages in the Dashboard and assign each to its template.
Now, you can add the loop in your font-page.php
<?php
$args = array(
'post_type' => 'page',
'post__in' => array(1, 2, 3) // the ids of those pages
);
$the_query = new WP_Query($args);
?>
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<?php
the_title();
the_content();
// etc
?>
<?php endwhile; wp_reset_postdata(); ?>
hello there all i have a proplem that we are using one of the new themes called fancy theme the theme comes with a proplem in pagination now we are trying to fix that problem
as you see here http://www.uniblues.com/ when you press page 1,2,3 it redirects you to the same page no change only the url changes too http://www.uniblues.com/page/3/or /4 , /5 according to the page number you press here is the code that the theme uses ..
<?php
//query_posts('paged='.$paged);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=7');
?>
any ideas ?? .. thanks
in case if some body wants to now how i manged to did this i simply used this code and it's done ..
global $query_string;
parse_str( $query_string, $my_query_array );
$paged = ( isset( $my_query_array['paged'] ) && !empty( $my_query_array['paged'] ) ) ? $my_query_array['paged'] : 1;
query_posts('post_type=post&posts_per_page=7&paged='.$paged);
?>
and it works like charm .. thanks all
Is it a theme we can download or did you develop it ?
The code you show get the last 7 articles, so the way it reacts is normal ^^
Here is, for example, the code used in twenty twelve :
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
[...]
It simply use the have_post() function to get the articles, and use the template called content (content.php) to show them.
And the number of post to show is set in the administration panel > Settings > Reading.
If you are developing your own theme, you should take a look at how the base themes (like twenty twelve) work.
how about
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post();
// if you use the <!-- more --> in your posts.
global $more;
$more = 0;
?>
<div class="post">
etc...
</div>
<?php endwhile; ?>
<div class="navigation">
<?php next_posts_link(''); ?>
<?php previous_posts_link(''); ?>
</div>
<?php else: ?>
<div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>
:)
I've decided to start using wp_query instead of query_posts, on my home page I want to call the content from a particular page inside a div. Just wondering what am I missing here as nothing is appearing and I do have a page with content with an ID of 140. Im still quite new to this thanks!
<div class="home-info" >
<?php
if (have_posts() ) :
$the_query = new WP_Query( 'paged=140' );
while ($the_query->have_posts() ) : $the_query->the_post();
echo the_content();
endwhile;
wp_reset_postdata();
endif;
?>
</div>
You've used the wrong argument in WP_Query, it should be page_id rather than paged. Here's the reference from the Codex;
http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters