Display post count in each category - wordpress - wordpress

The code below displays correctly except for the part that is suppose to show post count in the respective categories. What will be to correct syntax to use? Thanks!
<ul class="cat-menu list-group">
<?php $category_ids = get_all_category_ids();
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<li class="list-group-item d-flex align-items-center"><i class="fa fa-chevron-right" aria-hidden="true"></i>' . $category->name . '<span>' . $the_query->found_posts . '</span></li>';
}
?>

Try this-
<ul class="cat-menu list-group">
<?php
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
echo '<li class="list-group-item d-flex align-items-center"><i class="fa fa-chevron-right" aria-hidden="true"></i>' . $category->name . '<span>' . $category->category_count . '</span></li>';
}
?>
</ul>

Very thanks #Arif-Khan.
and it is a simple update for persian Websites.
<div class="card card-body pb-0">
<div class="single-post">
<p class="font-bold dark-grey-text text-center spacing grey lighten-4 py-2 mb-4">
<strong>دسته بندی</strong>
</p>
<ul class="cat-menu list-group my-4">
<?php
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
echo '<li class="list-group-item d-flex justify-content-between align-items-center"><a href="' . get_category_link( $category->term_id ) . '">
<i class="fas fa-chevron-left blue-text" aria-hidden="true"></i><p class="blue-grey-text mb-0 pr-1 d-inline-block">
' . $category->name . '</p></a>
<span class="badge badge-pill blue-grey font-small">' . $category->category_count . '</span></li>';
}
?>
</ul>
</div>
</div>

Related

How to define fallback image for get_the_post_thumbnail_url inside a shortcode

My shortcode is working fine but how to set a fallback image in there? Please suggestion
// display default post as a shortcode with prev/next link
function genericposts_shortcode($atts){
extract( shortcode_atts( array(
'expand' => '',
), $atts) );
global $paged;
$posts_per_page = 10;
$settings = array(
'showposts' => $posts_per_page,
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'ASC',
'paged' => $paged
);
$post_query = new WP_Query( $settings );
$total_found_posts = $post_query->found_posts;
$total_page = ceil($total_found_posts / $posts_per_page);
$list = '<div class="bloglist">';
while($post_query->have_posts()) : $post_query->the_post();
$list .= '
<div class="bloglist__item">
<div class="bloglist__imgwrap">
<img src=" '. get_the_post_thumbnail_url(get_the_ID(),'full') .'">
</div>
<div class="bloglist__wrapt">
<div class="bloglist__datetitle">
<span class="bloglist__date"> '. get_the_date( 'd' ) . '</span>
<span class="bloglist__mnt"> '. get_the_date( 'M' ) . '</span>
<span class="bloglist__yr"> '. get_the_date( 'Y' ) . '</span>
<div class="bloglist__title"> '. get_the_title() .' </div>
</div>
<div class="bloglist__more">
Read More
</div>
</div>
</div>
';
endwhile;
$list.= '</div>';
if(function_exists('wp_pagenavi')) {
$list .='<div class="page-navigation">'.wp_pagenavi(array('query' => $post_query, 'echo' => false)).'</div>';
} else {
$list.='
<span class="prev-posts-links">'.get_previous_posts_link('Previous page').'</span>
<span class="next-posts-links">'.get_next_posts_link('Next page', $total_page).'</span>
';
}
return $list;
}
add_shortcode('post-shortcode', 'genericposts_shortcode');
I have solved the issue below way.
add_shortcode( 'post-shortcode', 'genericposts_shortcode' );
function genericposts_shortcode( $atts ) {
ob_start();
global $paged;
$posts_per_page = 9;
$settings = array(
'showposts' => $posts_per_page,
'post_type' => 'post',
'orderby' => 'menu_order',
'order' => 'ASC',
'paged' => $paged
);
$post_query = new WP_Query( $settings );
$total_found_posts = $post_query->found_posts;
$total_page = ceil($total_found_posts / $posts_per_page);
if ($post_query->have_posts() ) { ?>
<div class="bloglist">
<?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
<div class="bloglist__item" id="post-<?php the_ID(); ?>">
<div class="bloglist__imgwrap">
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('full');?>
<?php } else { ?>
<img src="https://kanooelite.com/upgrade/wp-content/uploads/2020/11/placeholder.png" />
<?php } ?>
</div>
<div class="bloglist__wrapt">
<div class="bloglist__datetitle">
<span class="bloglist__date"> <?php echo get_the_date( 'd' ); ?></span>
<span class="bloglist__mnt"> <?php echo get_the_date( 'M' ); ?></span>
<span class="bloglist__yr"> <?php echo get_the_date( 'Y' ); ?></span>
<div class="bloglist__title"> <?php echo get_the_title(); ?></div>
</div>
<div class="bloglist__more">
Read More
</div>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<span class="prev-posts-links"><?php echo get_previous_posts_link('Previous page'); ?></span>
<span class="next-posts-links"><?php echo get_next_posts_link('Next page', $total_page); ?></span>
<?php $myvariable = ob_get_clean();
return $myvariable;
} ?>
<?php } ?>

Woocommerce custom product loop no longer working after update

