Jump to content

Recommended Posts

I am trying to do switch where it changes the name of the post title. I just can't seem to get the format correct. I tired the variable $P also and it doesn't work.

function corepages() {
$pages = get_pages();
global $blog_id;
foreach ($pages as $p) {
if ( file_exists( compat_get_plugin_dir( 'dir' ) . '/icons/' . $p['icon'] ) ) {
$image = compat_get_plugin_url( 'dir' ) . '/icons/' . $p['icon'];
} else {
$image = compat_get_upload_url() . '/images/' . $p['icon'];
}

if ($p['post_title'] !== "") :
{
echo('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . __($p['post_title']) . '</a></li>'); }
else :
switch($p['post_title']){
case 'Products Pages':
$p = "Our Products";
break;

case 'Contact Our Company':
$p = "Contact Us";
break;
}

endif;
}
}

Link to comment
https://forums.phpfreaks.com/topic/271866-help-with-switch-statement/
Share on other sites

Are you getting an error? What exactly is going wrong with the switch? Also, amoungst your cases, try adding a default case so you know if none of your cases are being matched.

 

<?php
switch($p['post_title']){

default:
$p = "Default Title";
break;

case 'Products Pages':
$p = "Our Products";
break;

Edited by Stooney

I got it to work but Ihave to hard code each page. I was trying to get something more dynamic to switch the name.

function corepages() {
$pages = get_pages();
global $blog_id;
foreach ($pages as $p) {
 if ( file_exists( compat_get_plugin_dir( 'dir' ) . '/icons/' . $p['icon'] ) ) {
  $image = compat_get_plugin_url( 'dir' ) . '/icons/' . $p['icon'];
 } else {
 $image = compat_get_upload_url() . '/images/' . $p['icon'];
}

if ($p['post_title'] == "Products Pages") :
{

 echo ('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . 'Our Products' . '</a></li>'); }
 else :
  switch($p['post_title']){
  case 'Contact Our Company':
  echo ('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . 'Contact Us' . '</a></li>');
  break;
  }
  case 'Our Locations':
  echo ('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . 'Locations' . '</a></li>');
  break;
 endif;
}
}

What you did in your first posted code would never work as you've placed your switch() within the else statement, which is conditional on $p['post_title'] not having a value.  So, $p['post_title'] being empty, your switch() will not work (as expected):

 

if ($p['post_title'] !== "") :
   echo('<li><a href="' . get_permalink($p['ID']) . '"><img src="' . $image . '" alt="icon" />' . __($p['post_title']) . '</a></li>');
else :
   switch($p['post_title']) { //nothing to "switch" as $p['post_title'] does not contain a value; use "default" to handle issues within a switch()
       case 'Products Pages':
       $p = "Our Products";
       break;

       case 'Contact Our Company':
       $p = "Contact Us";
       break;

       default: 'This is the default title';
       $p = 'Some page';
       break;
   }
endif;

 

Edit: And I noticed you're using a triple comparison operator:

 

if ($p['post_title'] !== "") :

 

I will assume you know what it does over a double comparison operator, however, if not, please see the following link to get a better understanding: http://php.net/manual/en/language.operators.comparison.php

 

Edit #2: switch() is no different that 'if/else if' in logic.  It is arguably a more readable structure, but that just boils down to personal preference.  So don't go out of your way to get a switch() statement within your code just for the sake of it.

 

Within a switch, the case is your if/else if, and default is your final else statement/condition.  It's all the same, really.

Edited by mrMarcus

hanks for the info. I will leave the above as it is. I now realize the similarity of if/else and a switch.

How about this example for a another part of my site. Same type of issue. I am printing a page title and I need to change the name dynamically. All I get is the page name its not changing based on my logic.

 

 

$title = the_title();

if ($title == 'Contact Info'):
{
echo ("Contact Us");

}
else:
switch($title){
case 'Products Pages':
echo ("Our Products");
break;

case 'Find An Agent':
echo ("Locate an Agent");
break;
}

endif;

Edited by mallen
All I get is the page name its not changing based on my logic.

 

I don't understand what you're getting at here.

 

What is your expected output?  Have you echo'd $title to ensure it holds an expected value?

 

You're are needlessly combining if/else with switch.

 

$title = the_title();

switch ($title) {
   case 'Contact Info':
       echo 'Contact Us';
       break;
   case 'Products Pages':
       echo 'Our Products';
       break;
   case 'Find An Agent':
       echo 'Locate an Agent';
       break;
   default:
       echo '$title not set';
       // header('Location: /'); // possible redirect if $title not set/is empty
       //exit(0);
       break;
}

Edited by mrMarcus
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.