May 14, 2015
add_action('init', 'register_post_types'); function register_post_types() { $labels = array( 'name' => _x('Widgets', 'post type general name'), 'singular_name' => _x('Widget', 'post type singular name'), 'add_new' => _x('Add New Widget', 'Widget'), 'add_new_item' => __('Add New Widget'), 'edit_item' => __('Edit Widget'), 'new_item' => __('New Widget'), 'all_items' => __('All Widgets'), 'view_item' => __('View Widget'), 'search_items' => __('Search Widgets'), 'not_found' => __('No Widgets found'), 'not_found_in_trash' => __('No Widgets found in Trash'), 'parent_item_colon' => '', 'menu_name' => __('Widgets') ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array('slug'=>'widgets'), 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields', 'revisions' ), 'public' => true, ); register_post_type('widgets',$args); } add_action('add_meta_boxes','build_sidebar_meta_box'); function build_sidebar_meta_box() { add_meta_box( 'page_select_box', 'Shown on these pages', 'page_select_box', 'widgets', 'side', 'default' ); } function page_select_box($post) { $all_pages = get_pages(); $selected_pages = explode(',',get_post_meta($post->ID,'page_display',true)); $show_in_dropdown = get_post_meta($post->ID,'show_in_dropdown',true); $all_pages_checked = ''; if($selected_pages[0] == 'all_pages') { $all_pages_checked = ' checked="checked" '; } ?> <script type="text/javascript"> var lastChecked = null; jQuery(document).ready(function() { var jQuerychkboxes = jQuery('.display_page_checkbox'); jQuerychkboxes.click(function(event) { if(!lastChecked) { lastChecked = this; return; } if(event.shiftKey) { var start = jQuerychkboxes.index(this); var end = jQuerychkboxes.index(lastChecked); jQuerychkboxes.slice(Math.min(start,end), Math.max(start,end)+ 1).attr('checked', lastChecked.checked); } lastChecked = this; }); }); </script> <br /> <input type="checkbox" value="show_on_all_pages" name="show_on_all_pages" <?php echo $all_pages_checked; ?>/><label for="show_on_all_pages"> Show on all pages.</label> <?php wp_nonce_field('sidebar_widget_nonce','sidebar_widget_nonce'); ?> <p>Visible on the following pages:</p> <ul style="background:white; border:1px solid #ddd; padding:5px;max-height:400px;overflow:auto"> <?php foreach($all_pages as $p){ $checked = ''; if(in_array($p->ID,$selected_pages)) { $checked = ' checked="checked" '; } echo '<li><input type="checkbox" name="sidebar_display_page[]" '.$checked.' value="'.$p->ID.'" class="display_page_checkbox"/> <label for="'.$p->post_title.'">'.$p->post_title.'</label></li>'; } ?> </ul> <p> Shift + Click check boxes to select all in between checkboxes. </p> <?php } add_action( 'save_post', 'save_sub_page_widget' ); /* When the post is saved, saves our custom data */ function save_sub_page_widget( $post_id ) { // verify if this is an auto save routine. // If it is our form has not been submitted, so we dont want to do anything if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; // verify this came from the our screen and with proper authorization, // because save_post can be triggered at other times if ( !wp_verify_nonce( $_POST['sidebar_widget_nonce'], 'sidebar_widget_nonce' ) ) return; // Check permissions if ( 'page' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_page', $post_id ) ) return; } else { if ( !current_user_can( 'edit_post', $post_id ) ) return; } // OK, we're authenticated: we need to find and save the data $mydata = $_REQUEST['sidebar_display_page']; // Do something with $mydata while (list ($key_check,$val_check) = @each ($mydata)){ $value_check .= $val_check.","; } $all_value_check = substr($value_check, 0, -1); $page_display = $all_value_check; if($_POST['show_on_all_pages']){ $page_display = 'all_pages'; } // save to meta field update_post_meta($post_id,'page_display',$page_display); } function display_sub_page_widgets($get_count = false){ $args = array( 'post_type' => 'widgets','numberposts'=>-1 ); $sub_page_widgets = get_posts( $args ); global $post; $temp_post = $post; $widget_count = 0; $current_page_id = get_the_ID(); foreach($sub_page_widgets as $post){ setup_postdata($post); $selected_pages = explode(',',get_post_meta(get_the_ID(),'page_display',true)); $bypass = false; if($selected_pages[0] == 'all_pages'){ $bypass = true; } if(in_array($current_page_id,$selected_pages) || $bypass == true){ $link = get_post_meta(get_the_ID(),'widget_link',true); $video_box = get_post_meta(get_the_ID(),'video_box',true); $widget_img = get_post_meta(get_the_ID(),'widget_img',true); echo '<div class="sub_page_widget '.$widget_img.'">'; echo '<a href="'.$link.'" class="'.$video_box.'">'; the_post_thumbnail(); echo '</a>'; echo '<h3><a href="'.$link.'" class="'.$video_box.'">'.get_the_title().'</a></h3>'; /* the_content(); */ /* } */ echo '</div>'; } $widget_count ++; } $post = $temp_post; } function has_sub_page_widgets(){ $args = array('post_type'=>'widgets'); $all_widgets = get_posts($args); $has_widgets = false; global $post; $current_page_id = $post->ID; foreach($all_widgets as $widget){ $selected_pages = explode(',',get_post_meta($widget->ID,'page_display',true)); $bypass = false; if($selected_pages[0] == 'all_pages'){ $bypass = true; } if(in_array($current_page_id,$selected_pages) || $bypass == true){ $has_widgets = true; } } return $has_widgets; }
Then to display the content:
<?php display_sub_page_widgets(false,true,4); ?>