Forums

[resolved] Help with in_category reference (3 posts)

  1. Food Blogger
    Member
    Posted 2 years ago #

    Does anyone know what php code I would use to perform an action such as:

    if the post category is XX then display post excerpt, if in any other category then display the full content?

    I've looked through the codex and it seems I can test the category in the loop ie:

    <?php
    if ( in_category( 'pachoderms' )) {
    	// They have long trunks...
    } elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
    	// They are warm-blooded...
    } else {
    	// & c.
    }
    ?>

    Would it be:

    <?php
    if ( in_category( 'pachoderms' )) {
    	<?php the_excerpt ?>
    } else {
    	<?php the_content ?>
    }
    ?>

    I'm not very good with syntax, so could someone look this over to see if that's how it would be done, please? Thanks!

  2. Edward Caissie
    Member
    Posted 2 years ago #

    I believe the "is_category()" function would be more correct for your code snippet above.

    Here is a link to more information: http://codex.wordpress.org/Function_Reference/is_category

  3. Food Blogger
    Member
    Posted 2 years ago #

    I figured it out:

    <?php
    if ( in_category( 'featured' )) {
    	echo (get_the_excerpt( $more_link_text, $stripteaser, $more_file ));
    }
    else {
    	echo (get_the_content( $more_link_text, $stripteaser, $more_file ));
    }
    ?>

    When placed inside the loop this will show the excerpt of all posts in the category of giveaways on the main page, and the full post content of any other category.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.