Jump to content

simply php if statement


Guest ldexterldesign

Recommended Posts

Guest ldexterldesign

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

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

 

Guest ldexterldesign

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  ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.