Support » Developing with WordPress » Terms wont show up in custom page template.

  • Resolved kirasiris

    (@kirasiris)



    I have a custom page template which displays the children post of the current post and I do have as well two loop to display both the categories and tags for the current post ID but somehow is just not working…. though I have another custom page template similar o this one and that one works great!

    Here is the code that I’m using in my custom page template to display the categories from the current post ID, can you tell me where’s my error?

    
    <?php 
                        global $post;
                        $terms = wp_get_post_terms( get_the_ID(), 'categorias_anime', array( 'fields' => 'all' ) );
                        if ($terms) {
                            $output = array();
                            foreach ($terms as $term) {
                                $output[] = '<a href="' .get_term_link( $term->slug, 'categorias_anime') .'"><span class="label label-success">' .$term->name .'</span></a>';
                            }
                            echo join( ' ', $output );
                        } else {
                           echo '<a><span class="label label-success">Sin categoria</span></a>';
                        }
                        echo $terms;
                        ?>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Hi kirasiris,

    There’s not enough context to be sure, but I suspect $post has the wrong value so get_the_ID() does not work correctly. get_the_ID() either needs to be within the loop displaying the current post, or $post needs to be assigned the current post object so that get_the_ID() believes it is in the loop. If all is correct, $post->ID should work equally well. Despite that, get_the_ID() is the better choice. I mention $post->ID only as a possible confirmation test.

    Look here the whole html file but I already solved it, I forgot to put the reset_query :v . Thanks for helping suggesting tho.

    
    <?php 
    /*
    Template Name: Listado de Episodios
    Template Post Type: anime
    */
    ?>
    <?php get_header(); ?>
    	<div class="container">
            <?php echo get_breadcrumb(); ?>
        	<div class="row">
            	<!-- Sidebar -->
                <div class="col-md-3">
                    <div class="list-group">
                        <div class="list-group-item" style="padding:0;">
                          <?php if(has_post_thumbnail()) : ?>
                            <img class="img-responsive" style="width:100%;" src="<?php the_post_thumbnail_url(); ?>">
                          <?php else : ?>
                            <?php no_image(); ?>
                          <?php endif; ?>
                        </div>
                        <?php if (!$post->post_parent ) : ?>
                            <span class="list-group-item"><b>Estado:</b> <?php echo estado($post->ID); ?></span>
                        <?php endif; ?>
                    </div>
                </div>
                <!-- Main Content -->
                <div class="col-md-9">
                <?php if(have_posts()) : the_post() ?>
                	<!-- Titulo -->
                	<div class="panel panel-default">
                    	<div class="panel-heading" id="video_titulo">
                        	<h1 class="panel-title"><i class="fa fa-header" aria-hidden="true"></i> 
                        		<?php the_title(); ?>
                        	</h1>
                        </div>
                    </div>
                    <!-- Sinopsis -->
                	<div class="panel panel-default">
                    	<div class="panel-heading">
                        	<h1 class="panel-title"><i class="fa fa-book" aria-hidden="true"></i> Resumen</h1>
                        </div>
                        <div class="panel-body">
                        	<?php the_content(); ?>
                        </div>
                    </div>
                    <!-- Listado de Episodios -->
                	<div class="panel panel-default">
                    	<div class="panel-heading">
                        	<h1 class="panel-title"><i class="fa fa-list" aria-hidden="true"></i> Lista de Episodios</h1>
                        </div>
                        <div class="panel-body" id="video_player">
                        <?php 
                            $listados = new WP_Query(array(
                                'post_type' => 'anime',
                                'order' => 'DESC',
                                'post_parent'   => $post->ID,
                                'post_parent__not_in' => array(0),
                                'posts_per_page' => -1,
                            ));
                        ?>
                        <table class="table table-hover" id="listado">
                        <?php if($listados->have_posts()) : ?>
                                <tbody>
                            <?php while($listados->have_posts()) : $listados->the_post(); ?>
                                    <tr>
                                        <td>
                                            <i class="fa fa-youtube-play text-success"></i> <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
                                        </td>
                                    </tr>
                            <?php endwhile; ?>
                            <?php else : ?>
                                <div class="alert alert-danger" id="alerta_error">No existe ningun episodio.</div>
                                </tbody>
                        <?php endif; ?>
    /////// HERE
    <?php wp_reset_query(); ?>
                        </table>
                        </div>
                    </div>
                    <!-- Autores -->
                	<div class="panel panel-default">
                    	<div class="panel-heading">
                        	<h1 class="panel-title"><i class="fa fa-users" aria-hidden="true"></i> Autores</h1>
                        </div>
                        <div class="panel-body">
                        	
                        </div>
                    </div>
                    <!-- Categorias -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h1 class="panel-title"><i class="fa fa-tag" aria-hidden="true"></i> Categorias</h1>
                        </div>
                        <div class="panel-body">
                        <?php 
                        global $post;
                        $terms = wp_get_post_terms( get_the_ID(),'categorias_anime');
                        if ($terms) {
                            $output = array();
                            foreach ($terms as $term) {
                                $output[] = '<a href="' .get_term_link( $term->slug, 'categorias_anime') .'"><span class="label label-success">' .$term->name .'</span></a>';
                            }
                            echo join( ' ', $output );
                        } else {
                           echo '<a><span class="label label-success">Sin categoria</span></a>';
                        }
                        ?>
                        </div>
                    </div>
                    <!-- Tags -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h1 class="panel-title"><i class="fa fa-tags" aria-hidden="true"></i> Tags</h1>
                        </div>
                        <div class="panel-body">
                        <?php 
                        global $post;
                        $terms = wp_get_post_terms( get_the_ID(), 'tags_anime' );
                        if ($terms) {
                            $output = array();
                            foreach ($terms as $term) {
                                $output[] = '<a href="' .get_term_link( $term->slug, 'tags_anime') .'"><span class="label label-success">' .$term->name .'</span></a>';
                            }
                            echo join( ' ', $output );
                        } else {
                           echo '<a><span class="label label-success">Sin tags</span></a>';
                        }
                        ?>
                        </div>
                    </div>
                  <!-- Comentarios -->
                  <div class="panel panel-default">
                      <div class="panel-heading">
                          <h3 class="panel-title"><i class="fa fa-comments"></i> Comentarios
                          <span class="pull-right">
                              <div class="fb-like" data-href="<?php the_permalink(); ?>" data-layout="button" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>
                          </span>
                          </h3>
                      </div>
                          <?php comments_template(); ?>
                  </div>
                <?php endif; ?>
                </div>
            </div>
        </div>
    <?php get_footer(); ?>
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Terms wont show up in custom page template.’ is closed to new replies.