Query posts inside custom post type
-
Hello friends,
this is my first post here and im sorry that i have to start it with a question (i’m very new to wordpress).
I’ve managed to create a custom post type “Product” with many custom fields. I also have written my own template and made a page where i can list all my products. When a user clicks on a product, he gets to the single product page where all custom fields are shown. That’s working great! Now on the same site (single “product”), i want to show the user all articles which i’ve tagged with the products name.
I’ve also managed to do that but here comes the issue:
How to enable paging on the single product?
When i add “/page/2/” at the end of the URL of my single “product” (so it looks like localhost/product/testproduct/page/2/) it jumps back immediately to the previous url (without the paging).Is there a way to do this? Any info would be helpful!
Thank you very much!
-ScienArtic
-
have you set ‘paged’ => $paged at query_posts arguments?
hi you can use this code
please put this code instead of this code<?php if ( have_posts() ) : ?> <!-- Add the pagination functions here. --> <!-- Start of the main loop. --> <?php while ( have_posts() ) : the_post(); ?> <!-- the rest of your theme's main loop --> <?php endwhile; ?> <!-- End of the main loop --> <!-- Add the pagination functions here. --> <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div> <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div> <?php else : ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>Replace with
<div class="pagin"><span class="paginat"> <?php $big = 999999999; echo paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages, 'show_all' => False, 'end_size' => 1, 'mid_size' => 5, 'prev_next' => True, 'prev_text' => __('PREV'), 'next_text' => __('NEXT'), 'type' => 'plain', 'add_args' => False, 'add_fragment' => '' ) ); ?> </span></div>For single post pagination (with the <!–nextpage–> quicktag) urls look similar to this (without “/page”):
/ocalhost/product/testproduct/2/https://codex.ww.wp.xz.cn/Function_Reference/wp_link_pages
Or use previous_post_link() and next_post_link() to navigate to the next and previous post.
https://codex.ww.wp.xz.cn/Function_Reference/previous_post_link
https://codex.ww.wp.xz.cn/Function_Reference/next_post_linkHello!
Thank you very much for all your answers. Unfortunately i was not yet able to solve the issue.@lidya1859: yes, i use the ‘paged’ => $paged in my WP_Query query.
@piyush: Thank you for your code. When i replace the required parts, i see a nice counter from 1 to 5 pages with a “next” link, but when i click on any links, nothing happens. The page just reloads. I guess this is because the links are adding the /page/[pagenumber] to the url which is not working
@keesiemeijer: When i add the page number to the ULR without /page/, then it is leaving the URL with the page number but this does not effect the WP_Query.
Edit: I was able to create a pagination! I’ve modified Piyush code so it looks like this:
echo paginate_links( array( 'base' => $permalink.'/%#%', 'format' => '?page=%#%', 'current' => max( 1, get_query_var('page') ), 'total' => $wp_query2->max_num_pages, 'show_all' => False, 'end_size' => 1, 'mid_size' => 5, 'prev_next' => True, 'prev_text' => __('PREV'), 'next_text' => __('NEXT'), 'type' => 'plain', 'add_args' => FALSE, 'add_fragment' => '' ) );(The $permalink variable is the permalink of the single product)
And in my WP_Query i use ‘paged’ => get_query_var(‘page’)Thank you very very much for your support!
The topic ‘Query posts inside custom post type’ is closed to new replies.