<ul>
<li class="artist-website"></li>
<li class="artist-youtube"></li>
<li class="artist-twitter"></li>
</ul>
I want to add an if exists statement before each of the 'li' items. For example, if "artist-website" exists, then echo <li class="artist-website"></li>
How can I do make this work?
<ul>
<?php if(isset($artist_website){ ?> <li class="artist-website">...</li> <?php } ?>
...
</ul>
Or if(!empty($artist_website)), depending how your variables are setup.
As simple as:
<?php if (/* exists */) : ?>
<li class="artist-website"></li>
<?php endif; ?>
Ah, I understand what you're asking. Here is a link showing how to check if a website is available. The other 2 comments mainly showed you how to set up the if statement.
http://css-tricks.com/snippets/php/check-if-website-is-available/
EDIT: unless you are checking if a variable is set or not... then it would be:
if (isset($var)) {
// do this
}
Related
I'm trying to make my sub category looks different when it's selected, for that I've used sub_cat variable in my php.
So value of $sub_cat depends on the page that loads.
On the top I've this code:
if(!empty($_GET['sub_cat'])){
$sub_cat=$_GET['sub_cat'];
}
else{
$sub_cat=0;
}
Now what my code for displaying list.
<li <?php if($sub_cat==11) echo "style='color:#C00'"; ?> >Popular</li>
<li <?php if($sub_cat==12) echo "style='color:#C00'"; ?> >Exotic</li>
<li <?php if($sub_cat==13) echo "style='color:#C00'"; ?> >Fibrous Veg</li>
But it doesn't seem to make any difference, value of $sub_cat is coming fine as in 11,12,13 but this code is not working. Can anyone spot the minor mistake or major mistake?
How about transferring the style attribute to the a element instead of placing it in the li.
<li><a href="vegetables.php?sub_cat=11" <?php if($sub_cat==11) echo "style='color:#C00'"; ?>>Popular</a></li>
<li><a href="vegetables.php?sub_cat=12" <?php if($sub_cat==12) echo "style='color:#C00'"; ?>>Exotic</a></li>
<li><a href="vegetables.php?sub_cat=13" <?php if($sub_cat==13) echo "style='color:#C00'"; ?>>Fibrous Veg</a></li>
Try this:
if(isset($_GET['sub_cat'])){
$sub_cat=$_GET['sub_cat'];
}
else{
$sub_cat=0;
}
Using PHP to generate out a list.
I Simply want to set a different class on every fifth entry. (1/5), (4+1)
<li class="<?php the_code();?>"> content </li>
Output would be like this
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="hello"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="yolo"> Content </li>
<li class="hello"> Content </li>
How can i do this ?
Guess it's pure basic for anyone with php skills, that's not me but i would also be glad to get pointed towards some page that explains this so i can learn it.
To clarify am using Wordpress loop and it contains +100 items and i want to alter class on item 5,10,15,20,25,30,35 etc..
You can use a counter ($i) and increment it on each time you create a new list item (++$i), then check to see if the result is divisible by 5 (% 5 == 0). Then simply use the ternary operator (?…:…) to decide which value to output:
<?php $i = 0; ?>
<?php for(...) { ?>
<li class="<?= (++$i) % 5 == 0 ? 'yolo' : 'hello' ?>"> content </li>
<?php } ?>
But it truth, there's no need to even use PHP, when a little CSS will probably do the job:
li:nth-child(5n)
{
...
}
See a demonstation here.
As a drop in function for the code you provided:
function the_code() {
static $count = 0;
$count++;
echo $count % 5 == 0 ? 'hello':'yolo';
}
the static keyword in the function keeps the value of count whenever the function is called.
Can anyone suggest a way I can get my class="selected" to work on my navigation in Wordpress?
I have a wordpress navigation setup:
If I am on the home page, my home page class is selected.
If I want to go to page one, I want this class to change in thenavigation so only this class loads and not the home page.
I use: class="selected" to activate my roll over effect.
I can get this to work on a fixed site, just not on wordpress, any suggestions here?
<div class="nav">
<ul>
<li class="selected">Home</li>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
<li>Six</li>
</ul>
</div>
Could I use an if statement here?
<?php if(is_home() ) {
//for example load class="selected"
} else {
//for example if other page don't load class="selected"
} ?>
From what I understood, you're using a hardcoded navigation, and not wp_nav_menu() from WordPress.
So, you can conditionally check for each page you are using:
<div class="nav">
<ul>
<li<?php echo is_home()? ' class="selected"'; '';?>>
Home
</li>
<li<?php echo is_page('Contact')? ' class="selected"'; '';?>>
Contact page
</li>
<li<?php echo is_single('My first Post')? ' class="selected"'; '';?>>
Myfirst post
</li>
</ul>
</div>
You should make use of the Conditional Tags present in WordPress.
If you look at the first <li> you will see <?php echo is_home()? ' class="selected"'; '';?> This code is expressed as a ternary operator which is the equivalent of
if( is_home() ){
echo ' class="selected"'; #echo class. white space in front so that it does not stick to the "<li"
else{
echo ''; #do nothing
}
In the example above, I used three functions:
is_home() - Returns true if you are on the home page
is_page($arg) - Returns true if you are on the page specified by $arg.
is_single($arg) - Returns true if you are on the post specified by $arg.
There are other conditional tags available that you can choose to use.
I am using a wordpress plugin that requires the following code in my template:
<li><a href="#"><?php if (class_exists('MultiPostThumbnails')
&& MultiPostThumbnails::has_post_thumbnail('shanti', 'two-image')) : MultiPostThumbnails::the_post_thumbnail('shanti', 'two-image', NULL, 'big');
endif; ?></a></li>
Basically, it says "if the MultiPost Thumbnails class exists, then display the 'big' image." Im not too great with PHP, but I would like to include the <li><a href="#"> within the conditional statement. Reason is because if there is no image to spit out, I dont want a blank <li> to be displayed. Any idea how to rewrite this code to include the <li>/<a> in the conditional?
Thanks
Try advanced escaping:
<?php if (class_exists('MultiPostThumbnails')
&& MultiPostThumbnails::has_post_thumbnail('shanti', 'two-image')): ?>
<li><a href="#">
<?php MultiPostThumbnails::the_post_thumbnail('shanti', 'two-image', NULL, 'big'); ?>
</a></li>
<?php endif; ?>
Hi I am trying to have the navigation highlights determined by what category or page you are looking at using wordpress. Can someone tell me what is wrong with a statement like this:
<?php if (in_category('b')){ ?>
<ul>
<li>A</li>
<li><a class="current" href="#">B</li>
</ul>
<?php } else { ?>
<ul>
<li><a class="current" href="#">A</a></li>
<li><a href="#">B</li>
</ul>
<?php } ?>
I am trying to use something like this but my else statement is ignored and the 'b' is always current regardless of category.
You are either not inside a post or everything is in category 'b'.
See http://codex.wordpress.org/Function_Reference/in_category for in_category() information.
read here.
You've got to write something like:
<?php if (in_category('b')): ?>
<ul>
<li>A</li>
<li><a class="current" href="#">B</li>
</ul>
...