WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Help with Conditional tags and the loop

[Resolved] Help with Conditional tags and the loop

  • So I am trying to set up a conditional tag that will change a class name on a div that is nested inside my loop.

    Right now the way it is set up, my posts only display on two pages, the homepage, and the page that actually has the post (comments, trackback, etc).

    What I am trying to achieve is possibly set up a conditional tag that says: “if home, class equals this, else class equals that”

    Here is what I currently have:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <?php if (in_category('include')) continue; ?> 
    
    <!-- trying to change the class name post -->
    <div class="post" id="post-<?php the_ID(); ?>">
    <!-- -->
    
    	 <h3 class="storytitle"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    	<div class="meta">   <?php the_time('l, F j, Y') ?>
    	<div class="feedback">
    		<img src="<?php bloginfo('stylesheet_directory'); ?>/images/comment.gif">
    		<?php wp_link_pages(); ?>
    		<?php comments_popup_link(__('Post Comment'), __('1 Comment'), __('% Comments')); ?>
    	</div>
    
    	 </div>
    
    	<div class="storycontent">
    		<?php the_content(__('(more...)')); ?>
    
    	</div>
    
    </div>

    (I have commented out the code I am trying to effect)
    Thanks,
    James Bruno

Viewing 5 replies - 1 through 5 (of 5 total)
  • not sure if this is quite right, but try it & take it from there

    change ‘different_class’ to whatever you want it to be

    ?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <div class=”<?php if (in_category(‘include’)) { echo ‘different_class’; } else { echo ‘post’; } ?>” id=”post-<?php the_ID(); ?>”>

    Thanks for the response, ill try this later on and post with results.

    Thanks again,
    JamesBruno

    even easier is to use the is_home(); function (or is_front_page() if you have a static front page setup).

    <?php
    if(is_home()){
    echo "home";}
    else{
    echo "post";}
    ?>

    Mestro42,

    This worked beautifully. I didnt realize that you could put the php right in the middle of the div like that. Thank you for the help.

    Also, thanks again to stvwlf for your help.

    -Anthony

    no problem 😉

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Help with Conditional tags and the loop’ is closed to new replies.
Skip to toolbar