Please recommend a smart, elegant way to write these repetitive 'if' statements
I just know I'm doing this in a really bad way:
<?php } elseif ( is_single()) {
$url = wp_get_referer();
$path_parts = pathinfo($url);
$mycat = $path_parts['filename'];
if ( $mycat == "animation" ) {
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "direction"){
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "grading"){
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "online"){
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "showcase"){
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "showreel"){
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "vfx"){
$_SESSION["theCategory"] = $mycat;?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php }else{ ?><a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a><?php };?>
<?php } //end if is_single ?>
It works. But it's messy and repetitive. I tried going this route:
<?php if ($开发者_如何转开发mycat == "animation" || "direction" || "grading"){
But it didn't work at all.
Create an array holding the values, then test if the value of $mycat
exists in the array. Also, you're echoing the link whatever $mycat
is, so the else statement can be completely dropped and the if structure further simplified to just the following:
$categories = array(
'animation',
'direction',
'grading',
'online',
'showcase',
'showreel',
'vfx'
);
if (in_array($mycat, $categories)) {
$_SESSION["theCategory"] = $mycat;
}
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
And like others have said, remember to escape your output.
Something like this?
<?php
$cats = array(
'animation',
'direction',
'grading',
'online',
'showcase',
'showreel'
'vfx',
);
if ( in_array($mycat, $cats) ) :
$_SESSION['theCategory'] = $mycat; ?>
<a href="<?php bloginfo('home')?>/category/<?php echo $mycat;?>"><?php echo $mycat;?></a>
<?php else: ?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php endif; ?>
And using the switch
statement:
switch($mycat) {
case 'animation':
case 'direction':
case 'grading':
case 'online':
case 'showcase':
case 'showreel':
case 'vfx':
$_SESSION["theCategory"] = $mycat;
default:
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php
}
Note the fall-through after the case statements. You could either do that, or:
switch($mycat) {
case 'animation':
case 'direction':
case 'grading':
case 'online':
case 'showcase':
case 'showreel':
case 'vfx':
$_SESSION["theCategory"] = $mycat;
}
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php
I don't understand why you even need a conditional here. You're not changing anything inside each conditional possibility.
$_SESSION["theCategory"] = $mycat;
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
Use the in_array() function to write you condition:
<?php if (in_array($mycat, array("animation", "direction", "grading")))
Path 1:
<?php if ($mycat == "animation" || $mycat == "direction" || $mycat == "grading"){
Path 2:
<?php if (in_array($mycat,array("animation","direction","grading"))) {
精彩评论