• I created a custom post type and added categories to the custom post. My goal is to create a custom post ‘Resource’ and then categories and multiple titles.

    Appraisal Company:
    • Title one
    • Title two
    • Title Three

    but what I get is :

    Appraisal Company
    • Title 1

    Appraisal Company
    • Title 2

    Any help would be much appreciated.

    <!-- beginning of the directory -->
    			<?php $loop = new WP_Query( array( 'post_type' => array('Resources'), 'posts_per_page' => 8, 'order' => ASC,) ); ?>
    			<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    						<div id="resource">
    						<div <?php post_class( '', $tag_id ); ?>>
    						<h3 class='<?php the_ID(); ?>'>
    							<?php
    							$category = get_the_category();
    							echo $category[0]->cat_name;
    							?>
    						</h3>
    						<p>	<?php
    							 foreach((get_the_category()) as $category) {
    							 echo $category->category_description . ' ';
    							 }
    							 ?>
    						</p>
    						<ul>
    						<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    						</ul>
    						</div>
    						</div>
    
    			<?php endwhile; ?>
    		<?php wp_reset_query(); ?>
    		<!-- #end of the directory -->
Viewing 1 replies (of 1 total)
  • You need to save the current category name and print only when it changes. Try this (untested):

    <!-- beginning of the directory -->
    			<?php $loop = new WP_Query( array( 'post_type' => array('Resources'), 'posts_per_page' => 8, 'order' => ASC,) ); ?>
    			<?php $current_cat = ''; ?>
    			<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    						<div id="resource">
    						<div <?php post_class( '', $tag_id ); ?>>
    							<?php $category = get_the_category();
    							if( $current_cat != $category[0]->cat_name ) :
    								$current_cat = $category[0]->cat_name; ?>
    								<h3 class='<?php the_ID(); ?>'>
    									<?php echo $category[0]->cat_name; ?>
    								</h3>
    							<?php endif; ?>
    						<p>	<?php
    							 foreach((get_the_category()) as $category) {
    							 echo $category->category_description . ' ';
    							 }
    							 ?>
    						</p>
    						<ul>
    						<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    						</ul>
    						</div>
    						</div>
    
    			<?php endwhile; ?>
    		<?php wp_reset_query(); ?>
    		<!-- #end of the directory -->
Viewing 1 replies (of 1 total)
  • The topic ‘Creating a directory with Categories and Titles under that category’ is closed to new replies.