Trying to control my custom post type
-
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(); ?>
- The topic ‘Trying to control my custom post type’ is closed to new replies.