PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] How to find one categories all posts' all category? And again specific category form those categories.?

 July 09, 2022     categories, php, posts, wordpress     No comments   

Issue

I have one category named City and Second category Named Decease Inside City different cities will be there and inside Decease different decease will be there.

I will have posts having one city and more than one decease.

So what I want is when I click on One City I want to show all the decease belonging to this category.

So In detail with one city suppose 4 posts are there and all posts belongs to some decease So I need to find that how many posts are there having one category as this city. And then I need to find All the categories of all these posts and from that I need to find which categories parent category name is Decease. And then I need to show those categories.

How I should code this in wordpress in a nice manner? If some one has already spend some time on doing like this code then Kindly guide what will be good way to do this.


Solution

$deseace_cat_id = getCareTypeCategoryId(); 
    $careType=array();
    if(have_posts())
    {
        $cats = array();
        $cat_ids=array();                           

         while (have_posts()) : the_post();
            $post_categories = wp_get_post_categories( $post->ID );
            foreach($post_categories as $c){                
                $cat = get_category( $c );
                if(!in_array($c, $cat_ids) && $cat->category_parent==$deseace_cat_id)
                {
                    $cat_ids[]=$c;          
                    $cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );                          
                }               
            }                                                           
            echo "<br>";
         endwhile;
         for($i=0;$i<count($cats);$i++)
         {
            ?>
            <a class="careTypeUrl" href="#" careSlug='<?php echo $cats[$i]['slug'];?>'><?php echo $cats[$i]['name'];?></a><br>
            <?php   
         }       
    }


Answered By - Dena
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing