WordPress.org

Forums

A more complex in_category check..is this possible? (4 posts)

  1. wpNewbie80
    Member
    Posted 6 years ago #

    Hello, is it possible to do something to the equivalent of this

    if( in_category('5') || in a child category of category 6 )

    I have tried my best to look but I don't know if I just havn't found the right bit or if it is not possible to do within an if like above.

    thank you

    J

  2. slambert
    Member
    Posted 6 years ago #

    explain better what you're trying to do. Do you want to call both category 5 and children of category 6?

  3. wpNewbie80
    Member
    Posted 6 years ago #

    thank you for replying.

    category 5 is my ebook category. All categories that are a child of category 6 are subjects that an ebook is on.

    an ebook post will also appear in one the subject categories, along with other posts.

    Basically, when viewing a single post within category 5 ie an ebook, i want to display in a floated div, an image and paypal button for that ebook. On posts in a category that is a child of category 6...I want to show in a floated div the ebook for that 'subject' but instead of a paypal button, just show a link to the ebook's post. I can do the necessary for the div / paypal but it is just how to work out which, if any, of the 2 options, I had to do, from within my single.php

    If the post is in a category that is a child of 6, ie it is related to a particular subject, I will then have to get the necessary ebook's info to display.

    If that makes sense?

  4. wpNewbie80
    Member
    Posted 6 years ago #

    I have come up with this..it seems to work but I'm not sure if its luck or judgement! the function came from pointlessjon's post

    code is outside the main loop

    $tempQuery = $wp_query;
    
        $currentId = $post->ID;
    	$catToUse = "";
    
    	   function post_child_of_cat($child_post_id, $parent_cat_id){
    		$cat_array = wp_get_post_categories($child_post_id);
    		   foreach ($cat_array as $category){
    			if (cat_is_ancestor_of((int)$parent_cat_id, (int)$category)){
    			    global $catToUse;
                                $catToUse = $category;
          			    return true;
    			 }
    		     }
    		     return false;
    		}
    
            if ( ( $isAguide=in_category('16') ) || ( $needsAguide=post_child_of_cat($post->ID, 6) ) ){
    
    		 if($needsAguide == true){
                     //the post is from a subject category and it needs a guidebook
    			    $GetTheGuideBook = new WP_Query(array('category__and'=>array(16,$catToUse)));
    //we know the guidebook post will be in category 16 and the current post's subject category
    
    				 if($GetTheGuideBook ->have_posts()) : while($GetTheGuideBook ->have_posts()) : $GetTheGuideBook ->the_post();
    				   $guideBookPostID = $post->ID;
                       $guideLink ='<a href="'. get_permalink() .'" title="' .the_title("", "", false). '">'. the_title("", "", false) .'</a>';
    				   $guideImage = get_post_meta($post->ID, 'guideImage', $single ='true');
    
    				   $guideName = get_post_meta($post->ID, 'guideName', $single ='true');
    
    				   $guidePrice = get_post_meta($post->ID, 'guideAmount', $single ='true');
    
    			     endwhile;
    
    			   endif;
    			 }
    		}
        $wp_query = $tempQuery;

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.