WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Duplicate Categories being shown (3 posts)

  1. Blindacme
    Member
    Posted 1 year ago #

    Hi I am trying to setup my custom post types archive page and taxonomy, I have that setup no prob.. My dilemma is that I am trying to use the snippet below to show the categories available for my CPT with links to those categories archive pages.. Like I said everything is working fine except the categories show up multiple times.. I have attached the template hoping that someone can tell me what to do or what I am doing wrong and why it lists multiple times.. I do have a dropdown towards the header that allows users to filter out the page, but want to have the main archive just display the categories too. I hope that makes sense. Like I said it just lists multiple times so I dont know if I need to reset something or what..

    <?php
    /*
    Template Name: Archives for Sponsors
    
    */
    ?>
    <?php get_header(); ?>
          <div id="pagename" style="width:200px;float:left;">
            <h2><span>
    
    		<?php if (have_posts()) : ?>
    
    		Sponsors
    
    		</span></h2>
          </div>
          <div style="width:300px;float:right;padding:20px;">
          <?php get_search_form() ?>
          <div style="height:10px;"></div>
    <?php
    
      $categories = get_categories('taxonomy=sponsors_categories');
    
      $select = "<select name='cat' id='cat' class='postform'>n";
      $select.= "<option value='-1'>Select category</option>n";
    
      foreach($categories as $category){
    	if($category->count > 0){
    		$select.= "<option value='".$category->slug."'>".$category->name."</option>";
    	}
      }
    
      $select.= "</select>";
    
      echo $select;
    ?>
    
    <script type="text/javascript"><!--
        var dropdown = document.getElementById("cat");
        function onCatChange() {
    		if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
    			location.href = "<?php echo home_url();?>/?sponsors_categories="+dropdown.options[dropdown.selectedIndex].value+"/";
    		}
        }
        dropdown.onchange = onCatChange;
    --></script>
    
             </div>
             <div style="clear:both;"></div>
        </div>
        <!-- container END here -->
      </div>
      <!-- header END here -->
      <!-- content START here -->
      <div id="content">
        <!-- container START here -->
        <div class="container">
          <!-- main-colum START here -->
          <div id="main-colum" class="two_third">
          	<?php yoast_breadcrumb('<p id="breadcrumbs">','</p>'); 
    
    		?>
    		 <?php while ( have_posts() ) : the_post(); ?>
                     <!-- Display review title and author -->
                     <?php
    				 if ( is_post_type_archive('sponsor_post_types') ) {
    
    				  ?>
    				  <div><?php   $args = array( 'taxonomy' => 'sponsors_categories' );
    
    $terms = get_terms('sponsors_categories', $args);
    
    $count = count($terms);
    if ($count > 0) {
        $cape_list = '<p class="my_term-archive">';
        foreach ($terms as $term) {
            $i++;
        	$term_list .= '<a href="/sponsors_categories/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></p>';
    
        }
        echo $term_list;
    } ?></div>
    				  <?php
    			  } else {
    				 ?>
                    <div style="width:200px;float:left;"><a href="<?php the_permalink(); ?>">
                   <?php if ( has_post_thumbnail() ) {
    				    the_post_thumbnail( array( 100, 100 ) );
    			   } else {
                    the_title(); } ?></a>
                    </div>
    
                  <div style="width:200px;float:left;">
                  <?php echo esc_html( get_post_meta( get_the_ID(), 'sponsor_phone', true ) ); ?>
                  </div>
    
                  <div style="clear:both;"></div>
                  <hr />
                  <?php } ?>
             <?php endwhile; ?>
    
    <!-- Display page navigation -->
    
    <?php global $wp_query;
    if ( isset( $wp_query->max_num_pages ) && $wp_query->max_num_pages > 1 ) { ?>
        <nav id="<?php echo $nav_id; ?>">
       <div class="nav-previous"><?php next_posts_link( '<span class="meta-nav">&larr;</span> Older reviews'); ?></div>
       <div class="nav-next"><?php previous_posts_link( 'Newer reviews <span class= "meta-nav">&rarr;</span>' ); ?></div>
      </nav>
     <?php };
    endif; ?>
    
          </div>
         <!-- main-colum END here -->
    	<?php get_sidebar('blog'); ?>
    	</div>
        <!-- container END here -->
      </div>
      <!-- content END here -->
    </div>
    <!-- site-wrapper END here -->
    <?php get_footer(); ?>
  2. Blindacme
    Member
    Posted 1 year ago #

    I think the problem may be because I am pulling my categories into my template more than once? IE here are the areas I think causing the conflict from the full snippet above

    <?php if (have_posts()) : ?>
    
    		Sponsors
    
    		</span></h2>
          </div>
          <div style="width:300px;float:right;padding:20px;">
          <?php get_search_form() ?>
          <div style="height:10px;"></div>
    <?php
    
      $categories = get_categories('taxonomy=sponsors_categories');
    
      $select = "<select name='cat' id='cat' class='postform'>n";
      $select.= "<option value='-1'>Select category</option>n";
    
      foreach($categories as $category){
    	if($category->count > 0){
    		$select.= "<option value='".$category->slug."'>".$category->name."</option>";
    	}
      }
    
      $select.= "</select>";
    
      echo $select;
    ?>

    which handles my dropdown select

    and this is how I am queing up the categories to be displayed if is the main archive page:

    <?php while ( have_posts() ) : the_post(); ?>
                     <!-- Display review title and author -->
                     <?php
    				 if ( is_post_type_archive('sponsor_post_types') ) {
    
    				  ?>
    				  <div><?php   $args = array( 'taxonomy' => 'sponsors_categories' );
    
    $terms = get_terms('sponsors_categories', $args);
    
    $count = count($terms);
    if ($count > 0) {
        $cape_list = '<p class="my_term-archive">';
        foreach ($terms as $term) {
            $i++;
        	$term_list .= '<a href="/sponsors_categories/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></p>';
    
        }
        echo $term_list;
    } ?></div>

    so what am I doing wrong and why is it listing the categories multiple times (duplicates)?

  3. Blindacme
    Member
    Posted 1 year ago #

    I got it worked out, I ended up running the loop within my else statement and it worked out.

Topic Closed

This topic has been closed to new replies.

About this Topic