Forums

[resolved] Problems with in_category() (3 posts)

  1. do77
    Member
    Posted 1 year ago #

    Hi,

    I want to style single posts of category 1 different than those of category 2. In the function reference I read that this can be achieved with in_category() so I added the tag to my single.php file within the loop. However, now it says I have an unexpected error '<' on line 36.

    Here is my code:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php if ( in_category( 'news' )) {
    
    <h1><?php the_title(); ?></h1>
    <div class="single"><?php the_content(__('Read more', ''));?></div>
    
    } else {
    
    <h1><?php the_title(); ?></h1>
    <div>A lot of code including custom fields and an if statement if a user is logged in etc. </div>
    
    }
    ?>

    Any idea what I am doing wrong? Would appreciate your help!

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    make sure to close and open your php tags anytime the code switches to and from html code:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php if ( in_category( 'news' )) { ?>
    
    <h1><?php the_title(); ?></h1>
    <div class="single"><?php the_content(__('Read more', ''));?></div>
    
    <?php } else { ?>
    
    <h1><?php the_title(); ?></h1>
    <div>A lot of code including custom fields and an if statement if a user is logged in etc. </div>
    
    <?php }
    ?>
  3. do77
    Member
    Posted 1 year ago #

    That was it! Thank you so much alchymyth!

Topic Closed

This topic has been closed to new replies.

About this Topic