Skip to content

Instantly share code, notes, and snippets.

@kirandash
Last active May 2, 2017 11:14
Show Gist options
  • Save kirandash/e8311f95abbb5b43fd178ea752b3485f to your computer and use it in GitHub Desktop.
Save kirandash/e8311f95abbb5b43fd178ea752b3485f to your computer and use it in GitHub Desktop.
Accordion Shortcode
<?php
/**
* Accordion Shortcode
*/
// Extended subscription function with subscription type variable
function accordion_shortcode($atts) {
$atts = shortcode_atts(
array(
'id' => ''
), $atts
);
// Generating Output
extract($atts);
$count = 0;
$rand = rand();
$accordion .= '<div class="panel-group" id="ctrc-accordion'. $rand .'">';
while(have_rows('accordion_panels', $id)): the_row();
$accHead = get_sub_field('accordion_panel_title');
$accIcon = get_sub_field('accordion_panel_icon');
$accIconb = get_sub_field('accordion_panel_icon_blue');
$accIconp = get_sub_field('accordion_panel_icon_pink');
$accBody = get_sub_field('accordion_panel_content');
$sgnuplabel = get_sub_field('accordion_panel_button_label');
$sgnupurl = get_sub_field('accordion_panel_button_url');
$accopen = get_sub_field('accordion_open_panel');
if($sgnupurl): $sgnupbtn = '<div class="sign-up text-center"><a href="'. $sgnupurl .'" class="btn btn-primary btn-pink">'. $sgnuplabel .'</a></div>'; else: $sgnupbtn = ''; endif;
if($accopen): $accopenclass = 'in'; $collapseclass= ''; else: $accopenclass = ''; $collapseclass= 'collapsed'; endif;
$accIconImg = '';
$accIconbImg = '';
$accIconpImg = '';
if($accIcon != ''): $accIconImg = '<img src="'.$accIcon['url'].'" class="black">'; endif;
if($accIconb != ''): $accIconbImg = '<img src="'.$accIconb['url'].'" class="blue">'; endif;
if($accIconp != ''): $accIconpImg = '<img src="'.$accIconp['url'].'" class="pink">'; endif;
$details = strtolower(str_replace(' ', '-', $accHead)) . $rand;
$details = preg_replace('/[^A-Za-z0-9\-]/', '', $details);
$accordion .= '<div class="panel panel-default">';
$accordion .= '<div class="panel-heading"><h4 class="panel-title">';
$accordion .= '<a class="accordion-toggle '. $collapseclass .'" data-toggle="collapse" data-parent="#ctrc-accordion'. $rand .'" href="#'.$details.'-0-'.$count.'"><span>';
$accordion .= $accIconImg . $accIconbImg . $accIconpImg . $accHead . '</span></a></h4></div>';
$accordion .= '<div id="'.$details.'-0-'.$count.'" class="panel-collapse collapse '. $accopenclass .'">';
$accordion .= '<div class="panel-body">'.$accBody. $sgnupbtn . '</div></div></div>';
$count++;
endwhile;
$accordion .= '</div>';
return $accordion; //Return the HTML.
}
add_shortcode('accordion', 'accordion_shortcode');
/**
* Register an Accordion
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_Accordion_init() {
$labels = array(
'name' => _x( 'Accordions', 'post type general name', 'ctrctheme' ),
'singular_name' => _x( 'Accordion', 'post type singular name', 'ctrctheme' ),
'menu_name' => _x( 'Accordions', 'admin menu', 'ctrctheme' ),
'name_admin_bar' => _x( 'Accordion', 'add new on admin bar', 'ctrctheme' ),
'add_new' => _x( 'Add New', 'Accordion', 'ctrctheme' ),
'add_new_item' => __( 'Add New Accordion', 'ctrctheme' ),
'new_item' => __( 'New Accordion', 'ctrctheme' ),
'edit_item' => __( 'Edit Accordion', 'ctrctheme' ),
'view_item' => __( 'View Accordion', 'ctrctheme' ),
'all_items' => __( 'All Accordions', 'ctrctheme' ),
'search_items' => __( 'Search Accordions', 'ctrctheme' ),
'parent_item_colon' => __( 'Parent Accordions:', 'ctrctheme' ),
'not_found' => __( 'No Accordions found.', 'ctrctheme' ),
'not_found_in_trash' => __( 'No Accordions found in Trash.', 'ctrctheme' )
);
$args = array(
'labels' => $labels,
'menu_icon' => 'dashicons-star-filled',
'description' => __( 'Description.', 'ctrctheme' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'accordion' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title' )
);
register_post_type( 'accordion', $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment