Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action("admin_init", "add_pop_meta_box");
- //Add the Meta Box only to our template page
- function add_pop_meta_box(){
- $post_id = isset( $_GET['post'] ) ? $_GET['post'] : 0 ;
- if($post_id) {
- $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
- if ($template_file == 'page-pop.php') {
- add_meta_box("pageofposts", "Page of Posts", "pop_meta_options", "page", "side", "core");
- } else {
- $meta = get_post_meta($post_id, '_cat_id', true);
- if( $meta ) {
- pop_update_post_meta($post_id, '_cat_id', '');
- pop_update_post_meta($post_id, '_page_title', '');
- pop_update_post_meta($post_id, '_posts_title', '');
- pop_update_post_meta($post_id, '_order_by', '');
- pop_update_post_meta($post_id, '_asc', '');
- pop_update_post_meta($post_id, '_post_count', '');
- pop_update_post_meta($post_id, '_list_style', '');
- pop_update_post_meta($post_id, '_days', '');
- remove_meta_box("pageofposts","page","side");
- }
- }
- }
- add_action('save_post', 'update_pop_meta');
- }
- //Build the select arrays!
- $order_list = array(
- 'none' => array( 'value' => 'none','label' => 'None' ),
- 'id' => array( 'value' => 'ID','label' => 'Post ID' ),
- 'author' => array( 'value' => 'author','label' => 'Author' ),
- 'title' => array( 'value' => 'title','label' => 'Post Title' ),
- 'date' => array( 'value' => 'date', 'label' => 'Post Date' ),
- 'modified' => array( 'value' => 'modified','label' => 'Modified Date' ),
- 'parent' => array( 'value' => 'parent','label' => 'Parent Post' ),
- 'rand' => array( 'value' => 'rand','label' => 'Random' ),
- 'comment_count' => array( 'value' => 'comment_count','label' => 'Comment Count' ),
- 'menu_order' => array( 'value' => 'menu_order','label' => 'Menu Order' ),
- );
- $post_styles = array(
- 'default' => array( 'value' => 'default','label' => 'Default' ),
- 'excerpt' => array( 'value' => 'excerpt','label' => 'Excerpts' ),
- 'title-only' => array( 'value' => 'title-only','label' => 'Titles Only' ),
- 'thumb-excerpt' => array( 'value' => 'thumb-excerpt','label' => 'Thumbnails and Excerpts' ),
- );
- $sort = array(
- 'DESC' => array( 'value' => 'DESC','label' => 'Descending' ),
- 'ASC' => array( 'value' => 'ASC','label' => 'Ascending' ),
- );
- function pop_meta_options(){
- $post_id = !empty($_GET['post']) ? $_GET['post'] : 0;
- if( !$post_id ) return;
- $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
- if ($template_file != 'page-pop.php') return;
- global $order_list,$post_styles,$sort;
- $categories = get_categories();
- //See if we have any values
- $post_meta=array();
- $post_meta = get_post_meta( $post_id,false );
- $cat_id = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
- $page_title = isset( $post_meta['_page_title'] ) && $post_meta['_page_title'] ? $post_meta['_page_title'][0] : '';
- $posts_title = isset( $post_meta['_posts_title'] ) && $post_meta['_posts_title'] ? $post_meta['_posts_title'][0] : '';
- $order_by = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
- $asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
- $list_style = isset( $post_meta['_list_style'] ) ? $post_meta['_list_style'][0] : 'default';
- $post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
- if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
- $days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : '0';
- if($days && !is_numeric( $days )) $days = '0';
- ?>
- <!-- Start page of posts meta box -->
- <div class="inside">
- <p><label><strong><?php _e( 'Page Title', 'pageofposts' ); ?></strong></label></p>
- <input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $page_title; ?>"/>
- <p><label><strong><?php _e( 'Posts Title', 'pageofposts' ); ?></strong></label></p>
- <input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $posts_title; ?>"/>
- <p><label><strong><?php _e("Category")?></strong></label></p>
- <select id="_cat_id" name="_cat_id">
- <?php
- //Category List
- foreach ($categories as $cat) :
- $selected = ( $cat->cat_ID == $cat_id ) ? ' selected = "selected" ' : '';
- $option = '<option '.$selected .'value="' . $cat->cat_ID;
- $option = $option .'">';
- $option = $option .$cat->cat_name;
- $option = $option .'</option>';
- echo $option;
- endforeach;
- ?>
- </select>
- <p><label><strong><?php _e("Sort by")?></strong></label></p>
- <select id="_order_by" name="_order_by">
- <?php
- foreach ($order_list as $output) :
- $selected = ( $output['value'] == $order_by ) ? ' selected = "selected" ' : '';
- $option = '<option '.$selected .'value="' . $output['value'];
- $option = $option .'">';
- $option = $option .$output['label'];
- $option = $option .'</option>';
- echo $option;
- endforeach;
- ?>
- </select>
- <p><label><strong><?php _e("Order")?><strong></label></p>
- <select id="_asc" name="_asc">
- <?php
- foreach ($sort as $output) :
- $selected = ( $output['value'] == $asc ) ? ' selected = "selected" ' : '';
- $option = '<option '.$selected .'value="' . $output['value'];
- $option = $option .'">';
- $option = $option .$output['label'];
- $option = $option .'</option>';
- echo $option;
- endforeach;
- ?>
- </select>
- <p><label><strong><?php _e("Posts Style")?><strong></label></p>
- <select id="_list_style" name="_list_style">
- <?php
- foreach ($post_styles as $output) :
- $selected = ( $output['value'] == $list_style ) ? ' selected = "selected" ' : '';
- $option = '<option '.$selected .'value="' . $output['value'];
- $option = $option .'">';
- $option = $option .$output['label'];
- $option = $option .'</option>';
- echo $option;
- endforeach;
- ?>
- </select>
- <p><strong><label><?php _e( 'Posts per Page', 'pageofposts' ); ?><strong></label></p>
- <input id="_post_count" name="_post_count" type="text" value="<?php echo $post_count; ?>" size="3" />
- <p><strong><label><?php _e( 'Posts in the last days', 'pageofposts' ); ?><strong></label></p>
- <input id="_days" name="_days" type="text" value="<?php echo $days; ?>" size="3" />
- </div>
- <!-- End page of posts meta box -->
- <?php
- }
- function update_pop_meta( $post_id ){
- if ( empty( $_POST ) ) {
- return;
- } else {
- $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
- if ($template_file != 'page-pop.php') return;
- if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
- return $post_id;
- } else {
- if ( $_POST['post_type'] == 'page' ) {
- if ( !current_user_can( 'edit_page', $post_id ) )
- return $post_id;
- } else {
- if ( !current_user_can( 'edit_post', $post_id ) )
- return $post_id;
- }
- $meta = isset( $_POST['_cat_id'] ) ? $_POST['_cat_id'] : 1;
- pop_update_post_meta($post_id, '_cat_id', $meta);
- $meta = isset( $_POST['_page_title'] ) ? $_POST['_page_title'] : '';
- pop_update_post_meta($post_id, '_page_title', $meta);
- $meta = isset( $_POST['_posts_title'] ) ? $_POST['_posts_title'] : '';
- pop_update_post_meta($post_id, '_posts_title', $meta);
- $meta = isset( $_POST['_order_by'] ) ? $_POST['_order_by'] : 'date';
- pop_update_post_meta($post_id, '_order_by', $meta);
- $meta = isset( $_POST['_asc'] ) ? $_POST['_asc'] : 'DESC';
- pop_update_post_meta($post_id, '_asc', $meta);
- $meta = isset( $_POST['_list_style'] ) ? $_POST['_list_style'] : 'default';
- pop_update_post_meta($post_id, '_list_style', $meta);
- $meta = isset( $_POST['_post_count'] ) ? $_POST['_post_count'] : get_option('posts_per_page');
- pop_update_post_meta($post_id, '_post_count', $meta);
- $meta = isset( $_POST['_days'] ) ? $_POST['_days'] : 0;
- pop_update_post_meta($post_id, '_days', $meta);
- return;
- }
- }
- }
- function pop_update_post_meta($post_id, $key, $data) {
- $post_meta = get_post_meta($post_id, $key, true);
- if( $data != '' && $post_meta != $data) {
- update_post_meta($post_id, $key, $data);
- } elseif ( $post_meta != '' && $data == '' ) {
- delete_post_meta($post_id, $key);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement