Omitting item from array, foreach loop wordpress - php

I have created a foreach loop for the category section in my sidebar. Everything is working good, however, I don't want to allow one of the categories to show. I have not been able to find anything on how to null out an object in query or anything.
here is my code, the name of the category is "general information"
<aside class="sidebar">
<div class="category" data-aos="fade-left" data-aos-delay="300">
<h2>Popular Categories</h2>
<ul class="category-list">
<?php
$args = array(
'orderby' => 'count',
'order' => 'DESC'
);
$terms = get_terms('category', $args); /*Name Of category*/
foreach (array_slice($terms, 0, 5) as $term ):
?>
<li class="list-items" data-aos="fade-left" data-aos-delay="400">
<a href="<?php echo site_url('/general');?>">
<?php echo
$term->name ;?> </a>
<span>(<?php echo $term->count ;?>)</span>
</li>
<?php
endforeach;
wp_reset_query();
?>
</ul>
</div>
I am guessing that there is a call I can put in the $args(). Any help would be greatly appreciated.

$args = array(
'orderby' => 'count',
'order' => 'DESC'
'exclude' => array( 77 ), // ID of Category which you don't want to show
);
You can exclude like this.

Related

How can I loop through all categories to display each category post separately?

I'm developing a simple wordpress site. I've created a home page named home.php and trying to dynamically display posts separately according to category. the code is the following:
<?php
get_header();
?>
<main id="main" class="row">
<?php
$categories = ['politics' => 'রাজনীতি', 'finance' => 'অর্থনীতি', 'sports' => 'ক্রীড়া', 'entertainment' => 'বিনোদন', 'fenii' => 'আমাদের ফেনী', 'religion' => 'ধর্ম', 'literature' => 'সাহিত্য', 'study' => 'পড়া-লেখা', 'quiz' => 'কুইজ', 'zodiac' => 'রাশিফল'];
foreach($categories as $key => $category):
$query_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 3,
'ignore_sticky_posts' => true,
'category_name' => $category,
);
$query = new WP_Query( $query_args );
?>
<section id="<?php echo $key ?>" class="col-sm-6 section">
<h3 class="title"> <?php echo $query->query['category_name'] ; ?> </h3>
<?php
if ( $query->have_posts()):
while ( $query->have_posts() ):
$query->the_post();
?>
<h5> <?php the_title() ?> </h5>
<p><?php the_excerpt() ?></p>
<?php
endwhile;
echo "<hr />";
wp_reset_postdata();
else:
echo "<h6> Sorry! No post has been found of this type. </h6>";
echo "<hr />";
endif;
?>
<hr />
</section>
<?php
endforeach;
?>
</main>
<?php
get_footer();
?>
But, no post is displayed like the following:
So, how can I display my posts according to category dynamically by foreach loop as I've been trying since the beginning?
Thank you.
Have you tried using category_id as the argument for category instead of category_name in your $query_args?
Also, you should get your categories dynamically instead of creating the array for categories manually. Otherwise, you'll have a problem if you add or remove categories in the future. Have a look at https://developer.wordpress.org/reference/functions/get_categories/.

WP Get IDs of parent categories only

The snippet below works fine but I've hard coded the parent categories IDs, Now I'm looking for a way to get rid of hard coded IDs.
<div class="row">
<?php
$catsArray = array(176, 175); // This line need to be dynamic and the IDs are parent categories.
$categories = get_terms(
array(
'hide_empty' => false,
'include' => $catsArray, 'orderby' => 'include'
) );
foreach ($categories as $key => $cat) {
$cat_thumb_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$term_link = get_category_link( $cat->term_id );
?>
<div class="col-md-6">
<div class="sellers-wrap is-revealing">
<figure>
<img src="<?php echo $cat_img; ?>" alt="" class="img-fluid">
</figure>
<div class="sellers-text">
<p><strong><?php echo $cat->name; ?></strong></p>
</div>
</div>
</div>
<?php } ?>
</div>
The first parameter you have to get_terms is an array of arguments. All possible values are documented here: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/ . It sounds like you just want to get all categories that are at the top level of the hierarchy (i.e. have no parent). So to do that you can just use the 'parent' argument and pass 0. Like this:
$categories = get_terms([
'hide_empty' => false,
'parent' => 0
]);
If 0 is passed, only top-level terms are returned.
$categories = get_terms(
'category',
array('parent' => 0)
);

Wordpress - How do I display posts grouped by custom taxonomy?

