WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying Category-Specific Post Formatting (4 posts)

  1. criticalxnature
    Member
    Posted 1 year ago #

    I'm new to WordPress and what I'm looking to do is display posts differently depending on their category - including any subcategories beneath.

    I've seen the 'testing if a post is in a descendant category' function but I can't seem to get it to work how I want it to without getting a server error. Ideally, I would be looking to modify the category.php to something like this after get_header:

    <?php
    if ( in_category( 'fruit' ) || post_is_in_descendant_category( 11 ) ) {
    	include 'cat-fruit.php';
    } elseif ( in_category( 'vegetables' ) || post_is_in_descendant_category( 12 ) ) {
    	include 'cat-vegetables.php';
    } else {
    	// Continue with normal Loop
    	if ( have_posts() ) : while ( have_posts() ) : the_post();
    	// ...
    }
    ?>

    I realise the code is most likely wrong, but its only to try and show what I'm trying to do.

    Posts within 'fruit' and/or its subcategories would be displayed as stated in cat-fruit.php. Posts within 'vegetables' and/or its subcategories would be displayed as stated in cat-vegetables.php, and anything else would be displayed as the default.

    I'm using a child of Twenty Twelve, and have pasted the function from the codex into the child's functions.php.

    Any help would be much appreciated.

  2. WPyogi
    Volunteer Moderator
    Posted 1 year ago #

    Have you looked at using Custom Post Types - they might do what you want more easily - http://codex.wordpress.org/Post_Types

  3. criticalxnature
    Member
    Posted 1 year ago #

    I have, but to be honest I'm not that confident with WordPress yet and wouldn't know how to set it up.

  4. WPyogi
    Volunteer Moderator
    Posted 1 year ago #

    There're some good tutorials if you want to try it:

    http://wp.smashingmagazine.com/2012/11/08/complete-guide-custom-post-types/

    http://wp.tutsplus.com/tutorials/plugins/a-guide-to-wordpress-custom-post-types-creation-display-and-meta-boxes/

    I just think you'll have many more options...and they are pretty cool :).

Topic Closed

This topic has been closed to new replies.

About this Topic