This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

check if posts are in custom taxonomy

Home Forums Product Support Forums Ajax Search Pro for WordPress Support check if posts are in custom taxonomy

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #41578
    kokosankokosan
    Participant

    Hi, I almost finish. Could you tell me abit about this? I want to show div in the result if posts are in custom taxonomy “topic1” (for example). I have different design to show based on custom taxonomy. In the result template, I don’t kow how to check if posts are in taxonomy “topic1”.

    <?php 
    $key = count_val';
    if ($r->content_type == 'pagepost' ) {
    $meta_value = get_post_meta( $r->id, $key, true );
    ?>
    <div> //show this div on the result if posts are in taxonomy "topic1"
    <span><?php echo $meta_value;?></span>
    </div>
    <?php
    }
    ?>
    • This topic was modified 3 years, 3 months ago by kokosankokosan.
    #41597
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi,

    I am not exactly sure where this code is placed, so it highly depends. But you are using get_post_meta, which is for custom fields and not taxonomies. If you are checking for taxonomies, then maybe use the get_terms function or similar.

    #41602
    kokosankokosan
    Participant

    Hi, sorry for not being clear. The code below displays the div on every post. I want to display the div below only if a post has custom taxonomy “topic1”. In this case, use $r->content_type == 'pagepost' or 'term'?

    <?php
    if ($r->content_type == ‘pagepost’ ) {
    ?>
    <div>show this div on the result if posts are in taxonomy “topic1″</div>
    <?php
    }
    ?>

    #41606
    Ernest MarcinkoErnest Marcinko
    Keymaster

    But the problem is, that posts can’t have taxonomies – posts can only have terms from custom taxonomies associated to them.
    Taxonomies are the “group” for terms, for example categories are part of a taxonomy called “categories” etc..

    If you want to check if the post is in a specific category, then use the has_term() function. With this you can check if a post is within a specific category or any other taxonomy term.

    #41610
    kokosankokosan
    Participant

    I can get it done now, somehting like this

    add_filter( 'asp_results', 'asp_show_element_taxonomy', 10, 1 );
    function asp_show_element_taxonomy( $results ) {
    	foreach ($results as $k=>&$r) {
    	    $extext = get_post_meta($r->id, 'example_text', true);
          if (has_term('', 'exampletaxonomies',$r->id))
    	  	$r->content = "<span>$extext</span><span>show this div</span>";
    	}
    	return $results;
    }

    Very awesome plugin, actually no need to create a separated custom post type to achieve this like I think before.

    #41611
    kokosankokosan
    Participant

    One problem is that filter by this taxonomy is not working. I asign two posts to this taxonomy and add it to filter. When I click this filter, it show all posts, in stead of the twe posts that has this taxonomy . But, I see the two posts if use Uncategorized category filter.

    #41617
    Ernest MarcinkoErnest Marcinko
    Keymaster

    For the filter, try this configuration: https://i.imgur.com/Iu8w0WB.png
    That should exclude posts which have not been assigned any terms from the taxonomy.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.