Display Posts Category Wise - wordpress

I have code to show the latest 3 posts of every category and I want to exclude the child categories, but it doesn't exclude the sub category and it shows sub category separately and it show the latest 3 posts of sub category. Is there any way to exclude sub categories from loop.
Here is the code:
<?php
//start page loop
if (have_posts()) : while (have_posts()) : the_post();
//get meta to set parent category
$library_filter_parent = '';
$library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; }
?>
<div id="library-by-category-wrap">
<?php
//get meta to set parent category
$library_filter_parent = '';
$library_parent = get_post_meta($post->ID, 'wts_library_parent', true);
if($library_parent != 'select_library_parent') { $library_filter_parent = $library_parent; } else { $library_filter_parent = NULL; };
//term loop
$terms = get_terms('library_cats','orderby=custom_sort&hide_empty=1&child_of='.$library_filter_parent.'');
foreach($terms as $term) { ?>
<div class="heading">
<h2><?php echo $term->name; ?></h2>
</div>
<div class="library-category">
<?php
//tax query
$tax_query = array(
array(
'taxonomy' => 'library_cats',
'terms' => $term->slug,
'field' => 'slug'
)
);
$term_post_args = array(
'post_type' => 'library',
'numberposts' => '3',
'tax_query' => $tax_query
);
$term_posts = get_posts($term_post_args);
//start loop
foreach ($term_posts as $post) : setup_postdata($post);
//get images
$featured_image = get_the_post_thumbnail($post->ID, 'cat-thumbnail'); ?>
<?php if(!empty($featured_image)) { ?>
<div class="library-item">
<a class="library-title" href="#" title="<?php the_title(); ?>" target="_blank">
<h3><?php the_title(); ?></h3>
</a>
</div>
<!-- /library-item -->
<?php } ?>
<?php endforeach; ?>
</div>
<!-- /library-category -->
<?php } wp_reset_postdata(); ?>
</div>
<!-- /library-by-category-wrap -->
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>

<?php
//get all terms (e.g. categories or post tags), then display all posts in each term
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Note see the Time Parameters in the query_posts() article.

<?php // get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts(“cat=$cat_id&post_per_page=100");
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<?php the_title(); ?>
<?php echo '<hr/>'; ?>
<?php endwhile; endif;
// done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>

<?php
$catquery = new WP_Query( array( 'post_type' => 'testimonials', 'category_name'=>'hello','posts_per_page' => 10 ) );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li>
<h3>
<?php the_title(); ?>
</h3>
</li>
<li>
<?php the_content(); ?>
</li>
</ul>
<?php endwhile; ?>

$taxonomy = 'category';
$term_args = array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0
);
$terms = get_terms($taxonomy, $term_args);

[homepage_cat_grid limit='3' cat_id='2']
add_shortcode( 'homepage_cat_grid', array($this, 'homepage_postgrid_shortcode') );
/**
* HOME PAGE CATEGORY WISE POST GRID
*/
public function homepage_postgrid_shortcode( $atts, $content ) {
extract( shortcode_atts( array (
'order' => '',
'orderby' => '',
'limit' => '',
'cat_id'=> ''
), $atts ) );
ob_start();
$args = array(
'post_type' => 'post',
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $limit,
'cat' => $cat_id
);
$home_post = new WP_Query( $args );
if( $home_post->have_posts() ) : ?>
<div class="post-header">
<div class="head-left">
<?Php
$terms = wp_get_post_terms($home_post->post->ID,'category');
$cat_nm=$terms[0]->name;
if($cat_nm=='News'): echo "Recent News"; else: echo $cat_nm; endif;
?>
</div>
<a href="<?php
$category_link = get_category_link( $terms[0]->term_id );
echo $category_link; ?>" class="btn post-head-btn">VIEW ALL</a>
</div>
<div class="row">
<?php while ( $home_post->have_posts() ) : $home_post->the_post(); ?>
<div class="col-md-4">
<div class="single-grid">
<a href="<?php echo get_permalink( $post->ID ); ?>">
<?php the_post_thumbnail( $post->ID ); ?>
</a>
<div class="post-caption">
<?php echo the_category(); ?>
<div class="post-heading">
<?php echo the_title(); ?>
</div>
<div class="post-content">
<?php the_excerpt(); ?>
</div>
<div class="row">
<div class="col-12">
<a class="btn btn-dark cust-btn" href="<?php echo get_permalink( $post->ID ); ?>">Read More</a>
</div>
</div>
</div>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php else :
get_template_part( 'template-parts/content', 'none' );
endif;
return ob_get_clean();
}

Related

Page Title Displaying Incorrectly

