• I’m not positive if this is the forum but I want to place different CSS styling on page and post titles. I’m not approaching it correctly so far:

    <?php if (is_single()) {
    			echo '<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    			<h2><?php the_title(); ?></h2><br />&nbsp;<br />';
    			} else if(is_page()) {
    			echo '<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    			<h1><?php the_title(); ?></h1><br />&nbsp;<br />';
    			} ?>

    This is within my index.php. I suspect there may be different solutions. I’m a novice. Any suggestions or feedback on what I’m doing wrong? As you can see I just changed the H tag above for a page and for a post. Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi

    the code you have there is just fine.

    The main thing you are missing is, what do you want to display when what is being displayed is not single and not a page, like the posts page or a category page? right now those are falling into a black hole and producing no output.

    Thread Starter estevancarlos

    (@estevancarlos)

    Hm, I’ve got this now:

    <?php if (is_category('4')) {
    			echo '<h2><?php the_title(); ?></h2><br />&nbsp;<br />';
    			} elseif (is_page()) {
    			echo '<h1><?php the_title(); ?></h1><br />&nbsp;<br />';
    	 		} else {
    	 		echo '<h1><?php the_title(); ?></h1><br />&nbsp;<br />';
    			} ?>

    Should that work because it doesn’t. Titles now don’t display at all.

    This bit of code doesn’t seem to fit in that location either:

    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

    It results in, “post-” appearing on the page.

    Steve

    (@stevejohnson)

    <?php if (is_category('4')) {
    	echo '<h2>'; the_title(); echo '</h2>&nbsp;';
    } elseif (is_page()) {
    	echo '<h1>'; the_title(); echo '</h1>&nbsp;';
    } else {
    	echo '<h1>'; the_title(); echo '</h1>&nbsp;';
    } ?>

    Hi
    1) What you are now saying is “If its category do one thing otherwise do another thing, since what you are doing for pages and everything else is the same. Thus your logic can be slightly simplified..

    2) there were some mistakes in your code – I’ve corrected it here: – you had <?php the_title(); ?> within an active PHP block, in an echo statement, so what was happening is it was printing out your code, not executing it. I corrected that here…

    <?php
    if (is_category('4')) { ?>
       <h2><?php the_title(); ?></h2><br />&nbsp;<br />
    } else {
       <h1><?php the_title(); ?></h1><br />&nbsp;<br />
    } ?>

    you are placing this code WITHIN the WordPress loop, yes, as below?

    Also, to be clear, in most themes WordPress uses category.php, not index.php, to display category pages

    The loop starts with something like (depending on the theme)

    <?php if (have_posts()) : ?>
       <?php while (have_posts()) : the_post(); ?>
    
    <?php
    if (is_category('4')) { ?>
       <h2><?php the_title(); ?></h2><br />&nbsp;<br />
    } else {
       <h1><?php the_title(); ?></h1><br />&nbsp;<br />
    } ?>
    
    ....
    
    endwhile
    endif

    Thread Starter estevancarlos

    (@estevancarlos)

    Ah nevermind, refer to my comment below this one.

    ————————-

    I’m customizing WordPress Classic of which does not include the category.php file. Is that important?

    Either, so far your suggestions don’t work. Here’s my code (also, thanks again for all your help):

    <?php
    /**
     * @package WordPress
     * @subpackage Classic_Theme
     */
    get_header();
    ?>
    <div id="contentFrame">
    
    <?php get_sidebar(); ?>
    
    		<div id="contentWindow"></div>
    			<div id="words">
    			<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    			<?php /* the_date('','<strong>','</strong>'); */ ?>
    
    			<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    
    	 		<!-- ARTICLE TITLE -->
    
    	 		<?php if (is_category('4')) {
    			echo '<h2><?php the_title(); ?></h2>&nbsp;';
    			} elseif (is_page()) {
    			echo '<h1><?php the_title(); ?></h1>&nbsp;';
    	 		} else {
    	 		echo '<h1><?php the_title(); ?></h1>&nbsp;';
    			} ?>
    
    			<!-- h2><?php the_title(); ?></h2>&nbsp;<br / -->
    			<!-- END ARTICLE TITLE -->
    
    			<?php the_content(__('(more...)')); ?>
    			<div class="meta"><?php edit_post_link(__('Edit This')); ?></div>
    			<div class="feedback">
    			<?php wp_link_pages(); ?>
    			<?php /* comments_popup_link(__('Comments (0)'), __('Comments (1)'), __('Comments (%)')); */ ?>
    			</div>
    
    </div>
    
    <!-- ?php comments_template(); // Get wp-comments.php template ? -->
    <?php endwhile; else: ?><?php _e('Sorry, no posts matched your criteria.'); ?>
    <?php endif; ?>
    <?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
    
    			</div>
    
    		</div>
    
    	</div>
    
    <?php get_footer(); ?>
    Thread Starter estevancarlos

    (@estevancarlos)

    Oh wait. This mostly works, from Steve Johnson:

    <?php if (is_category('4')) {
    	echo '<h2>'; the_title(); echo '</h2>&nbsp;';
    } elseif (is_page()) {
    	echo '<h1>'; the_title(); echo '</h1>&nbsp;';
    } else {
    	echo '<h1>'; the_title(); echo '</h1>&nbsp;';
    } ?>

    Although it doesn’t recognize my category. Would this be related to the fact that I’m not using category.php?

    is the category ID code of the category you want this displayed on ACTUALLY “4” or did you copy that from someone else’s code?

    To find the cat ID, go to the category maint screen – point at the name of the category – look in the lower left corner at the browser’s status line – you will see a URL – the cat ID is at the end of that URL.

    use THAT cat ID in your code

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Different Titles for different conditions’ is closed to new replies.