Guest ldexterldesign Posted April 25, 2008 Share Posted April 25, 2008 hi guys, fairly new to php, and it seems i've forgotten most of what i've learnt in the past sadly. looking to perk up a bit. i'll get straight to the point anyway. i'm working with wordpress and have a post category variable i need to detect the value of. depending on the value i need to generate a little bit of text to make up a class name to give the desired category styles. what i'm working with: <div class="post"> <h5 class="category">Thrown in the<?php the_category(', ') ?>scrapheap</h5> what i'm trying out, but isn't working: <div class="post<?php if (the_category == "design") {print "-design";}?>" id="post-<?php the_ID(); ?>"> <h5 class="category">Thrown in the<?php the_category(', ') ?>scrapheap</h5> can anybody point me in the right direction. i'm assuming maybe i just have some weary syntax somewhere? but not quite sure why the_category can get away without being $the_category atm? :S many thanks, lewis ps. the categories i want to detect for are 'art', 'design', 'development' and the classes i want to generate/complete accordingly are class="post-art", class="post-design", class="post-development" - hopefully that helps. Link to comment https://forums.phpfreaks.com/topic/102841-simply-php-if-statement/ Share on other sites More sharing options...
triskelion69 Posted April 25, 2008 Share Posted April 25, 2008 Hi Lewis, Try this: <?php switch($the_category){ case "art": $divClass="post-art"; break; case "design": $divClass="post-design"; break; case "development": $divClass="post-development"; break; default: $divClass="******A CLASS YOU KNOW EXISTS******"; }?> <div class="<?=$divClass; ?>" id="post-<?=$the_ID(); ?>"> <h5 class="category">Thrown in the <?=$the_category; ?> scrapheap</h5> I hope this is what you're after, Triskelion Link to comment https://forums.phpfreaks.com/topic/102841-simply-php-if-statement/#findComment-526860 Share on other sites More sharing options...
Guest ldexterldesign Posted April 25, 2008 Share Posted April 25, 2008 hi mate, thanks for the response. no luck unfortunately, although i'm sure we're getting somewhere now. i've made a few discoveries. 1) <?= isn't working. i'm simply getting more success from using <?php, so i'm sticking with that - is there a difference? 2) by adding an echo here i can get 'dog' class output. without the echo i can't, so i've left it in: <?php switch($the_category){ case "art": $divClass="post-art"; break; case "design": $divClass="post-design"; break; case "development": $divClass="post-development"; break; default: $divClass="dog"; }?> <div class="<?php echo $divClass; ?>" id="post-<?php the_ID(); ?>"> now i can get a class value out the other end i've looked at why i can't get the case values to activate. firstly are the case string values case sensitive? the category technically is 'Development' (with a capital 'D'). either way i've tried using 'Development' and 'development' as case values - no luck. so this suggests, for me, the switch statement isn't taking in the proper variable is the first place; '$the_category' in the example. personally i think '$the_category' would be a list or array variable, after all 'the_category(', ')' in the xhtml output script is outputting one (or a list) of categories i could categorize a post under. this is confirmed when i publish a post under more than one category - it outputs more that one value i.e. art, development if i choose these two categories. so i think i'm maybe looking to access the first category, or list item/array object in '$the_category' - i've tried using $the_category(0) and $the_category[0], but no luck. if you think of anything else please shunt it my way. this really can't be that hard to solve lewis Link to comment https://forums.phpfreaks.com/topic/102841-simply-php-if-statement/#findComment-526887 Share on other sites More sharing options...
TEENFRONT Posted April 25, 2008 Share Posted April 25, 2008 <? echo $var; ?> would be the correct way to output as far as im aware i think <?= $var; ?> was depreciated. Link to comment https://forums.phpfreaks.com/topic/102841-simply-php-if-statement/#findComment-526949 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2008 Share Posted April 25, 2008 The shortcut for echo "<?=" only works when short tags are enabled. use <?php echo 'xxxx' ?> when you want to echo something. Ken Link to comment https://forums.phpfreaks.com/topic/102841-simply-php-if-statement/#findComment-526956 Share on other sites More sharing options...
Guest ldexterldesign Posted April 25, 2008 Share Posted April 25, 2008 yea, cheers guys. i kinda predicted it could be shorthand for <?php echo. i think i need to get to grips with the basics before i revel in shortcuts any thoughts on the problem at hand though? best, lewis Link to comment https://forums.phpfreaks.com/topic/102841-simply-php-if-statement/#findComment-526960 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.