I am creating a FAQ page using a custom post type and custom taxonomy. I'm trying to create an unordered list for each taxonomy in order to group the FAQ's. In that unordered list, I want the first listed item to be the taxonomy name and then repeat the second listed item for all the questions in the taxonomy. This is the page I'm working on link.
It's currently duplicating the posts instead of displaying in the rightful taxonomies.
<?php
// get all the categories from the database
$cats = get_terms( array(
'taxonomy' => 'faq_categories',
));
// loop through the categories
foreach ($cats as $cat) {
// setup the category ID
$cat_id = $cat->term_id;
?>
<!-- Make a header for the category -->
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
// create a custom wordpress query
query_posts( array(
'post_type' => 'faqs',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories', //or tag or custom taxonomy
'field' => 'slug',
'terms' => 'for-women'
)
)
));
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category
wp_reset_postdata();
?>
</ul>
<?php } // done the foreach statement ?>
Your query is not changing as you iterate through the $cats array. Perhaps changing the the value of the 'terms' array to $cat->slug would give you better results.
Thank you so much. You have both provided great insight as to what I was missing. I've resolved it now and this is how I went about solving it taking your suggestions into consideration.
<?php
$cats = get_terms(
array(
'taxonomy' => 'faq_categories',
'orderby' => 'term_id',
'order' => 'ASC'
)
);
foreach ($cats as $cat) :
?>
<ul id="<?php echo $cat->slug; ?>" class="cd-faq-group">
<li class="cd-faq-title">
<h2>Questions <?php echo $cat->name; ?></h2>
</li>
<?php
$questions = new WP_Query(
array(
'category_name' => $cat->slug
)
);
$questions = new WP_Query( array(
'post_type' => 'faqs',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'faq_categories',
'field' => 'slug',
'terms' => array($cat->slug),
)
)
));
?>
<?php if ($questions->have_posts()) : while ($questions->have_posts()) : $questions->the_post();?>
<li>
<a class="cd-faq-trigger" href="#0"><?php the_title(); ?></a>
<div class="cd-faq-content">
<?php the_content(); ?>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
In your query_post, your tax_query's field should be term_id and your terms be assigned to your $cat_id variable, instead of a hardcoded term.

If post has parent add class

so I have this Issue that I cannot solve no matter what I do , thanks in advance for help :
What I am trying to do is that when I am on the post page , I give a class to the parent to its parent
I am using the following code to get the post id and its category id:
get_header();
$theCategory = get_the_category();
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 52,
'hide_empty' => 0
));
$thiscat = get_queried_object_id();
$catobject = get_category($thiscat,false); // Get the Category object by the id of current category
$catobject2 = get_category($catobject,false);
$parentcat = $catobject2->category_parent; // the id of the parent category
?>
<div class="container">
<?php echo category_description( get_query_var( 'cat' ) ); ?>
</div>
<?php var_dump($thiscat) ?>
After this , if the post is in the right categoty I am trying to add a class of active here :
<?php if( $categories ): ?>
<ul class="main-categ">
<?php foreach ( $categories as $kCat => $vCat ): ?>
<li class="main-item <?php echo $thiscat->category_parent == $vCat->cat_ID ? 'active' : ''; ?>"><?php
$subCategories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => $vCat->cat_ID,
'hide_empty' => 0
));?>
<a class="main-link"><?php echo $vCat->name; ?></a>
<?php if( $subCategories ): ?>
<ul class="subcateg">
<?php foreach ( $subCategories as $kSub => $vSub ): ?>
<li class="<?php echo get_query_var( 'cat' ) == $vSub->cat_ID ? 'activ':'';?>"><i class="fa fa-angle-double-right"></i> <?php echo $vSub->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Show sticky posts and sort them out according to their category

I would like to know if it's possible to show the sticky posts inside the wp_query and sort them out according to their respective categories:
loop(
- the first sticky post has the category 1
- the second sticky post has the category 2
- the third sticky post has the category 1
)
and that should display:
- category 1:
- the first sticky post
- the third sticky post
- category 2:
the second sticky post
with this html:
<div class="flex-6">
<h4><?php
$category = get_the_category();
echo $category[0]->cat_name;
?></h4>
<ul class="list">
<li><?php the_title(); ?></li>
</ul>
</div>
I have the correct loop for the sticky posts:
$sticky = get_option('sticky_posts');
$query = new WP_Query(array('post__in' => $sticky));
if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
$category_name = get_the_category();
$category_name = $category_name[0]->cat_name;
endwhile; endif;
To have this final result
<div class="flex-6">
<h4>Category 1</h4>
<ul class="list">
<li>The first title</li>
<li>The third title</li>
</ul>
</div>
<div class="flex-6">
<h4>Category 2</h4>
<ul class="list">
<li>The secondtitle</li>
</ul>
</div>
Any idead?
Thanks for your time
The most straightforward way would be to get the categories first:
<?php
$cat_args = array(
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'taxonomy' => 'category'
);
$cats = get_categories($cat_args);
and then cycle through them getting the posts:
$sticky = get_option('sticky_posts');
foreach ($cats as $cat) :
$args = array(
'post_type' => 'post',
'post__in' => $sticky,
'posts_per_page' => -1,
'orderby' => 'title', // or whatever you want
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cat->slug
)
)
);
$posts = get_posts($args);
if ($posts) :
?>
<div class="flex-6">
<h4><?php echo $cat->cat_name; ?></h4>
<ul class="list">
<?php foreach ($posts as $post) : ?>
<li><?php echo get_the_title($post->ID); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
endif;
endforeach;

Categories