Can someone help me with this ? Trying to make the home page show abc with H1 and abc with H2 on all other pages This is what i used : will appreciate help on why it doesnt work : )) <?php if ( is_home() ) { <h1><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1> } else { <h2><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h2> } ?> There's something wrong with my else statement, becos when i use only the IF, it works
Well, first off you probably shouldn't be opening and closing php so much (It's just sloppy programming), you aren't opening/closing the php properly in the first place, in general this is just 'wrong'. I'd store the desired level in a variable, then write it out flat in a single echo. <?php $hLevel=(is_home() ? '1' : '2'); echo ' <h',$hLevel,'> <a href="',get_option('home'),'">',bloginfo('name'),'</a> </h',$hLevel,'> '; ?> Code (markup): But then, I consider opening and closing php more than once in a file to be sloppy programming (and completely nonsensical) so... Probably comes from my three decades of programming other languages and understanding that for all the 'optimizers' at it's heart php is still an interpreted language - as such more code = slow, conditionals should be minimized, variable references are leaner than multiple conditionals, straight strings (single quote) are faster than parsed (double quote), etc, etc.
Hmmm.... to make the answer more apparent, I think you're problem is that you need: <?php echo bloginfo('name'); ?> instead of just: <?php bloginfo('name'); ?> If you had looked at the resulting HTML source itself, this may have been easier to pick up. I won't get into the rest of what deathshadow is talking about.
WOW ! Thanks for the effort : )) But i really can't understand this! eh .. mabbe you be able to convert what i just done into that? I managed to get what i wanted to do to work. Here's the coding i used <?php if(is_home() && !is_paged()) { ?> <h1><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1> <?php } else if(is_paged()){ ?> <h1><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h1> <?php } else { ?> <h2><a href="<?php echo get_option('home'); ?>/"><?php bloginfo('name'); ?></a></h2> <?php } ?>
Ok, for that version you need to think about your logic a bit - you've got more conditionals than you need. First, you are checking is_paged() twice - you only need to once, put that first. if (is_paged()) { // do h1 } else if (is_home()) { // we now KNOW it's not paged! // do h1 } else { // do h2 } From that re-arrangement though we see something else - we do h1 for two conditions, so we can simplify that down to an or. if (is_paged() || is_home()) { // do h1 } else { // do h2 } My version uses an expression to determine which header level to use, and dumps that into a variable. I then just plug in that value as appropriate. If you aren't familiar with expressions (also called inlined conditionals in some other languages) the format is very simple: boolean ? true : false You feed it a boolean expression, followed by a question mark. The first value is if it's true, the second is for if it's false, separated by a colon. (1==1) ? '1' : '0' - would always return 1 (1==2) ? '1' : '0' - would always return 0 Knowing that, and combining the logic simplification from above, we can simplify your latest example down to: <?php $hLevel=(is_paged() || is_home()) ? '1' : '2'; echo ' <h',$hLevel,'> <a href="',get_option('home'),'">',bloginfo('name'),'</a> </h',$hLevel,'> '; ?> Code (markup): You can delete the formatting inside the echo if you like - more personal preference as I like my php output to be formatted semi-normal. if you REALLY wanted to do it with an if statement, that would go something like: <?php if (is_paged() || is_home()) { echo '<h1><a href="',get_option('home'),'">',bloginfo('name'),'</a></h1>'; } else { echo '<h2><a href="',get_option('home'),'">',bloginfo('name'),'</a></h2>'; ?> Code (markup): A hell of a lot clearer without all those stupid <?php and ?> getting in the way.