0

I'm using the following code in my category.php file. I want it to show my custom post type 'Products' the thumbnail and an excerpt.

At the moment it's showing the post title, the thumbnail for all the posts. However it's only showing the excerpt for the first post.

<?php
/*
Template Name: Category
*/
?>
<?php get_header(''); ?>
                            <?php /* Products sidebar */
    if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('product_sidebar') ) : ?><?php endif; ?>
<div id="main">
<?php if ( function_exists('yoast_breadcrumb') ) {
    yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
    <div id="post">
            <div id="productlist">       
<?php

if (!category_has_children()) {?>
<?php foreach(get_the_category() as $category) {
$cat = $category->cat_ID; }?>
<?php query_posts('post_type=Product&cat=' .$cat .'&order=ASC'); ?>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><?php the_post_thumbnail(array(100,100)); ?>
            <?php the_excerpt(); ?></p>
                    <?php endwhile;?>
        <?php endif; ?> 
<?php get_template_part('loop', 'index');
} else {?></div><!-- /productlist -->
<div id="productcategories">
<?php 
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
    echo '<p>'. $categories_item->description . '</p>';
}
echo "</ul>";
}
?>

            </div>

    </div><!-- /post -->
</div><!-- /main -->    
<?php get_footer(); ?>

This is in my functions.php as well

//Display product categories
function category_has_children() {
global $wpdb;
$term = get_queried_object();
$category_children_check = $wpdb->get_results(" SELECT * FROM wp_term_taxonomy WHERE parent = '$term->term_id' ");
     if ($category_children_check) {
          return true;
     } else {
          return false;
     }
}

Does anyone know why?

What i'm trying to achieve: My main category page on my site is 'Products'. On this page it displays child categories of the parent category.

Going on to a child category page will either display more child categories, or if there isn't any, it's displays my custom post types 'Product'.

The code i'm using is saying. If the Category has children then display them else display Products.

Update: I've been experimenting and found something out. Using the following code and a different custom post type 'News', it displays the excerpt for all posts.

<?php query_posts(array('post_type'=>'News')); ?>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <p><?php the_time('j M. Y'); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php the_excerpt();?></p>
           <?php endwhile;?>
        <?php endif; ?> 

If i change 'News' to 'Product' the excerpt only displays for the first product created.

The only difference i can think of between these two custom post types is 'News' is a post type and 'Product' is a page type.

I don't know why this isn't working. Also i haven't written an excerpt for either of them, so the excerpt is automatic from Wordpress.

8
  • Can you provide a live link to the rendered output? Nov 3, 2011 at 16:12
  • Also: why are you modifying the category on a category.php template file? Nov 3, 2011 at 16:14
  • Here's a link. asgdatasolutions.co.uk/dev/ndr/0002/products/ticket-printers I've set my category.php file up so it displays my categories, then sub-categories if it has any. If it doesn't have any it shows my posts belonging to that category.
    – Corbula
    Nov 3, 2011 at 16:23
  • Can we see the entire category.php markup? Nov 3, 2011 at 16:27
  • I've updated the question with all of the code.
    – Corbula
    Nov 3, 2011 at 16:30

1 Answer 1

0

I suspect you're misusing query_posts(), which is always and only ever intended to modify the Primary Loop.

query_posts() is meant for altering the main loop. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered - this may or may not be the intended result.

If, as I assume, you're outputting it multiple times, then by definition, you're creating secondary Loops, for which you should use WP_Query or get_posts() instead.

But I'm somewhat confused by the flow of the code, due to lack of consistent indentation.

EDIT

Based on this comment:

I've set my category.php file up so it displays my categories, then sub-categories if it has any. If it doesn't have any it shows my posts belonging to that category.

Let's try to lay out logically what you want category.php to display.

Assumptions

  1. First, display Custom Post Types that are in the current category
  2. Second, additional loops are used to display Custom Post Types that are in child categories of the current category
  3. If Custom Post Types in neither the current category nor its child categories exist, display Blog Posts that are in the current category.
  4. Categorized or child-categorized Custom Post Types always display, if they exist
  5. Categorized Blog Posts only display if CPTs do not exist

All good so far?

5
  • The code i'm using i've got different parts of it from different places to achieve what i needed. So instead of query_posts i should use get_posts and try and do it that way?
    – Corbula
    Nov 3, 2011 at 16:53
  • Franken-code is always fraught with unintended outcomes. Let's try to lay out logically what you want to display in category.php. Nov 3, 2011 at 16:58
  • Close. I've updates my original question with what is it i'm trying to do. This is only meant to show categories and my custom post type that belongs to them.
    – Corbula
    Nov 3, 2011 at 17:13
  • Can anyone help me to try and achieve what i'm after?
    – Corbula
    Nov 4, 2011 at 20:42
  • I will accept this as an answer, however i couldn't get the_excerpt to work. I've had to use on of the custom fields and display that instead so it works.
    – Corbula
    Nov 6, 2011 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.