WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Help with Conditional tags and the loop (6 posts)

  1. jamesbruno
    Member
    Posted 6 years ago #

    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

  2. stvwlf
    Member
    Posted 6 years ago #

    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(); ?>">

  3. jamesbruno
    Member
    Posted 6 years ago #

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

    Thanks again,
    JamesBruno

  4. maestro42
    Member
    Posted 6 years ago #

    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";}
    ?>
  5. jamesbruno
    Member
    Posted 6 years ago #

    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

  6. maestro42
    Member
    Posted 5 years ago #

    no problem ;)

Topic Closed

This topic has been closed to new replies.

About this Topic