• <?php if ( get_the_term_list( get_the_ID(), 'category1', true) ||  get_the_term_list( get_the_ID(), 'category2', true )) { ?>
    
                            <?php echo get_the_term_list( get_the_ID(), 'category1', "", "", "" ) ?>
                            <?php echo get_the_term_list( get_the_ID(), 'category2', "", "", "" ) ?>
                <?php } ?>

    This code basicly checks both categories if they got something to show and if they do (any of them) , it loads which ever has the info.
    Now i want to create another condition which does this
    <?php if ( $postcount = 1 ) { ?> and <?php if ( $postcount > 1 ) { ?>

    But i couldnt seem to manage it. It doesnt have to have postcount code in it. I just want to filter the output based on the quantity of it.

    I would apriciate any help.

    Thanks.

Viewing 15 replies - 1 through 15 (of 35 total)
  • If you want counts for each term, you need to use get_the_terms() instead of get_the_term_list().

    You will need to use code similar to that shown in the article above to list the terms.

    Thread Starter Aaron

    (@gardi)

    Is it me or does wp has few hundred way of doing the same thing. Im kinda confused with all the answers around the web and cant seem to solve this
    problem.

    Ive read through the doc and used the code but it seems it only works with one taxonmy otherwise it gives “foreach” error. Also coundnt manage to use “if has post more than 3 than use this, if else use this” kinda code.

    Anywayz. Thanks alot vtxyzzy.

    Please describe in words (not code) what you want to do.

    Thread Starter Aaron

    (@gardi)

    Well its not easy to describe but will give it a try. This is a commercial project thus i cannot go in to details of the “logic” of the examples (like; currently its not possible to dispose of the structure)

    I have 2 post types with similar but separete custom taxonomies. Like, lets say Actors and Directors. In NEWS post, i would like to show them in one box called “Related People”. If no one is related to this News than no box appears (which i did through the /’category1′, true /)

    Upto this point ive managed it. I have no problems with that.

    Now i want to add some more customisation by conditioning the out put to check the number of the out put. As in; if only a person is related to this news than the box will say = “Person Related to this News” And if it is more than 1, than the box should say “People Related to this Post

    Does this make any sense now?

    That helps, but not quite enough.

    In general there could be multiple terms under a taxonomy, or multiple posts for a single term. So, how do you define ‘more than one person’?

    There seem to be several possibilities:

    • More than one term under Actors and none under Directors
    • More than one term under Directors and none under Actors
    • At least one term under each of Actors and Directors
    • Only one term under either Actors or Directors, but with more than one post

    Can you please define exactly what you need to test for ‘more than one person’?

    I guess one simpler way to state it might be:

    Use ‘more than one person’ if the total number of posts in Actors and Directors is greater than one.

    Thread Starter Aaron

    (@gardi)

    Im aware this doesnt make sense from the Actor vs Director point of view but basicly the fields “realy values” which are not actors or directors can only have one of the either field as either singular result (like only one actor OR director) or multiple and combined kinda result (in this case Actors and Directors combined to give Crew).

    SO maybe if i said “Person related to the news and Crew members related to the news , would be much better example.

    I understand you example. So im saying, when its multiple, it really is 2 + with both fields and when its singular, its just one of the two fields.

    Thread Starter Aaron

    (@gardi)

    Alright for the sake of going one step at a time, what would be the code (or the document i can read so i can implement the code from) for differentiating the singular and plural results

    Like just for the actors.

    <?php if ( get_the_term_list( get_the_ID(), 'actors', true ) {?>
    
    						<?php if $term= (actors) =1  ?>  Actor:
                            <?php echo get_the_term_list( get_the_ID(), 'actors', "", ",", "" ) ?>
    <? if else (actors) >2  ?> 
    
    Actors:
                            <?php echo get_the_term_list( get_the_ID(), 'actors', "", ",", "" ) ?> 
    
                <?php } ?>

    Im looking somewhere along this. (I know the code doesnt work, i just wanted to give an example of what im planing on doing and to give you an idea of what i mean to do)

    Let me see if I got it.

    If there is only one of Actors or Directors
       Use singular
    Else
       Use plural
    Thread Starter Aaron

    (@gardi)

    Yes.

    And if i may add that

    _If Actors or Directors exists than

    __ If there is only one of Actors or Directors
    ____Use singular
    __Else
    ____Use plural

    _if no actor or director exist
    /empty/
    _end

    The first condition is the base of what i currenlty use and have. I would like to add on top of that if possible.

    I think this will do what you want:

    <?php if ( $cat1 = get_the_term_list( get_the_ID(), 'category1', '', '', '') ||
          $cat2 = get_the_term_list( get_the_ID(), 'category2', '', '', '' )) {
       if ( $cat1 && $cat2 ) {
          // Plural code
          echo $cat1;
          echo $cat2;
       } else {
          // Singular code
          echo $cat1;
          echo $cat2;
       }
    } ?>
    Thread Starter Aaron

    (@gardi)

    Well ive tried. But it didnt work. It only gives out output as ” 1 ” ( i dont know why only 1)

    <?php if ( $cat1 = get_the_term_list( get_the_ID(), 'actors', '', '', '') ||
          $cat2 = get_the_term_list( get_the_ID(), 'directors', '', '', '' )) {
       if ( $cat1 && $cat2 ) {
          // Plural code
    	  echo 'Crew';
          echo $cat1;
          echo $cat2;
       } else {
          // Singular code
    	  echo 'Person ';
          echo $cat1;
          echo $cat2;
       }
    } ?>

    Odd. This code snippet works for me:

    $cat1 = get_the_term_list( 1298, 'category', '', ', ', '');

    so I know the basic code is correct.

    Time for some debugging. Insert debugging lines like this:

    <?php if ( $cat1 = get_the_term_list( get_the_ID(), 'category1', '', '', '') ||
          $cat2 = get_the_term_list( get_the_ID(), 'category2', '', '', '' )) {
       $id = get_the_ID();
       print_r("<p>ID:$id  CAT1:" . htmlentities($cat1) . ' CAT2:' . htmlentities($cat2) . '</p>');
       if ( $cat1 && $cat2 ) {
    Thread Starter Aaron

    (@gardi)

    it gave out
    ID:819 CAT1:1 CAT2:Person1

    By the way, there are several Actor and Directors are attached to the post. And they are not displaying.

    Please change the debugging lines to this:

    $id = get_the_ID();
       $cat1 = get_the_term_list(819, 'actors');
       print_r("<p>ID:$id  CAT1:" . htmlentities($cat1) . ' CAT2:' . htmlentities($cat2) . '</p>');
Viewing 15 replies - 1 through 15 (of 35 total)
  • The topic ‘get the term list with postcount based output.’ is closed to new replies.