I have created a custom post type and taxonomy on my website but when I view the taxonomy page it is showing the incorrect page title. Rather than showing 'Taxonomy Term Name' as the Title it is showing the title of the first post shown on that page?
This is my taxonomy page template...
<div class="one-whole">
<!-- Course Tax -->
<?php
// Get the taxonomy
$taxonomy = get_queried_object();
$id = $taxonomy->term_id;
$name = $taxonomy->name;
$slug = $taxonomy->slug;
?>
<h1><?php echo $name; ?> Training Courses</h1>
<?php echo term_description( $id, $name ) ?>
<hr>
<section class="flexbox one-whole">
<?php // Create a custom loop for all items in this taxonomy
$args = array(
'post_type' => 'htl_training',
'posts_per_page' => -1,
'order' => 'asc',
'orderby' => 'name',
'tax_query' => array ( array(
'taxonomy' => 'course-categories',
'terms' => $taxonomy->slug, // Taxonomy Term to Search in
'field' => 'slug',
'operator' => 'IN')
)
);
$posts = new WP_Query($args);
if($posts->have_posts()) :
while($posts->have_posts()) :
$posts->the_post(); ?>
<!-- Card -->
<div class="card card-title-below">
<?php $image = get_field('accrediting_body'); ?>
<?php if( !empty($image) ){ ?>
<?php
// check if the repeater field has rows of data
if( have_rows('endorsing_bodies','options') ){
// loop through the rows of data
while ( have_rows('endorsing_bodies','options') ) {
the_row();
$hook = get_sub_field('logo_hook', 'options');
$icon = get_sub_field('logo','options');
$accrediting_body = get_field('accrediting_body');
if( $accrediting_body ){ ?>
<div class="training-provider">
<?php if( $accrediting_body === $hook ) {
echo '<img src="' . $icon['url'] . '" alt="' . $hook .'" />';
} ?>
</div>
<?php
}
}
} else { } ?>
<?php } ?>
<a href="<?php the_permalink(); ?>" class="non-animated-link">
<?php htl_the_post_thumbnail(); ?>
</a>
<h4><?php the_title(); ?></h4>
</div><!--/ Card -->
<?php
endwhile;
else:
echo' Oops, there are no posts';
endif;
?>
<?php wp_reset_postdata(); ?>
</section>
<!-- Course Tax -->
</div>
How can I show the taxonomy term name as the page title?
Use following function to display archive title
the_archive_title();
More info here

Related post by category, but only one

I'm using this code to display related post by category and it works fine if there is only one category. But in the case of multiple categories I only want to use one, the first one. How do I limit this to just one category?
function related_posts_categories() {
$categories = get_the_category();
if ($categories) {
foreach ($categories as $category) {
$cat = $category->cat_ID;
$args=array(
'cat' => $cat,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page'=>4,
'ignore_sticky_posts'=>1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h3>Related Content</h3><ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li>
<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
<?php the_post_thumbnail( 'related-posts' ); ?>
</a>
<div class="related_content">
<?php the_title(); ?>
</div>
</li>
<?php
endwhile;
}
echo '</ul></div>';
}} wp_reset_query(); }

Wordpress ACF plugin - how to order by date field

