• Resolved Aimee

    (@greatdanemaniac)


    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]

Viewing 15 replies - 1 through 15 (of 21 total)
  • 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).

    Thread Starter Aimee

    (@greatdanemaniac)

    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?

    Thread Starter Aimee

    (@greatdanemaniac)

    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?

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

    Thread Starter Aimee

    (@greatdanemaniac)

    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…

    <?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> ';
        }
      }
    }
    ?>
    Thread Starter Aimee

    (@greatdanemaniac)

    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!

    You would likely put that after the_content().

    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;
    }
    ?>

    Thread Starter Aimee

    (@greatdanemaniac)

    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…

    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.

    Thread Starter Aimee

    (@greatdanemaniac)

    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!

    Thread Starter Aimee

    (@greatdanemaniac)

    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…

    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

    Thread Starter Aimee

    (@greatdanemaniac)

    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…https://net.tutsplus.com/tutorials/wordpress/introducing-wordpress-3-custom-taxonomies/

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Display taxonomies using custom page template?’ is closed to new replies.