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(); }
Related
I am struggling with this code to display categories (with posts) only if posts in that category are greater than / equal to 3.
I am using WP_Query with args() to get the categories and display latest 3 posts from those categories.
Any help will be highly appreciated.
<?php
$categories = get_categories( $args );
foreach( $categories as $category ) {
$args = array(
'cat' => $category->term_id,
'posts_per_page' => 3,
'post_type' => 'post',
);
$terms = get_terms(array(
'taxonomy' => 'category'
) );
foreach($terms as $term) {
if($term->count >= 3) {
echo $term->name;
$the_query = new WP_Query( $args );
echo '<h3>' . $category->name . '</h3>'; // Display category name
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</h3>
<?php
endwhile;
}
}
}
wp_reset_postdata();
?>
The WP_Query will return posts for a specific category and not categories themself.
You need to grab all of the categories and loop through them and check the count.
https://developer.wordpress.org/reference/functions/get_terms/
It should be something like this: (untested!)
$terms = get_terms(array(
'taxonomy' => 'category'
) );
foreach($terms as $term) {
if($term->count >= 3) {
echo $term->name;
}
}
$categories = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false,
));
foreach ($categories as $category) {
$args = array(
'cat' => $category->term_id,
'posts_per_page' => -1,
'post_type' => 'post'
);
if ($category->count >= 3) {
echo $category->name;
$the_query = new WP_Query($args);
echo '<h3>' . $category->name . '</h3>'; // Display category name
while ($the_query->have_posts()) : $the_query->the_post();
?>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
<h3>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</h3>
<?php
endwhile;
}
}
wp_reset_postdata();
get_terms() array can take in a lot of arguments so please check the documentation first.
Let me know if you have any problems with it. I'll try to help :)
I'm trying to get all related posts on a single template. Posts should have at least one matching tag.
$post_id = $GLOBALS['wp_the_query']->get_queried_object_id();
$tags = get_the_tags( $post_id );
if ($tags && !is_wp_error($tags)) {
$tag_ids = array();
foreach ($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
$args = array (
'tag__in' => $tag_ids,
'post__not_in' => [$post_id]
);
$related_tags_posts = new WP_Query( $args );
wp_reset_postdata();
}
Here is where I'm printing the posts.
<?php while ($related_tags_posts->have_posts()) : $related_tags_posts->the_post() ?>
<?php the_title(); ?>
<?php endwhile; wp_reset_postdata(); ?>
But it's printing just one post from each matching tag. How do I print ALL posts that have matching tags? Not sure what I'm missing.
Please have a look below:
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile;
}
wp_reset_query();
}
I hope it would help you out.
<?php //
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('showposts=3' . '&paged='.$paged);
?>
<ul class="column-three">
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>">
<?php if (has_post_thumbnail()){ the_post_thumbnail('post-thumbnails'); } else {echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/b.jpg'. '"/>'; }?>
<span class="title"><?php the_title(); ?></span>
<span class="incrypt"><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,30); ?></span>
<span class="read-more">Read More</span>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
before code above is working until wordpress updates, my problem is the number of post display must be 3 post only but it display more than 3 is there any hope or is there something messing in my code above
Well, the showposts parameter is replaced with posts_per_page. So try with
$wp_query->query('posts_per_page=3' . '&paged='.$paged);
You could use:
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
);
$query = new WP_Query( $args );
Your Code is working for me and I have make page-test.php and get posts in that page and "showposts" working for me still use "posts_per_page" as mentioned by #jogesh_pi comment and also try below code that will also get posts by get_posts function.
$post_list = get_posts( array(
'post_type' => 'post',
'numberposts' => 3,
'orderby' => 'menu_order',
'sort_order' => 'asc'
) );
?>
<ul class="column-three">
<?php foreach ( $post_list as $post ) { ?>
<li>
<a href="<?php the_permalink(); ?>" id="post-<?php $post->ID; ?>">
<?php if (has_post_thumbnail($post->ID)){ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
?>
<img src="<?php echo $image[0]; ?>"/>
<?php
} else {echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/b.jpg'. '"/>'; }?>
<span class="title"><?php echo $post->post_title; ?></span>
<span class="incrypt"><?php $excerpt = $post->post_excerpt; echo substr( $excerpt , 0, 30);?></span>
<span class="read-more">Read More</span>
</a>
</li>
<?php } ?>
</ul>
<?php
wp_reset_postdata();
Try With this.
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '-1'
);
$query = new WP_Query ($args );
while ( $query->have_posts() ): $query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; wp_reset_postdata();
?>
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();
}
Can anyone please help me how to display the woocommerce product details based on category id?
I know how to display product details based on category name. The code is,
<ul class="productshome">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2, 'product_cat' => 'Salwar-Kameez', 'orderby' =>'rand','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="producthome">
<a>post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
In the above code 'product_cat' => 'Salwar-Kameez' will display the category name of salwar-kameez. But i need the product details should display based on category id
Changing 'product_cat' => 'Salwar-Kameez' to tag_ID' => 15 where 15 is the category id.
Also the code you proved above has some errors. You didn't close the <ul>, you didn't close the <li>, had a </div> at the end but no opening div and your link wouldn't work. I have fixed those:
<ul class="productshome">
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 55, 'tag_ID' => 15, 'orderby' =>'rand','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="producthome">
<a href="<?php echo get_permalink($loop->post->ID) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>