Dynadot

WordPress If/Else if/Else Question...

Spaceship Spaceship
Watch
Impact
41
I want to use this code...

<?php include_once(ABSPATH . WPINC . '/rss.php');
wp_rss('http://www.mysite.com/blog/category-2/feed', 5); ?>

...inside of a PHP if/else statement so that it works like this:

IF /page-1
<?php include_once(ABSPATH . WPINC . '/rss.php');
wp_rss('http://www.mysite.com/blog/category-1/feed', 5); ?>

ELSE IF /page-2
<?php include_once(ABSPATH . WPINC . '/rss.php');
wp_rss('http://www.mysite.com/blog/category-2/feed', 5); ?>

ELSE IF /page-3
<?php include_once(ABSPATH . WPINC . '/rss.php');
wp_rss('http://www.mysite.com/blog/category-3/feed', 5); ?>

ELSE
Do nothing.


Can somebody please help me figure this out?

Thank you.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Really hoping for some help with this.

Thank you.
 
0
•••
Bumping in hope of a response.

Thank you.
 
0
•••
It needs to be if(){

} else if() {

} else if() {

}

You can skip the final do nothing bit.
But what is the page-1 bit - do you mean if the script is running on page 1?
 
0
•••
PHP:
<?php 

global $post;
$post_slug=$post->post_name;

if($post_slug=='page-1'){
    include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://www.mysite.com/blog/category-1/feed', 5);
}elseif($post_slug=='page-2'){
    include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://www.mysite.com/blog/category-2/feed', 5);
}elseif($post_slug=='page-3'){
    include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://www.mysite.com/blog/category-3/feed', 5);
}

?>
 
1
•••
Hi the helpful code from Tia should do it.

You can access pages by name or post id - but where are you running this code - in a page, a theme, a plugin, a widget?
 
0
•••
The if else statement in wordpress is the same as it is with C programming language.
 
0
•••
You will use
<?php if(is_page(2)){
echo "you text";
}
else if(is_page(3)){
echo "you text";
}
else {
echo "you text";
}
?>
 
0
•••
The most efficient (fast and lightweight) approach would be:

PHP:
<?php
switch($varYouExpectToContainTheseValues) {
  case '/page-1':
    include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://www.mysite.com/blog/category-1/feed', 5);
  break;
  case '/page-2':
    include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://www.mysite.com/blog/category-2/feed', 5);
  break;
  case '/page-3':
    include_once(ABSPATH . WPINC . '/rss.php');
    wp_rss('http://www.mysite.com/blog/category-3/feed', 5);
  break;
  default:
    // Do nothing. This (including the "default:" declaration on the line above) can be removed if you really need no default action.
}
?>

Untested, but should work, and at least you should get the idea.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back