I am using Advanced custom Fields plugin to create an event date in a custom post. I would like to order my events by that date, but it doesn't works for me:
<div class="column one">
<?php
$args = array(
'post_type' => 'formation',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'date_formation',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$posts_query = new WP_Query( $args );
while ($posts_query->have_posts()) : $posts_query->the_post();
$presentation = get_field('date_formation');
$date_formation = mysql2date( 'j F Y ', $presentation);
$lieu = get_field('adresse_formation');
?>
<div class="column one-third eventpost">
<div class="event-col">
<?php echo '<h4>'.$date_formation.''.$lieu.'</h4>'; ?>
<?php if( !is_single() ) {?><?php the_excerpt(); ?> <?php }else{ ?>
<?php the_content(); ?>
<?php } ?>
<p>En savoir plus<i class="icon-right-open"></i></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
Try to use Parham's solution from this question. You'll need to modify it a bit like so:
<div class="column one">
<?php
function wpse_130954_orderby_fix($pieces){
global $wpdb;
$pieces['where'] .= " AND $wpdb->postmeta.meta_key = 'date_formation'";
$pieces['orderby'] = "$wpdb->postmeta.meta_value DESC";
return $pieces;
}
add_filter( 'posts_clauses', 'wpse_130954_orderby_fix', 20, 1 );
$args = array(
'post_type' => 'formation',
'posts_per_page'=> -1,
'post_status' => 'publish',
);
$posts_query = new WP_Query( $args );
while ($posts_query->have_posts()) : $posts_query->the_post();
$presentation = get_field('date_formation');
$date_formation = mysql2date( 'j F Y ', $presentation);
$lieu = get_field('adresse_formation');
?>
<div class="column one-third eventpost">
<div class="event-col">
<?php echo '<h4>'.$date_formation.''.$lieu.'</h4>'; ?>
<?php if( !is_single() ) {?><?php the_excerpt(); ?><?php }else{ ?><?php the_content(); ?><?php } ?>
<p>En savoir plus<i class="icon-right-open"></i></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
<?php remove_filter( 'posts_clauses', 'wpse_130954_orderby_fix', 20 ); ?>
</div>

Wordpress Category Template with Offset - Featured on every page

I'm trying to make a category template in which, first post is displayed with full text + big thumbnail (as featured) and then posts with titles only + a smaller thumbnail.
I've managed (with help) to do this with two queries, but first post remains the same on each page, won't change, as also, the loop won't adopt the offset of first page into other pages too.
Code is here:
<?php $cat_link = get_category_link( $cat_id );
$cat_name = get_cat_name($cat_id);
$cat_id = 22; ?>
<?php $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); ?>
<div class="catrecent">
<div class="recenttitle">
<h2 class="catidtxt"> <?php echo ($cat_name); ?></h2>
<h2 class="recentposttitle"><?php the_title(); ?></h3>
</div>
<?php if( has_post_thumbnail() ) { ?>
<div class="recentpostwrap">
<?php the_post_thumbnail( 'thumbcatbig' ); ?>
<?php the_content_limit(588,""); ?>
<div class="readrecent">
<?php _e("more", "mm"); ?>
</div>
</div>
<?php } else { ?>
<div class="holder no-thumb-big">
<?php the_content_limit(798,""); ?>
<div class="more">
<?php _e("more", "mm"); ?>
</div>
</div>
<?php } ?>
<?php endwhile; else: ?><?php endif; ?><?php wp_reset_query(); ?>
</div>
<ul class="catlist-recent">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$ppp = 7;
$offset = 1;
$cat_id = '22,27,29';
//Manually determine page query offset (offset + current page (minus one) x posts per page)
$page_offset = $offset + ( ($paged - 1) * $ppp );
$query = new wp_query(array(
'offset' => $page_offset,
'posts_per_page' => $ppp,
'cat' => $cat_id,
'paged' => $paged
));
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<li class="catlist-recentpost">
<?php the_post_thumbnail( 'thumbcatsmall' ); ?>
<?php the_title(); ?>
<div class="catlist-recentposttxt"> <?php the_content_limit(300, ''); ?></div>
</li>
<?php endwhile; else: ?><?php endif; ?>
</ul>
<div class="cat-pagenate">
<?php
// pagination
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'total' => ceil(($query->found_posts - $offset) / $ppp),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
));
wp_reset_query();
?>
</div>
Any help appreciated!
Try
$query = new wp_query(array(
'offset' => $page_offset,
'number' => $ppp,
'cat' => $cat_id,
'page' => $paged
));
Note the change from "paged" to "page"

Adding pagination to custom post loop in page

I have created a custom page template (testimonials-page.php) and in that template I am
loading custom post type 'testimonials' using the following loop:
<?php query_posts(array(
'posts_per_page' => 5,
'post_type' => 'testimonials',
'orderby' => 'post_date',
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
How do I add pagination to that? I installed the WP Paging plugin, and while that plugin works
great when I call the pagination into category.php using:
<p><?php wp_paging(); ?></p>
Inserting the same thing into testimonial-page.php results in broken formatting and links that
404 on me.
Firstly, never EVER use query_posts unless your intention is to modify the default Wordpress Loop.
Instead, switch to WP Query.
Here's something I wrote for a theme I did for a client using all built-in Wordpress functions. It's been working pretty well for me so far, so I'll integrate it into your code as best as I can:
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'post_type' => 'testimonials',
'orderby' => 'post_date',
'posts_per_page' => 5,
'paged' => $paged
);
$query = new WP_Query($args);
if($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div id="post-<?php the_ID(); ?>" class="quote">
<?php
echo get_the_post_thumbnail($post->ID, array($image_width,$image_height));
the_content();
?>
</div>
<?php
endwhile;
echo '
<div id="wp_pagination">
<a class="first page button" href="'.get_pagenum_link(1).'">«</a>
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$query->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '
<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">›</a>
<a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">»</a>
</div>
';
wp_reset_postdata();
endif;
?>
Jan 2018 Edit:
Also consider using paginate_links, since it's also built into Wordpress, and has more robust options and capabilities.
Try this code for custom loop with pagination:
<?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(
'post_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(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?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="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</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;
?>
Source:
WordPress custom loop with pagination

Resources