WordPress.org

Ready to get started?Download WordPress

Forums

Trying to control my custom post type (2 posts)

  1. Blindacme
    Member
    Posted 1 year ago #

    Hey everyone, I have worked with a tutorial found on wptuts to develop this custom post type plugin. Everything is working and looking great except that I am trying to customize the parent categories archive page. I have created a parent category and then child categories in my CPT. Every item I add gets assigned to the main (parent)category and a sub-category.

    So what I am trying to do is on the front end when someone is looking at the parent category archive page I am trying to have it display all the categories nested in that parent category. Then when someone clicks on one of those sub-cats it shows them all posts in that category. I hope that makes sense.

    Here is what I have in my archive page template. I can either get it to work one way or another but cant get it to work like I described above.

    <?php
    /*
    Template Name: Archives for CPT
    
    */
    ?>
    <?php get_header(); ?>
          <div id="pagename" style="width:200px;float:left;">
            <h2><span>
    
    		<?php if (have_posts()) : ?>
    
    		Exhibitors
    
    		</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>";
    		$item= "<a href='".$category->slug."'>".$category->name."</a>";
    	}
      }
    
      $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
    				 if ( is_post_type_archive('sponsor_post_types') ) {
    
    				  ?>
    				  <?php
    					  $args=array(
    						'orderby' => 'name',
    						'order' => 'ASC',
    						'taxonomy' => 'sponsors_categories'
    						);
    					  $categories=get_categories($args);
    						foreach($categories as $category) {
    						  echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    							}
    					  ?>
    				  <?php
    			  } else {
    				  //wp_reset_query();
    				 ?>
                     <?php while ( have_posts() ) : the_post(); ?>
    
                    <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 endwhile; ?>
                  <?php } ?>
    
    <!-- Display page navigation -->
    
    <?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 #

    anyone able to help me out here? I dont think it would be a difficult task to have a conditional saying if is_parent_category then show this, else if is_child_category then show this. Both are custom post types and I just need a way to change what is displayed if it is the parent category, otherwise run the normal loop

Topic Closed

This topic has been closed to new replies.

About this Topic