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; ?>
Related
I would like to have an if/then argument for 2 classes (from the css) for a menu item. One where the menu item is blue if it is NOT the active page, and one where the menu item is red if it IS the active page. I have figured out the active page portion, now I am trying to figure out the if it is not active portion. I hope that makes sense. I have included a code snippit below.
<ul class="menu ul">
<li><a class="Blue <?php if($page =='home'){echo 'active';}?>" href="../index.php" >Home</a></li>
I have tried multiple variations, however I cannot figure it out. Thanks for your help!
Oleksandr is correct: It's better to have your links styles blue by default and overwrite it with the active class.
If you would want to give a hyperlink either one class or the other based on a simple condition, I would recommend this syntax:
<ul class="menu ul">
<li>
<a class="<?= $page == 'home' ? 'active' : 'Blue' ?>" href="../index.php" >Home</a>
</li>
</ul>
The example above uses the ternary operator and the echo shortcut syntax, and simply echoes one of two values based on the outcome of the condition.
as far as I understood you want to add different classes to link depending on $page variable.
for this i would recommend you to just use else statement
<ul class="menu ul">
<li><a class="<?php if($page =='home'){echo 'Blue';}else{echo 'Red';} ?>" href="../index.php" >Home</a></li>`
However it would be much better to check state of $page somewhere up (to not make spagetti code). And then echo only class in the a element.
<?php if($page =='home'){$menu_class='Blue';}else{$menu_class= 'Red';};?>
<ul class="menu ul">
<li><a class="<?php echo $menu_class; ?>" href="../index.php" >Home</a></li></pre>
I'm building a wordpress theme. In the backend, the user has the option to enter the url for their social networks (i.e. twitter, facebook, instagram etc). These URL's are then dynamically added to images in the theme front end linking to the respective networks.
The issue I have is that if a user doesn't enter a url for a network, I don't want the image to display. I am trying to write code that says, if the url is blank, echo 'class="hidden"' - this class has display:none in the css.
here is a snippet of my php:
<ul class="icons">
<li <?php if (get_theme_mod('footer_twitter')==0) {echo 'class="hidden"'; } ?>><span class="label">Twitter</span></li>
</ul>
and the css:
ul.icons li.hidden {
display:none;
}
The php code above currently outputs the echo statement for all cases, even when a url is entered in the backend. Can anyone help me with this code
Check the return of "get_theme_mod()" You can check this by using, cause i dont think it "== 0". http://codex.wordpress.org/Function_Reference/get_theme_mod
var_dump(get_theme_mod('footer_twitter'));
//string(0)
Here is your new code:
<ul class="icons">
<li class="<?php echo empty(get_theme_mod('footer_twitter')) ? 'hidden' : ''; ?>">
<a href="<?php echo get_theme_mod('footer_twitter'); ?> " class="icon circle fa-twitter">
<span class="label">Twitter</span>
</a>
</li>
</ul>
Please check this Syntax: http://php.net/manual/en/control-structures.alternative-syntax.php its the best way to code control structures in your "View" code.
<?php if (!empty (get_theme_mod( 'set_address2' ))) {
echo get_theme_mod( 'set_address2');
}?>
This seemed to work for me in Wordpress.
Let me know if this works.
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.
<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
}
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>
...