Advertisement
daymobrew

CMB2 - Cannot have two taxonomy_* fields.

Jan 25th, 2018
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. <?php
  2. add_action( 'cmb2_admin_init', 'diabolo_home_page_listings' );
  3. function diabolo_home_page_listings() {
  4.     $prefix = '_dp_';
  5.  
  6.     $cmb = new_cmb2_box( array(
  7.         'id'           => $prefix . 'metaboxID',
  8.         'title'        => 'Column 2 & 3 Settings',
  9.         'object_types' => array( 'page' ),
  10.         'context'      => 'after_editor',
  11.         'show_names'   => true,
  12.         'show_on_cb' => 'diabolo_show_cmb2_on_front_page',
  13.     ) );
  14.  
  15.     $cmb->add_field( array(
  16.         'name'    => 'Column 2 title',
  17.         'desc'    => 'Enter the text to be shown above the posts in column 2.',
  18.         //'default' => 'standard value (optional)',
  19.         'id' => $prefix . 'col2title',
  20.         'type'    => 'text',
  21.     ) );
  22.     $cmb->add_field( array(
  23.         'id' => $prefix . 'col2category',
  24.         'type' => 'taxonomy_radio',
  25.         'taxonomy' => 'category',
  26.         'name' => 'Column 2 category',
  27.         'desc' => 'Select the category for posts in column 2',
  28.     ) );
  29.     $cmb->add_field( array(
  30.         'name' => 'Number of posts to show',
  31.         'desc' => 'Numbers only',
  32.         'id'   => $prefix . 'col2count',
  33.         'type' => 'text',
  34.         'attributes' => array(
  35.             'type' => 'number',
  36.             'pattern' => '\d*',
  37.         ),
  38.     ) );
  39.     $cmb->add_field( array(
  40.         'name'    => 'Column 3 title',
  41.         'desc'    => 'Enter the text to be shown above the posts in column 3.',
  42.         //'default' => 'standard value (optional)',
  43.         'id' => $prefix . 'col3title',
  44.         'type'    => 'text',
  45.     ) );
  46.     $cmb->add_field( array(
  47.         'id' => $prefix . 'col3category',
  48.         'type' => 'taxonomy_select',
  49.         'taxonomy' => 'category',
  50.         'name' => 'Column 3 category',
  51.         'desc' => 'Select the category for posts in column 3',
  52.     ) );
  53.     $cmb->add_field( array(
  54.         'name' => 'Number of posts to show',
  55.         'desc' => 'Numbers only',
  56.         'id'   => $prefix . 'col3count',
  57.         'type' => 'text',
  58.         'attributes' => array(
  59.             'type' => 'number',
  60.             'pattern' => '\d*',
  61.         ),
  62.     ) );
  63. }
  64.  
  65. function diabolo_show_cmb2_on_front_page( $cmb ) {
  66.     //error_log( "CMB: ".var_export( $cmb, true ) );
  67.  
  68.     // Extracted from: https://github.com/CMB2/CMB2/wiki/Adding-your-own-show_on-filters#example-front-page-show_on-filter
  69.     $post_id = 0;
  70.  
  71.     // If we're showing it based on ID, get the current ID
  72.     if ( isset( $_GET['post'] ) ) {
  73.         $post_id = $_GET['post'];
  74.     } elseif ( isset( $_POST['post_ID'] ) ) {
  75.         $post_id = $_POST['post_ID'];
  76.     }
  77.  
  78.     if ( ! $post_id ) {
  79.         return false;
  80.     }
  81.  
  82.     // Get ID of page set as front page, 0 if there isn't one
  83.     $front_page = get_option( 'page_on_front' );
  84.  
  85.     // There is a front page set and we're on it!
  86.     return $post_id == $front_page;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement