|
20
Oct 2010
|
|
If { you have to Use WordPress Or } Else {
|
|
Posted By: Lee in Wordpress |
0 comments.
|
When I first starting using Wodpress, along with PHP I will admit I did some things wrong. Well not necessarily wrong… just different. Some of the techniques that I felt so clever coming up with I put to better use further down the line. Take for instance that you wanted to have a DIV be visible only if you are on the front page of the website. Well what I would originally do is put an if statement that would change the display of the DIV from block to none:
1
2
3
4
5
6
7
| <div style="<?php if (is_front_page() ) {
echo 'display:block';
} else {
echo 'display:none';
} ?>">
Show and Hide this content
</div> |
Little did I know that I didn’t require a div at all. All I had to do is self contain the PHP code and whatever is between the initial “IF” statement and “ELSE” and “ELSE” and close, will be the content that toggles. Take for example:
1
2
3
4
5
| <?php if (is_front_page() ) { ?>
This is font page content
<?php } else { ?>
This is NOT front page content
<?php } ?> |
Even though both of these work, I find the second bit to be a lot cleaner. Now back to my statement at the begining of this post. Even though the first example may not have been 100% accurate, I still was able to use it down the line. This techniques can be used to switch the class of a certain object depending on what page you are on. Say for example that you want a link to change colour if you are “home”. Well all you need to do is make a class with the colour you want and call it say “.active”. Now just throw it in with the link:
1
2
3
4
5
| <a class="<?php if (is_front_page() ) {
echo 'active';
} else {
echo ' ';
} ?>">This is a link</a> |
Now I could go on forever about the “IF” statment but I think I will leave it where we are. Maybe in a future post I will write about all of the different IF’s available in Worpress. Stay tuned
Leave a Comment