Results 1 to 10 of 10

Thread: Custom Taxonomies

  1. #1
    keith70 is offline Hello World
    Join Date
    Jul 2010
    Posts
    25

    Default Custom Taxonomies

    I am trying to show the category selected for a taxonomie. The first php code works and outputs a list of categories selected for the taxonomie. How would I show the category without a list and just output the word?

    PHP Code:
    <?php echo get_the_term_list($post->id'Skills' ,'Skills: '', ',''); ?>
     
    <?php
         $term 
    =get_term_by('Skills'get_query_var('term'), get_query_var('taxonomy'));
         echo 
    "<BR>Term name: ".$term->name;
     
    ?>

  2. #2
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    First and foremost, you are asking a question regarding the "Skills" taxonomy but using the word "categories". This makes it hard for me to understand exactly what you are getting at. In my experience, it is best to state this as "a term of the Skills taxonomy" or simply "a skill".

    It appears that there is only a small error in the code that you posted above. The first parameter passed to get_term_by() should be either 'slug', 'name', or 'id'.

    PHP Code:
    if( 'skills' == get_query_var'taxonomy' ) ) {
        
    $skill get_term_by'slug'get_query_var'term' ), get_query_var'taxonomy' ) );
        if( 
    is_object$skill ) ) {
            echo 
    "\n" '<br />Skill: ' $skill->name;
        }


  3. #3
    keith70 is offline Hello World
    Join Date
    Jul 2010
    Posts
    25

    Default

    Here is my code, notice near the bottom I have a comment that say what works ok and the code you wrote above I have put in, but it does not output anything at all.

    Code:
    <?php 
       global $post;
      $loop = new WP_Query(array('post_type' => 'event', 'posts_per_page' => 5)); 
      while ( $loop->have_posts() ) : $loop->the_post(); 
      $custom = get_post_custom($post->ID);
      $event_name = $custom["event_name"][0];
         $event_date = $custom["event_date"][0];
         $event_time = $custom["event_time"][0];
     
     ?>
             <div id="event-container">
                <div class="events">
                <h1><?php the_title(); ?></h1>
     
                <?php the_content(); ?>
     
                       <p>Event Name : <?php echo $event_name; ?></p>
                       <p>Event Date : <?php echo $event_date; ?></p>
                       <p>Event Time : <?php echo $event_time; ?></p>
                       <?php 
           if( 'skills' == get_query_var( 'taxonomy' ) ) {
              $skill = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
              if( is_object( $skill ) ) {
               echo "\n" . '<br />Skill: ' . $skill->name;  # Does not show anything
              }
         }  
         ?>
                    <?php echo get_the_term_list($post->id, 'Skills' ,'Skills: ', ', ',''); #works fine?> 
                        <?php echo "<BR>"; ?>
                        <?php echo strip_tags( get_the_term_list( $post->ID, 'Skills', 'Skills: ', ', ', '' ) ); #works fine ?>

  4. #4
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    Which template is this? The code I posted is meant to be used in a template that displays multiple posts associated with a given skill. Maybe I am confused as to what your goal is. By the code you posted in the original post It looked like you were trying to target taxonomy.php but from the code you posted above, it looks like you are building a custom template of some kind.

  5. #5
    keith70 is offline Hello World
    Join Date
    Jul 2010
    Posts
    25

    Default

    Yes wanting to do a custom template. I just want to be able to have each term for a taxonomy set to a variable. So I then can list the term anyway I may need to.

  6. #6
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    OK. I would try to use get_the_terms() in that case.

    PHP Code:
    /* Get all skills associated with this post. */
    $skills get_the_terms$post->ID'skills' );

    /* Print the first skill. */
    if( isset( $skills[0]->name ) ) {
        print 
    $skills[0]->name;


  7. #7
    keith70 is offline Hello World
    Join Date
    Jul 2010
    Posts
    25

    Default

    I could not get the above code to work. This is what I eventually got to work:
    Code:
     $terms = get_the_terms($post->id, 'Skills');
         foreach ($terms as $term){
              echo "TERM: " .$term->name . "<BR>";
              unset ($term);   
             }
    Thanks for your help, much appreciated!

  8. #8
    keith70 is offline Hello World
    Join Date
    Jul 2010
    Posts
    25

    Default

    Now I am having an issue of getting a drop down list point to the correct link:
    Can't get Custom Post Type to display after select with Drop Down List

    I dug deep into this and found this also: http://core.trac.wordpress.org/ticket/13258
    mfields, did you find a way to work this out?

  9. #9
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    I did, but it's a lot of hacking around. I have released a taxonomy widget plugin which includes dropdowns. You could either install it or use the code as a guide: http://wordpress.org/extend/plugins/taxonomy-widget/

  10. #10
    mfields's Avatar
    mfields is offline Here For The Peanuts
    Join Date
    Nov 2009
    Location
    Portland, OR
    Posts
    168

    Default

    This solution was posted on Forrst today which might be of use to you as well: http://forr.st/~yxN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •