Jump to content

PHP if exceptions?


risingtalenttv

Recommended Posts

Hi, I have a simple if elseif statement set up right now to change one of my sidebar items based on the presence of posts filed under a certain array of tags, but I dont want it to change the item on the homepage (which displays the two most recent posts...) So, I am wondering if there is any kind of exception rule I can give it so it knows not to apply on the front page? ...or is there a better way to change the sidebar item based on categories/tags? I use Wordpress CMS btw.

Link to comment
Share on other sites

If you can set up a variable upon entering the home page, you can check for the presence of that variable.If you can't do that, you can simply see if $_SERVER['REQUEST_URI'] is the same as the one on your home page. I'm guessing that would mean "/", but if your "homepage" is actually at, say, "example.com/blog/", the home request URI will be "/blog".

Link to comment
Share on other sites

If you can set up a variable upon entering the home page, you can check for the presence of that variable.If you can't do that, you can simply see if $_SERVER['REQUEST_URI'] is the same as the one on your home page. I'm guessing that would mean "/", but if your "homepage" is actually at, say, "example.com/blog/", the home request URI will be "/blog".
I dont quite follow you... I understand the idea behind what you mean. By associating a variable to the homepage I could use it in the current elseif statement, but what is the best way to go about doing that. I would consider myself a beginner - intermediate coder... so please dumb it down for me :) Thanks.
Link to comment
Share on other sites

You can chain conditions in a single if() by using the "&&" (logical "and") operator. So, for example, in your home page, you may have, say:

$disableSidebar = true;

and in your actual condition for showing the sidebar, you can have

if ( (!isset($disableSidebar) || $disableSidebar !== true) && whatever_the_other_condition_is) {
Which says that if the variable is not present, or has a value different than true, proceed with the other check, and if all is true, display the sidebar.It's hard for me to be any more specific without any actual code.
Link to comment
Share on other sites

Ok boen I gotcha now, but I still don't see how this can be used as an efficient solution because I can use that to tell it to display on the homepage (containing that variable) but how can I tell it to ONLY enable when the variable is NOT present? The only way I see that working is if I applied the variable to every page other than the homepage. Maybe im missing something.Here is the example of the code I'm using if this helps.Code for displaying side box 1:

<?php /* If this is a category archive */ if ( is_front_page() ) { 						} elseif ( is_page() ) {						} elseif ( is_404() ) {						} elseif ( is_tag(array(39,40,42,46,47,48,49,50,51,52,53,54,55,56,57)) ) {						} elseif ( in_category(array(1,16,18,44,45)) ){ ?>

Code for displaying side box 2:

<?php if ( is_tag(array(25,26,27,28,29,30,31,32,33,34,35,36,37,38,43)) ) {						} elseif ( in_category('17') ){ ?>

Whats displaying now: Either Box 1 or Box 2 are displaying on their respective pages, but Box 2 is displaying (because post 2 fits its criteria) on the homepage/index where I would like to see Box 1 permanently.

Link to comment
Share on other sites

I'm not quite sure what you're doing with the elseifs, but it sounds like you just need to learn to use the logical operators. You could replace this, for example:

<?php /* If this is a category archive */ if ( is_front_page() ) { 						} elseif ( is_page() ) {						} elseif ( is_404() ) {						} elseif ( is_tag(array(39,40,42,46,47,48,49,50,51,52,53,54,55,56,57)) ) {						} elseif ( in_category(array(1,16,18,44,45)) ){

with this:

if (!is_front_page() && !is_page() && !is_404() && !is_tag(array(39,40,42,46,47,48,49,50,51,52,53,54,55,56,57)) && in_category(array(1,16,18,44,45))){}

Link to comment
Share on other sites

YES! I finally found and learned what I was looking for! LOGICAL OPERATORS FTW! I knew there had to be a way to do like "and" "or" and "not"...I took all that overly complicated code and condensed it to:

<?php /* Display Top Rated Articles */ if (is_front_page() || !in_category('17')) { ?>

<?php /* Display Critiques and Reviews */ if (in_category('17') && !is_front_page()) { ?>

LOL. It works perfectly now. Thanks guys!

Link to comment
Share on other sites

You can actually just write and, or, etc. - in PHP they are synonyms with the symbolic representations.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...