WordPress Shortcode - Change Layout Based On Attribute -


i have following custom shortcode displays custom post type of staff members in defined taxonomy of "type". i'm trying add second attribute allow user define layout style of output, example columns or rows. here envision shortcode looking like:

[staff type="international" style="rows"] 

i think have coding getting attributes, including setting default values, can't seem figure out how if statement in there alter output. seems should go right before first "$output .=" line.

function get_staff($atts) {     extract( shortcode_atts( array(          'type' => 'international',          'style' => 'rows'         ), $atts ) );      add_filter( 'posts_orderby' , 'posts_orderby_lastname' );      $loop = new wp_query(         array (             'post_type' => 'staff',             'orderby' => 'title',             'staff-type' => $type,             'style' => $style         )     );      remove_filter( 'posts_orderby' , 'posts_orderby_lastname' );      if ($loop->have_posts()) {         $output = '<div class="staff">';          while($loop->have_posts()){             $loop->the_post();             $meta = get_post_meta(get_the_id());             // attributes array featured image below             $attr = array(                                 'title' => get_the_title(),                                 'alt' => get_the_title(),                                 'class' => 'img_frame'                             );             $output .= '                 <div class="row-fluid" style="border-bottom: 1px solid #eee; margin-bottom: 16px; padding-bottom: 16px;">                 <div class="span3">' . get_the_post_thumbnail($post->id, 'small', $attr) . '</div>                 <div class="span9"><h3>' . get_the_title()  . '</h3>                 ' . get_the_content() . '             </div></div>         ';     }     $output .= "</div>"; } else {     $output = 'no staff meet criteria yet.'; }  return $output; };  // create last name sort staff custom post type function posts_orderby_lastname ($orderby_statement)  {   $orderby_statement = "right(post_title, locate(' ', reverse(post_title)) - 1) asc";     return $orderby_statement; }   add_shortcode('staff', 'get_staff');  

there few ways, depending on coding style. can check style param before starting loop on posts. way after starting loop. sure, there's code duplication, it's easier read , you'll have control need on exact markup want generate.

$output = '<div class="staff">';  if ($loop->have_posts()) {      if($style == 'rows'){ // rows         $output .= '<div class="layout-rows">';         while( $loop->have_posts() ){             // ...         }         $output .= "</div>";     } elseif($style == 'columns') { // columns         $output .= '<div class="layout-columns">';         while( $loop->have_posts() ){             // ...         }         $output .= "</div>";     } else { // else         $output .= '<div class="layout-default">';         while( $loop->have_posts() ){             // ...         }         $output .= "</div>";     }  } else {     $output = 'no staff meet criteria yet.'; }  $output .= "</div>"; 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -