I use the following code to query all the posts
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'san-pham', 'posts_per_page' => 8,
'paged' => $paged,
'order' => 'DESC',
)
);
?>
<?php $i=1; while(have_posts()) : the_post(); ?>
//
<?php $i++; endwhile ?>
<?php if (function_exists('wp_pagenavi')) {
wp_pagenavi(); } ?>
Now I want to query all the post from a specific category. How do I do this?
You can add the category_name in the query_posts array
For example
query_posts('showposts=3&category_name=Blog&orderby=ID&paged='.$paged);
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'san-pham',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 8,
'order' => 'DESC',
);
$query = new WP_Query($args);
if ($query->have_posts()):
while ( $query->have_posts() ):
$query->the_post();
?>
//content
<?php endwhile; ?>
<?php if (function_exists('wp_pagenavi')): ?>
<?php wp_pagenavi( array( 'query' => $query ) ); ?>
<?php endif; ?>
Related
Can you tell me what is the problem with my code? my code is to display the custom post by category. my code is perfectly working fine. but when I put a pagination, the code is error now.
<?php
$ourteam_category_check = '4';
$paged = (int) get_query_var('paged'); --> I inserted this
$niche_ourteam_args = array(
'posts_per_page' => 10,
'paged' => $paged, --> I inserted this
'post_type' => 'shop',
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
),
),
'tax_query' => array(
array(
'taxonomy' => 'shop_cat',
'field' => 'term_id',
'terms' => $ourteam_category_check
),
),
);
$niche_ourteam = new WP_Query($niche_ourteam_args);
while ($niche_ourteam->have_posts()) : $niche_ourteam->the_post();
?>
--> echo all the items from category 4 here
<?php endwhile; endif; ?>
<div class="page-nav-area">
<?php
if( function_exists('wp_pagenavi') ) {
wp_pagenavi(array('query' => $the_query));
}
?>
</div>
Try this code is working fine for my side
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="the_loop">
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'shop',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $cat->cat_ID, // your categories or you can use 'category__in' => array(2,6)
),
),
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop code goes here.
<?php endwhile; ?>
<?php if ($loop->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $loop;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
I am using the following code for the homepage pagination. The problem is that the posts are not changing whenever I try to switch between the pages but I could see the pagination changes in the permalink. Can someone please help me to solve this issue?
<?php
$ourCurrentPage = get_query_var('page') ? get_query_var('page') : 1;
$args = array(
'post_type'=> 'post',
'order' => 'DESC',
'posts_per_page' => 4,
'page' => $ourCurrentPage,
);
$my_query = new WP_Query( $args );
if($my_query->have_posts()) : while ($my_query->have_posts() ) : $my_query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-content" data-aos="zoom-in" data-aos-delay="200">
-------------
-------------
</div>
</article><!-- #post-## -->
<?php endwhile;
echo paginate_links(array(
'total' => $my_query->max_num_pages
));
?>
It is 'paged' not 'page'
$args = array(
'post_type'=> 'post',
'order' => 'DESC',
'posts_per_page' => 4,
'paged' => $ourCurrentPage,
);
I am currently putting this array into my table of contents. To create something like this...
<?php
$args = array(
'post_category' => 'live',
'post_status' => 'publish',
‘order’ => ‘ASC’,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_field('venue');
the_excerpt();
endwhile;
wp_reset_postdata();
?>
When I add a new custom post the information is displaying on the same line.
I want each post to start on a new line.
Use tag div for record in 1 one.
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
echo "<div class='item item-".$count."'>";
echo the_field( 'venue' );
the_excerpt();
echo "</div>";
$count++;
endwhile;
wp_reset_postdata();
?>
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo the_field( 'venue' );
the_excerpt();
echo PHP_EOL;
endwhile;
wp_reset_postdata();
?>
In case of HTML
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
?>
<div class="list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="list-item">
<?php echo the_field( 'venue' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
I have code like this for fetching and displaying products in woocommerce:
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 30
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
}
The problem is while the code displays 30 products at a time as specified, it does not add page links so other products could be viewed, is there a way to do this or am I missing somethin?
<ul class="products">
<?php
global $paged;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'paged' => $paged
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
?>
<nav>
<ul>
<li><?php previous_posts_link( '« PREV', $loop->max_num_pages) ?></li>
<li><?php next_posts_link( 'NEXT »', $loop->max_num_pages) ?></li>
</ul>
</nav>
<?php wp_reset_postdata(); ?>
</ul><!--/.products-->
Reference : Pagination in WooCommerce
Can you please check below code? i hope this code is work for you.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'product',
'posts_per_page' => 30,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//display results here;
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
endif;
wp_reset_postdata();
I have a while loop that gets all pages of a certain category on index.php which is my posts page.
However, I want to change the code below, so if it is the homepage, it does not run this loop but displays some text.
This code the loop works:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
?>
In this code, nothing loads on any page except the homepage:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC' ,
'paged'=>$paged
);
if ( is_home() ) {
echo 'Welcome!';
} else if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
You need to wrap your query and loop in your is_home() condition
if ( !is_home() ) { // This is not the home page
// Add your query and loop here
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
} else { // This is the homepage
echo 'Some text here';
}
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $the_query->the_post();
if ( is_home() ) {
echo 'Welcome!';
}
else{?>
<h2><a href="<?php echo get_permalink(); ?>"><?php the_title();
}
<?php endwhile; endif; ?>