I'm having an issue with a custom product loop for featured products. It worked fine before a update.
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) {
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice"><span style="text-decoration: line-through;">Only '.$currency.$price.'</span><span style="color: green;"> SALE '.$currency.$sale.'</span></div>VIEW</div>';
}
elseif($price)
{
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice">Only '.$currency.$price.'</div>VIEW</div>';
}
endwhile;
wp_reset_query();
?>
I've tried fixing the Permalinks but it hasn't sorted the issue. I'm no expert at Woo or even Wordpress but this loop did work.
Thank you for any help.
If you want to display featured products then do use below code in your query args it will solve the issue.
<?php
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_query' => $meta_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) {
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice"><span style="text-decoration: line-through;">Only '.$currency.$price.'</span><span style="color: green;"> SALE '.$currency.$sale.'</span></div>VIEW</div>';
}
elseif($price)
{
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice">Only '.$currency.$price.'</div>VIEW</div>';
}
endwhile;
wp_reset_query();
?>

how to show content in the wordpress recent post?

<div class="row">
<h3>Recent Posts</h3>
</div>
<div class="row recent-post-style">
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
echo the_content('10');
}
?>
</ul>
</div>
the element the_content('10') is not display the post content.
The content of the post is part of the $recent array.
The key you need is post_content, which is the raw content of the post. If you want it formatting in the same way as the_content(), then you'll want to wrap it in wpautop(). If you wish to trim it by words (you seem to be doing the_content(10), you can use wp_trim_words().
Here's your amended code:
<div class="row">
<h3>Recent Posts</h3>
</div>
<div class="row recent-post-style">
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
echo wpautop(wp_trim_words($recent["post_content"], 10));
}
?>
</ul>
</div>

Duplicated posts while using Infinite Scroll + random order

I'm using wp_query with infinite scroll to display posts in a specific cpt archive page. When i set the 'orderby' to 'date', everything works ok, but when i change it to 'rand' the query returns the correct number of posts, but some of them are duplicated.
This is the code i'm using:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loopb = new WP_Query( array( 'post_type' => 'my_post_type', 'posts_per_page' => 10, 'paged' => $paged, 'order' => 'rand' ) );
$value = get_field('thumbnail_sizing');
?>
<?php while ( $loopb->have_posts() ) : $loopb->the_post();
$thumb_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'small-size' );
$full_img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full-size' );
$thumb_img_ratio = 70;
if (isset($thumb_img[1]) && isset($thumb_img[2]) && $thumb_img[1] > 0) {
$thumb_img_ratio = $thumb_img[2] * 100/$thumb_img[1];
} ?>
<div class="mosaic">
<a href="#" class="grayscale">
<div class="mosaic__image" style="padding-top: <?php echo $thumb_img_ratio;?>%;">
<img src="<?php echo $thumb_img[0]; ?>" data-src="<?php echo $full_img[0]; ?>" alt="<?php the_title();?>">
</div>
<div class="meta">
<div class="flex">
<div class="flex_item">
<h2 class="meta_title"><?php the_field('name1');?> <span class="divider">&</span> <?php the_field('name2');?></h2>
<hr class="separator">
<span class="cat">view image</span>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
Carry a random seed with you i don't know if WP_Query supports it but it would be RANDOM(SEED) (e.g. RANDOM(1234)) in straightmysql

Custom taxonomy list with children and post included, not working

I have this code that works fine but the problem is with the ordering of the displayed data. Now it is little disorder and posts are duplicated when you check subcategory position. What can I do to display a tree of categories with posts included in each category and subcategory, for example :
<h1>Prime Category 1</h1>
<ul>
<li>Post 1</li>
<li>Post 2</li>
<li>...</li>
</ul>
<h2>Sub Category 1</h2>
<ul>
<li>Post 1</li>
<li> Post 2</li>
<li>...</li>
</ul>
<h2>Sub Category 2</h2>
<ul>
<li>Post 1</li>
<li> ...</li>
</ul>
<h1>Prime Category 2</h1>
<h2>Sub Category</h2>
<ul>
<li>Post 1</li>
<li> ...</li>
</ul>
<h2>Sub Category</h2>
<ul>
<li>Post 1</li>
<li> ...</li>
</ul>
This is my code
<?php
$post_type = 'biblioteka';
$tax = 'kategoria-pozycji';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
'child_of' => $tax_term->term_id,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => 2,
'hierarchical' => true,
'caller_get_posts'=> 1);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h1><?php echo $tax_term->name; ?></h1>
<ul>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_query();
}
}
?>
Thanks for all help!
Thanks all for help... This is working solution:
<?php
$args=array(
'post_type' => 'biblioteka',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'kategoria-pozycji',
'pad_counts' => false
);
$categories=get_categories($args);
foreach ( $categories as $category ) {
if ( $category->parent > 0 ) {
continue;
}
echo '<h1 style="font-weight:bold">' . $category->name . '</h1>';
$querystr = "SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->terms
WHERE term_id = (" . $category->cat_ID . ")
AND term_taxonomy_id = (" . $category->term_taxonomy_id . ")
AND ID = object_id
AND post_type = 'biblioteka'
AND post_status = 'publish'
ORDER BY post_date DESC";
$posts = $wpdb->get_results($querystr, OBJECT);
echo '<ul>';
foreach ( $posts as $post ) {
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
$categories2 = get_terms('kategoria-pozycji',array('parent' => $category->term_id , 'hide_empty'=> '0' ));
foreach ( $categories2 as $category ) {
echo '<h2>' . $category->name . '</h2>';
$posts = get_posts( array( 'kategoria-pozycji' => $category->name, 'post_type' => 'biblioteka' ) );
echo '<ul>';
foreach($posts as $post) {
setup_postdata($post);
echo '<li>'; the_title(); echo '</li>';
}
echo '</ul>';
}
}
?>

Resources