WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Issue with getting switch/case statement to work properly (4 posts)

  1. Andrew Rowland
    Member
    Posted 11 months ago #

    I have created a custom page template to dynamically change what content is displayed in the "Gallery" based on a Custom Field set for each page.

    In this template I have written a Switch statement to change the value for "$gallery", which is being used in $args for the WP_Query function I am using to pull in the thumbnails, titles, links etc. for the Gallery.

    The functions work to display the content, but my Switch only works for the case I list first. If I change the case listed first it works properly for each case, just not for any other case.

    I am a PHP noob so I am sure I am missing something simple with the break/continue, I just can't figure out what it is.

    Example of "Gallery Page" that works: http://beta.3blindmiceusa.com/featured/

    Code for Switch:

    <span id="gallerymeta"><?php
    $gallery_category = print_r ($gallery_cats);
    switch ($gallery_category)
    {
    	case "137":
    	$gallery="shades";
    	break;
    	case "185":
    	$gallery="window-film-window-tinting";
    	break;
    	case "174":
    	$gallery="featured-window-coverings";
    	break;
    	case "148":
    	$gallery="shutters";
    	break;
    }
    ?>
    </span>
    
    <?php $args = array(
    	'post_type' => 'page',
    	'gallery' => $gallery,
    	'posts_per_page' => '-1',
    );
    $the_query = new WP_Query( $args ); ?>
  2. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    Where is the value for $gallery_cats (and hence $gallery_category) being set?

  3. Andrew Rowland
    Member
    Posted 11 months ago #

    $gallery_cats is a variable in an array and I have no clue how else to get the value out of there other than the print_r function, but I know there should be a better way for me to detect what "$gallery_cats" a page belongs to, and then use that to Switch.

    $gallery_cats = isset( $et_ptemplate_settings['et_ptemplate_gallerycats'] ) ? (array) $et_ptemplate_settings['et_ptemplate_gallerycats'] : array();
  4. Andrew Rowland
    Member
    Posted 11 months ago #

    I found the solution. I had to fix the way I was defining $gallery_category to read:

    <?php
    $gallery_category = $gallery_cats[0];
    switch ($gallery_category)
    {
    	case "137":
    	$gallery="shades";
    	break;
    	case "185":
    	$gallery="window-film-window-tinting";
    	break;
    	case "174":
    	$gallery="featured-window-coverings";
    	break;
    	case "148":
    	$gallery="shutters";
    	break;
    }
    ?>

Reply

You must log in to post.

About this Topic