• Hi everyone. I’m learning more and more PHP everyday, and every time I figure something out, something else doesn’t work! Yay!

    Anyway, I am trying to display a welcome message on the front page (in my sidebar) and when on category pages, it is replaced by a listing of recent posts in that category. I got the category part working fine, but can’t figure out how to get rid of the Welcome text on category pages.

    Here’s my code:

    <?php if ( is_home() ); ?>
    <h2>Welcome</h2>
    <?php if ( is_category() ) { ?><h2>Recent Posts</h2>
    <?php
    foreach( ( get_the_category() ) as $category ) {
    $the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5');
    while ($the_query->have_posts()) : $the_query->the_post();
    ?>
    
        <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php } ?>
    <?php } else { ?><?php } ?>

    I’ve tried different ways of ending the first if tag (after the H2 tags), but none work.

    Also, will the Welcome text stay on single post pages?

    THANKS A LOT!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You have a syntax problem. This code:

    <?php if ( is_home() ); ?>
    <h2>Welcome</h2>

    in words would be: if is_home() do nothing, then output Welcome. The semicolon ends the if test. You could code it like this:

    <?php if ( is_home() ) { ?>
    <h2>Welcome</h2>
    <?php } ?>

    Also, if you have a static front page, is_home() will not work. Try this:
    <?php if ( is_front_page() ) echo '<h2>Welcome</h2>'; ?>

    Thread Starter acornrevolution

    (@acornrevolution)

    Thanks. It works perfect! Is there a way to also code for a catchall, for pages, single posts, other archive pages, etc, so that another set of text shows up? I tried the following but it didn’t work.

    <?php if else { ?>
    <h2>Test Text</h2>
    <?php } ?>

    Try this:

    <?php if ( is_home() ) {
       echo '<h2>Welcome</h2>';
    } else {
       echo '<h2>Test Text</h2>';
    } ?>

    And do what alchymyth said!

    Thread Starter acornrevolution

    (@acornrevolution)

    Thanks! I was trying to avoid echoing because the syntax is even more confusing to me. How would I echo in

    <?php
    foreach( ( get_the_category() ) as $category ) {
    $the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5');
    while ($the_query->have_posts()) : $the_query->the_post();
    ?>
    
        <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>

    into

    <?php if ( is_home() ) {
       echo '<h2>Welcome</h2>';
    } else {
       echo '<h2>Test Text</h2>';
    } ?>

    ?

    In your original post, you had these two lines at the top:

    <?php if ( is_home() ); ?>
    <h2>Welcome</h2>

    Just replace those two lines with:

    <?php if ( is_home() ) {
       echo '<h2>Welcome</h2>';
    } else {
       echo '<h2>Test Text</h2>';
    } ?>

    If that solves your problem, please mark this topic ‘Resolved’.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Conditionals help: is_home and is_category’ is closed to new replies.