Total noob here. I checked all over the net but I'm finding it difficult to add the pagination feature to my homepage. I'm using the recent_products per_page
query on my homepage but I want to limit to 12 products so that I can add the infinity scroll option.
<?php get_header(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => -1
);
$wp_query = new WP_Query($args);
if (isset($_GET['all']))
{
?>
<?php do_action('woocommerce_archive_description'); ?>
<?php if (have_posts()) : ?>
<?php
// I don't want the sorting anymore
//do_action('woocommerce_before_shop_loop');
?>
<ul class = "products-list">
<?php while (have_posts()) : the_post(); ?>
<?php woocommerce_get_template_part('content', 'product'); ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php
/* woocommerce pagination */
do_action('woocommerce_after_shop_loop');
?>
<?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>
<?php woocommerce_get_template('loop/no-products-found.php'); ?>
<?php endif; ?>
<?php else: ?>
{
// Code to display the product categories with thumbnails.
}
?>
<?php get_footer(); ?>
Would be great if someone can help me out with this. I need this template or a custom template you suggest to show the same pagination as the shop page (1,2,3) etc..
Thanks.
Related
PRECONDITION:
I created custom post type "comparison"
I created 'widget.php' and registered it in 'functions.php'
I added widget to 'front-page.php'
TASK:
On my Front Page and I want to list all posts with type "comparison" in separate widget with pagination
PROBLEM:
I don't know the 'best practice', how to implement it technically. How to list all "comparison" in widget?
Here is the custom post template code, You can assign the template on the page you want to display the posts list. You can try with below template code:
<?php
# Template Name: Comparison List
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 15,
'paged' => $paged,
'post_type' => 'comparison',
'orderby' => 'title',
'order' => 'asc'
);
$wp_query = new WP_Query($args);
?>
<?php get_header(); ?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'template-parts/content', 'comparison-list-item' ); ?>
<?php endwhile; ?>
<?php print_pagination($wp_query); ?>
<?php else: ?>
<?php get_template_part( 'template-parts/content', 'nothing-found' ); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>
I want to create custom page to show list title post by tag.
Example:
This title post by tag "handphone"
Title post
Title post
Title post
...etc
Any have code for this problem?
Create a template for your page
write the postcode below into the template
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>5, 'tag' => $brand_name);
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
?>
after that assigne the template from admin end to your page and lets see it's wwork or not .
Create a php file in your theme directory. You can give any name. And use code something like this. Replate tag_name by your desire tag. And then create a page. Set the template. You will see the list.
<?php
/**
* Template Name: Title by Tag
*
*/
get_header();
$args=array('posts_per_page'=>5, 'tag' => 'tag_name');
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
?>
<li>
<?php the_title(); ?>
</li>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Create page and add below code to your page
<?php
/**
* Template Name: Get title by tag
*
*/
get_header();
$tagname = 'handphone';
$wp_query = new WP_Query(array(
'post_status' => 'publish',
'post_type' => 'your-posttype', // or 'any'
'tag_slug__in' => $tagname,
'posts_per_page' => -1
));
if ( have_posts() ) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
wp_reset_postdata();
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I've the following code (simplified as I have some HTML between the PHP tags):
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php $args = array( 'posts_per_page' => 1, 'paged' => $paged, ); ?>
<?php query_posts($args); ?>
<?php if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content() ?>
<?php endwhile; ?>
<?php endif; ?>
<?php the_posts_pagination(); ?>
The problem is, when I click on the next page then it shows my 404 page.
This code is placed in the home.php and it is my news page.
How to fix this issue?
I've tried a lot queries on the internet, and already changed the permalink structure to default.
<?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!
Thanks for taking the time to check this out.
I've spend a couple days going over this. Lots of time on wp.org but i'm just not getting it. Should be an easy fix I'm sure. No matter what i try, i can't limit the number of posts per page nor get any pagination to display. This is my most recent attempt (maybe not my best attempt). The page just shows all the posts OR all the recent ones or something (not my site). Once i can at least get the page to limit the posts, then i'll tackle the pagination. Also, setting the posts per page in the WP dashboard does nothing...and never did. That's why i'm trying to code something myself. Why can't i limit the number of posts per page? Would i put the pagination where i currently have it? Is this a total mess, lol?
Thanks again,
Dave (code below)
<?php /* Template Name: Stories */ ?>
<?php
get_header(); ?>
<!-- *************************************** -->
<div class="custom_two_third">
<?php
// The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
}
// pagination
next_posts_link();
previous_posts_link();
} else {
get_template_part( 'template-parts/content', 'none' );
}
/* Restore original Post Data */
wp_reset_postdata();
?>
<div class="clear"></div>
</div><!-- custom_two_third -->
<?php
get_sidebar();
get_footer();
?>
Try using post_type
$args_articles =array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'order' => 'desc',
'paged' => $paged
);
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_eve_args = array( 'post_type' => 'news_events', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $news_eve_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 news or events at this time.', 'theme' ); ?></p>
<?php endif; ?>
Try this one, it will work in your case.
global $query_string;
query_posts("{$query_string}&posts_per_page=12");
while (have_posts()) {
the_post();
...
}