WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display taxonomies using custom page template? (22 posts)

  1. greatdanemaniac
    Member
    Posted 4 years ago #

    Hi!
    I got this code off of WP, and it's awesome.

    <?php
    /**
     * Template Name: Page for the tutorials
     *
     * Selectable from a dropdown menu on the edit page screen.
     */
    ?>
    
    <?php get_header(); ?>
    
    		<div id="container">
    			<div id="content">
    <?php
    $type = 'tutorial';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'paged' => $paged,
      'posts_per_page' => 10,
      'caller_get_posts'=> 1
    );
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);
    ?>
    
    <?php
    
     get_template_part( 'loop', 'index' );?>
    			</div><!-- #content -->
    		</div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    However, I do not know on how to display the taxonomies there, for my custom post type. I got it to work before by using this type of code, and that worked awesome as well, except for the fact that the metadata (posted in, by author etc.) wasn't displayed.

    <?php
    /*
    Template Name: Tutorials
    */
    ?>
    
    <?php get_header(); ?>
    
        <div id="container">
    
          <div id="content">
    
    <?php
    $tutorial = new WP_Query( array( 'post_type' => 'tutorial', 'posts_per_page' => 10, 'orderby' => 'rand' ) );
    
    while ( $tutorial->have_posts() ) : $tutorial->the_post();
    
    	the_title( '<h2><a href="' . get_permalink() . '" >', '</a></h2>' );
    	?>
    	<div class="entry-content">
    		<?php the_content(); ?>
    
    <?php echo get_the_term_list( $post->ID, 'language', '<p>Language: ', ', ', '</p>' ); ?>    
    
        </div>
    
    <?php endwhile; ?>
    
          </div><!-- #content -->
    
        </div><!-- #container -->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    Also I'd like to know how to get the CPT's displayed within the loop. Justin Tadlock's recipe for that isn't working anymore...

    add_filter('pre_get_posts','my_get_posts');
    
    function my_get_posts($query ){
    
    	if ( is_home())
    		$query->set('post_type', array('post', 'page', 'album', 'movie', 'quote', 'attachment'));
    
    	return $query;

    Thanks a lot!

    [sig moderated]

  2. MichaelH
    Member
    Posted 4 years ago #

    Since that code from the Pages article is meant for the TwentyTen theme then you will need to add

    <?php echo get_the_term_list( $post->ID, 'language', '<p>Language: ', ', ', '</p>' ); ?>

    to the loop.php in that theme (or your child theme).

  3. greatdanemaniac
    Member
    Posted 4 years ago #

    Ok, that I know... I'm just wondering where to put it. I've tried to put it in different kind of places, but none of them have worked so far... Do you know where I should put it?

  4. greatdanemaniac
    Member
    Posted 4 years ago #

    I actually solved it a bit. Found out that the best way to put it is below the_content() but it seems that I can only display one taxonomy at a time if I do so... I have several sites in my network who use the same theme...and the other site contains a lot of taxonomies... I'd like to display them all, but I don't know what I'm doing wrong.

    How do I go on from here?

  5. MichaelH
    Member
    Posted 4 years ago #

    So you want to display the taxonomies and the taxonomy terms that are on a given post?

  6. greatdanemaniac
    Member
    Posted 4 years ago #

    Yes, exactly!
    Otherwise, what do you have taxonomies for?

    I've tried with queries, but it doesn't seem to work.
    I'd really like a deep yet simple tutorial on this...

  7. MichaelH
    Member
    Posted 4 years ago #

    <?php
    //get all taxonomies and terms for this post (uses post's post_type)
    foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
      $object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
      if ($object_terms) {
        echo 'terms for '.$taxonomy;
        foreach ($object_terms as $term) {
          echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a><p> ';
        }
      }
    }
    ?>
  8. greatdanemaniac
    Member
    Posted 4 years ago #

    That code doesn't seem to work, but I don't know exactly where to put it either... Do I need to change something in that code so that it fits my needs, or what?

    I'm a huge newbie at this stuff, and I haven't taken a course in php. I'm just self taught by experiments, so I need a little more help than that.

    I just want to display the taxonomies in the post, just like Justin Tadlock's movie database

    It shouldn't be that hard, really... I don't use custom fields either since I haven't been able to understand how just yet. I find taxonomies a lot easier, if I just can be able do display them all if there are any taxonomies to display in the post. If not, then they should not be displayed. I know there is a solution for this!

  9. MichaelH
    Member
    Posted 4 years ago #

    You would likely put that after the_content().

  10. MichaelH
    Member
    Posted 4 years ago #

    If you do know the specific taxonomy you want to display you could use this:

    <?php
    // in post loop, display taxonomy 'genre' terms for each post
    $terms = wp_get_post_terms( $post->ID , 'genre', '');
    if ( !is_wp_error($terms) && $terms ) {
    $term_text = 'Genre ';
        foreach ($terms as $term) {
          $term_text .='<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> ';
        }
    echo $term_text;
    }
    ?>
  11. greatdanemaniac
    Member
    Posted 4 years ago #

    That doesn't work either. Doesn't display a thing...

    What if I instead create a taxonomy.php file with all taxonomies in them and then call for them like this: get_template_part( 'loop', 'index' );?>

    I really don't know how to do it... haven't found a tutorial yet on the subject... Seems like I either have to give up using taxonomies, or go back to the way I used to, since that worked like a charm

    <?php
    /*
    Template Name: Tutorials
    */
    ?>
    
    <?php get_header(); ?>
    
        <div id="container">
    
          <div id="content">
    
    <?php
    $tutorial = new WP_Query( array( 'post_type' => 'tutorial', 'posts_per_page' => 10, 'orderby' => 'rand' ) );
    
    while ( $tutorial->have_posts() ) : $tutorial->the_post();
    
    	the_title( '<h2><a href="' . get_permalink() . '" >', '</a></h2>' );
    	?>
    	<div class="entry-content">
    		<?php the_content(); ?>
    
    <?php echo get_the_term_list( $post->ID, 'language', '<p>Language: ', ', ', '</p>' ); ?>    
    
        </div>
    
    <?php endwhile; ?>
    
          </div><!-- #content -->
    
        </div><!-- #container -->
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>

    The irritating thing about this fix, is that I cannot display the meta, like I explained before...

    I just don't get it... why can't I re-use the get_the_terms_list several times in this page template? It's so easy...

    I hope I'm not too annoying about this. Just don't know that much about PHP...

  12. MichaelH
    Member
    Posted 4 years ago #

    So you have a post type of tutorial.

    What taxonomies are you using for that post type?

    Yes, the get_term_list() should work for the other taxonomies.

  13. greatdanemaniac
    Member
    Posted 4 years ago #

    Yes, that's true. I'm using taxonomies for almost all my CPT's, but not tutorials actually. Not yet. I was just using the taxonomy in the page template for example usage.

    In my main blog I use the taxonomy "language" for a CPT called "Essays".
    I'm working on a database about the breed great danes, and I have about 4 CPT's and I use a whole lot of taxonomies for these CPT's, sometimes I use several of these for the same CPT.

    For example: I have a CPT for "Dog food" and I use these taxonomies for this type:

    brand
    fat content
    protein content
    quality
    source

    and one more, that I can't remember.
    To be more exact, my custom page template that I used as the send example works like a charm - BUT the entry-meta does not show, so either I use the new page template (like the first one) and find a way to display ALL taxonomies (one way or another) or I'd have to find a way to display the entry-meta in my old page template(second example).

    Phew, that was a lot. Thank you for helping me!

  14. greatdanemaniac
    Member
    Posted 4 years ago #

    Just realized that the old method isn't working at all. WP must have changed something, so if you wanna display CPT's, you must use the "new" way, like the first example...

  15. MichaelH
    Member
    Posted 4 years ago #

    Yes, that's true. I'm using taxonomies for almost all my CPT's, but not tutorials actually. Not yet. I was just using the taxonomy in the page template for example usage.

    Okay you got me befuzzzled!!!

    So will point you to some references
    Custom Post Types
    Custom_Taxonomies

  16. greatdanemaniac
    Member
    Posted 4 years ago #

    Ok, what is so hard to understand?

    I just want to find out how the heck I'm supposed to do, so that I can get my custom taxonomies FOR my custom post types to be displayed within my custom post types posts, or whatever I should explain it...

    The references I've read like a thousand times and I've googled my ass off, and I've found absolutely nothing that has helped me with displaying custom taxonomies within a custom post type (or a regular post for that matter). I've found a few things, and I've tried them all, but nothing works...

    The only thing I'd like to have working is the get_the_terms_list since it seems that's the best way to go, but I can only (like I've said before) put in one line of that code in the loop.php file in twenty ten theme. If I put in several, the whole theme sort of crashes...

    Sorry if I'm being a pain in the butt, but I really need help with this! nothing seems to work! The "last" thing I tried was this, but I couldn't be able to make it work either...http://net.tutsplus.com/tutorials/wordpress/introducing-wordpress-3-custom-taxonomies/

  17. MichaelH
    Member
    Posted 4 years ago #

    I have a custom post type of book. That custom post type has a taxonomy called genre. As an example, using the TwentyTen theme, in single.php, after this line
    <?php the_content(); ?>

    I put this:

    <php
    echo get_the_term_list( $post->ID, 'genre', '<p>Genre: ', ', ', '</p>' );
    ?>

    And I see the Genre terms for the Book I am viewing.

  18. greatdanemaniac
    Member
    Posted 4 years ago #

    Yes, I've tried that and it works, but not if I put several in there, then it stops working. Like this:

    <php
    echo get_the_term_list( $post->ID, 'language', '<p>Language: ', ', ', '</p>' );
    ?>
    
    <php
    echo get_the_term_list( $post->ID, 'subject', '<p>Subject: ', ', ', '</p>' );
    ?>

    etc.
    One works fine, but not more... that's the whole problem...

  19. MichaelH
    Member
    Posted 4 years ago #

    Same scenario I just described with additional taxonomy of writer and it works:

    <?php echo get_the_term_list( $post->ID, 'genre', '<p>Genre: ', ', ', '</p>' ); ?>
    <?php echo get_the_term_list( $post->ID, 'writer', '<p>Writer: ', ', ', '</p>' ); ?>
  20. greatdanemaniac
    Member
    Posted 4 years ago #

    What?

    Then something must be terribly wrong with my theme and blog! That just does not work for me, even though it should!

    I'm gonna try and get this to work until my fingers bleed and I'll get back to you with the results... this is insane!

  21. greatdanemaniac
    Member
    Posted 4 years ago #

    I've solved it! And I feel so F*cking stupid!

    I totally forgot that I erased all of my taxonomies for certain posts, and when I got them back, everything is working. I haven't tried to put the code in the loop, but I'll try. Otherwise I'll just use several custom page templates for both single and page display... I'd like to have both.

    I'd like to send you flowers for the help you've got me, Michael. Thanks a bunch!

  22. MichaelH
    Member
    Posted 4 years ago #

    You are welcome, glad you got it sorted out.

    I'll mark this resolved. Happy coding!

Topic Closed

This topic has been closed to new replies.

About this Topic