WP - How to call posts by page parent category - php

I'm setting up a blog listings template for a site with multiple parent pages, and I need this page template to be used across the site, various pages will require there own blog listings page. Calling relevant posts depending on the Parent category.
i.e:
Food (Site Homepage)
Food Blog (Listings page for all Food related posts)
Fruit (Parent page)
Fruit Blog (Listings page for all Fruit related posts)
Veg (Parent page)
Veg Blog (Listings page for all Veg related posts)
My problem is that the posts per correct parent category aren't being called. I'm getting all posts.
I've set my code up like this: Many thanks in advance.
<div id="bloglistings">
<?php wp_reset_query(); ?>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$current_cat = intval( get_query_var('cat') );
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
$wp_query = new WP_Query( $args );
if ( $wp_query->have_posts() ) : ?>
<div class="row">
<?php $count=0; ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div class="span6">
<div class="media feature one">
<a class="pull-left" href="<?php the_permalink(); ?>">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail', array('class' => 'img-circle')); ?>
<img class="hoverimage" src="<?php echo get_template_directory_uri(); ?>/img/icon-read-bloglistings.png" alt="">
</a>
<div class="media-body">
<h2><?php the_title(); ?></h2>
<p class="date"><?php the_time('F j, Y'); ?></p>
<p><?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
</p>
</div>
</div>
</div>
<?php $count++; ?>
<?php if ($count==2 ||$wp_query->found_posts==0) :
echo '</div><div class="row">';
?>
<?php $count=0; ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<h2>Sorry but there are no posts.</h3>
<?php endif; ?>
<!-- PAGINATION -->
<div class="pagination">
<ul>
<li>
<?php
$big = 999999999; // need an unlikely integer
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,
) );
?>
</li>
</ul>
</div>
<?php wp_reset_query(); ?>
</div><!-- /.row -->
</div><!--/bloglistings-->

Try adding the category to the query.
Replace this:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);
with
$args = array(
'post_type' => 'post',
'category__in' => array( $current_cat ),// category was missing here
'paged' => $paged,
'posts_per_page' => 6,
'order' => 'DESC',
'orderby' => 'ID',
);

Related

WP my pagination function don't show in my custom Template

My pagination function shows in my index page , but when call function in a custom Template page (page-news.php) it don't show !!
functions.php
function numbering_pagination() {
global $wp_query;
$all_pages = $wp_query->max_num_pages;
$current_page = max(1,get_query_var('paged'));
if ($all_pages >1) {
return paginate_links(array(
'base' => get_pagenum_link() . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'mid_size' => 3,
'end_size' => 3,
'prev_text' => 'السابق',
'next_text' =>'التالي'
));
}
}
page-news.php
<?php /* Template Name: news */
get_header(); ?>
<div id="fh5co-blog-section" class="fh5co-section-gray">
<div class="container">
<div>
<div class="text-center heading-section animate-box">
<h3>news</h3>
</div>
</div>
</div>
<div class="container">
<div class="row row-bottom-padded-md">
<?php
$args = array(
'post_type' => 'post',
'category_name'=> 'news'
);
$posts = new WP_Query( $args );
while( $posts->have_posts() ):
$posts->the_post();
?>
<div class="col-lg-4 col-md-4 col-sm-6">
<div class="fh5co-blog animate-box">
<img src="<?php the_post_thumbnail_url('full')?>" alt="" />
<div class="blog-text">
<div class="prod-title">
<h3><?php the_title()?></h3>
<span class="posted_by"><?php the_time('F jS, Y'); ?></span>
<p><?php the_content('<span class="read-more"> ... more</span>'); ?></p>
</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>
</div>
<!-- ------ pagination ------ -->
<div class="pagination-numbers text-center">
<?php echo numbering_pagination() ?>
</div>
<!-- ------ END News ------ -->
How can I solve this?
From the WordPress codex:
<?php
//Protect against arbitrary paged values
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
'posts_per_page' => 5,
'category_name' => 'gallery',
'paged' => $paged,
);
$the_query = new WP_Query( $args );
?>
<!-- the loop etc.. -->
AND:
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
Mind the:
'total' => $the_query->max_num_pages
Also see: https://codex.wordpress.org/Function_Reference/paginate_links

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

Tag template instead of 'post' in the following loop

I'm using the code (loop) below for the blog post grid displaying. I want the following structure for my tag.php as well (the post by the tag instead of all blog posts). But I don't know how to do it. Could you, please, check it?
I can't remember how I solved the following problem in the past but I'm at a 100% sure that I found the solution previously.
<?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;
}
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="item col-sm-4">
<div class="well">
<h2><?php the_title(); ?></h2>
<span class="date"><?php echo get_the_date("j.n.Y"); ?></span>
<a href="<?php the_permalink(); ?>"><?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<?php the_excerpt(); ?>
<div class="readmore-wrapper">
<a class="readmore" href="<?php the_permalink(); ?>">Suite</a>
</div>
</div></div>
<?php endwhile; ?>
</div>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="next-posts-link">
<?php echo get_previous_posts_link( '< Page précédente' ); ?>
</div>
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Page suivante >', $custom_query->max_num_pages ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Please check WP_Query with Tag Parameter
https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
example:
$custom_query_args = array(
'taxonomy_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date', // modified | title | name | ID | rand
'tag' => 'tag1,tag2'
);

Wordpress pagination links reload page

I am trying to build a paginated archive sidebar using WP_Query. As the same sidebar is used on both single.php and news.php, the arguments used for the WP_Query are different.
On news.php, the pagination works perfectly, but on single.php the pagination links are present, and have the correct href values, but simply reload the page when clicked, i.e. anything beyond page 1 is inaccessible.
Update: Just in case this is relevant, single.php makes use of the Share Buttons by AddToAny, and Social (by MailChimp) plugins. I have deactivated both, but this has not fixed the issue with pagination.
Can anyone shed some light on this for me? Code here:
<aside>
<header>
<h1 class="column-title">Archive</h1>
<hr>
</header>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// remove first four posts if this is the news main page
if ( ! is_single() ) {
// Save first four posts
$first_four = new WP_Query ('post_type=post&orderby=date&order=desc&posts_per_page=4');
if ( $first_four->have_posts() ) : while ( $first_four->have_posts() ) : $first_four->the_post();
$skipIDs[] = $post->ID;
endwhile; endif;
wp_reset_postdata();
// Save all posts
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1
);
$all_posts = new WP_Query($args);
while ( $all_posts->have_posts() ) : $all_posts->the_post();
// Skip first four posts and save rest
if ( in_array($post->ID,$skipIDs) ) { continue; };
$offset_array[] = $post->ID;
endwhile;
wp_reset_postdata();
// Final arguments for WP_Query
$args = array(
'post__in' => $offset_array,
'paged' => $paged,
'posts_per_page' => 5
);
} else {
// Args for single.php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
}
$article_archive = new WP_Query($args);
$max_pages = $article_archive->max_num_pages;
if( $article_archive->have_posts() ) : while( $article_archive->have_posts() ) : $article_archive->the_post();
$has_image = get_the_post_thumbnail($post->ID,'thumbnail'); ?>
<article class="group">
<a class="anchor-overlay" href="<?php the_permalink(); ?>"></a>
<?php if( $has_image ) { echo $has_image; } else { echo "<img src='/wp-content/themes/DCSFC/images/news-calendar/news/no-image.jpg' alt='No image' />"; } ?>
<div>
<h3><?php the_title(); ?></h3>
<time datetime="dd/MM/YYYY"><?php echo get_the_date(); ?></time>
</div>
</article>
<?php endwhile; endif;
wp_reset_postdata();
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_pages
) );
?>
</div>

pagination elements not displaying in WordPress loop

I have the following code:
$attr = array(
'align' => 'left',
'class' => 'thumbnail imageRight',
'width' => 350,
'height' => 350
);
$post_query = array ( 'post_type' => 'post' );
$posts = new WP_Query ( $post_query );
if($posts->have_posts()){
while($posts->have_posts()){
$posts->the_post();
?>
<div class="post">
<?php the_post_thumbnail('medium', $attr); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
</div>
<?php
}
next_posts_link('« Older Entries');
previous_posts_link('Newer Entries »');
}
Which can be seen in action here. This page displays currently 3 of the 21 posts in the database. How ever there is no pagination.
Can some one tell me why?
Both next_posts_link and previous_posts_link use the global $wp_query and $paged. You have to chase function calls around the source code to see that (but it is pretty obvious with next_post_links). They don't work with custom queries but I believe you can cheat.
$old_wpq = $wp_query;
$wp_query = new WP_Query ( $post_query );
// your loop
$wp_query = $old_wpq;
Try that.
There is a related thread wordpress.stackexchange.com.
https://wordpress.stackexchange.com/questions/77661/next-posts-link-works-only-with-original-wp-query/77666#77666
<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post', //Post type
'posts_per_page' => 3, //How many post u want to display per page
'paged' => $paged
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<img src="<?=$url?>" width="350" height="350" class="thumbnail imageRight"/>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
<?php } } ?>
<div class="pagination">
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
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
) );
?>
</div>

Categories