I am registering a post in my wp-admin but I don't want editor etc, so adding some field. By the R & D I founded how to add text box and it's awesome but now I have to add a select box and the option value should be post title. I don't want to do this by plugin.
I added text field as:
$client_meta_box = array(
'id' => 'meta-client',
'title' => __('Client Options','mfn-opts'),
'page' => 'client',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'id' => 'post-link',
'type' => 'text',
'title' => __('Link', 'opts'),
'sub_desc' => __('Link to client`s site', 'opts'),
),
),
);
and I can add select box by just change the type as 'type' => 'select' but how did I get the post title value in option.
Using this to add meta box lile text, chackbox, selectoption.
$meta_boxes[] = array(
'id' => 'meta-client', // meta box id, unique per meta box
'title' => 'Client Options', // meta box title
'pages' => array('client'), // post types, accept custom post types as well,
//default is array('post'); optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array(
array(
'label'=> 'Text Input',
'desc' => 'A description for the field.',
'id' => $prefix.'text',
'type' => 'text'
),
array(
'label'=> 'Textarea',
'desc' => 'A description for the field.',
'id' => $prefix.'textarea',
'type' => 'textarea'
),
array(
'label'=> 'Checkbox Input',
'desc' => 'A description for the field.',
'id' => $prefix.'checkbox',
'type' => 'checkbox'
),
array(
'label'=> 'Select Box',
'desc' => 'A description for the field.',
'id' => $prefix.'select',
'type' => 'select',
'options' => array(
'option1' => 'Optionone', // 'value'=>'label'
'option2' => 'Optiontwo',
'option3' => 'Optionthree'
)
)
);
Related
I have created a Custom Post Type and added 50+ posts.
Now this post type have some meta values which I need to display.
Here is how I created the post type:
function colorshadescpt() {
register_post_type( 'colorshades',
array(
'labels' => array(
'name' => __('Color Shades'),
'singular_name' => __('Shade'),
'add_new_item' => __('Add New'),
'edit_item' => __('Edit'),
'new_item' => __('Add New'),
'view_item' => __('View'),
),
'public' => true,
'supports' => array( 'title'),
'capability_type' => 'post'
)
);
}
add_action('init', 'colorshadescpt');
And added this meta_boxes:
$prefix = 'cg_';
$meta_boxes = array();
$meta_boxes[] = array(
'id' => 'colorcodes',
'title' => 'Color Code',
'pages' => array('colorshades'),
'fields' => array(
array(
'name' => 'Color code', // field name
'desc' => 'Enter Color code here.', // field description, optional
'id' => 'color_code', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
),
)
);
$meta_boxes[] = array(
'id' => 'colorshades',
'title' => 'Color Value',
'pages' => array('colorshades'),
'fields' => array(
array(
'name' => 'R', // field name
'desc' => 'Enter Red color value here.', // field description, optional
'id' => 'red_code', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
),
array(
'name' => 'G', // field name
'desc' => 'Enter green color value here.', // field description, optional
'id' => 'green_code', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
),
array(
'name' => 'B', // field name
'desc' => 'Enter blue color value here.', // field description, optional
'id' => 'blue_code', // field id, i.e. the meta key
'type' => 'text', // text box
'std' => '', // default value, optional
),
array(
'name' => 'Type', // field name
'desc' => 'Please Select type for shade.', // field description, optional
'id' => 'shade_color_type', // field id, i.e. the meta key
'type' => 'radio', // text box
'std' => 'product',
'options' => array( // array of key => value pairs for radio options
'product' => 'Product',
'machine' => 'Machine',
), // default value, optional
),
)
);
$meta_boxes[] = array(
'id' => 'productid',
'title' => 'Product Settings',
'pages' => array('colorshades'),
'fields' => array(
array(
'name' => 'Product Id',
'desc' => 'Enter the product id here.',
'id' => 'colors_product_id',
'type' => 'text',
'std' => '',
),
)
);
$meta_boxes[] = array(
'id' => 'shade_details',
'title' => 'Other Details',
'pages' => array('colorshades'),
'fields' => array(
array(
'name' => 'Details', // field name
'desc' => 'Enter shade details.', // field description, optional
'id' => 'colors_details', // field id, i.e. the meta key
'type' => 'textarea', // text box
'std' => '', // default value, optional
),
)
);
foreach ($meta_boxes as $meta_box) {
$my_box = new RW_Meta_Box_Taxonomy($meta_box);
}
Here is the screenshot of how it looks as a post:
How I am getting the result on the other side:
<?php
global $product;
$args = array(
'post_type' => 'colorshades',
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'colors_product_id',
'value' => $product->id,
'compare' => '=',
)
)
);
$query = new WP_Query( $args );
if ($query->have_posts()):
foreach( $query->posts as $id ):
$Color_Code = get_post_meta($id, "color_code", true);
$R = get_post_meta($id, "red_code", true);
$G = get_post_meta($id, "green_code", true);
$B = get_post_meta($id, "blue_code", true);
$all_color_product_notification = '';
$image_url = site_url().'/wp-content/themes/porto/images/swatch.png';
echo '<div class="colors_box mb-4 mx-lg-4 mx-md-3 mx-2">
<p> '.get_the_title( $id ).'</p>
</div>';
?>
<?php
endforeach;
endif;
As you can see I am getting the post based on the meta field "product id", but Meta Query returns empty.
Without the meta query I get all the posts but I want on basis of Ids.
Here is the result if I query without Meta values:
Please guide me if I am doing something wrong here.
I would like to ask for help, I am currently learning the WordPress customizer and iplementing it on my project. I have a select field which I would like to increment and decrement on button click (+/-).
$wp_customize->add_section('samplecustom_section' , array(
'title' => esc_html__('Sample Customizer', 'theme_name'),
'priority' => 169,
'capability' => 'edit_theme_options',
));
$wp_customize->add_setting('sample-list', array(
'default' => '',
'transport' => 'refresh',
));
$wp_customize->add_control('sample-list', array(
'label' => esc_html__('Sample Label', 'theme_name'),
'description' => esc_html__('Sample description', 'theme_name'),
'section' => 'samplecustom_section',
'settings' => 'sample-list',
'type' => 'select',
'choices' => array(
'value1' => __( 'Value 1' ),
'value2' => __( 'Value 2' ),
'value3' => __( 'Value 3' ),
));
// button
$wp_customize->add_control('add_button', array(
'section' => 'samplecustom_section',
'settings' => 'sample-list',
'type' => 'button',
'capability' => 'edit_theme_options',
'input_attrs' => array(
'value' => '+',
'class' => 'customizerbutton button-primary',
)
));
here is a sample image where when I click the plus button the select field will increment (or decrement by minus button) creating new ids for the add-control customizer.
I am trying to use Redux inside the wordpress theme and not as a plugin. In functions.php I included both redux-framework.php and sample-config.php .
Now I need to create a repeater field.
From Redux doc, I got the following code to use in order to create a repeater field:
$this->sections[] = array(
'title' => __('Repeater Field', 'redux-framework-demo' ),
'icon' => 'el-icon-thumbs-up',
'fields' => array(
array(
'id' => 'repeater-field-id',
'type' => 'repeater',
'title' => __( 'Title', 'redux-framework-demo' ),
'subtitle' => __( '', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
//'group_values' => true, // Group all fields below within the repeater ID
//'item_name' => '', // Add a repeater block name to the Add and Delete buttons
//'bind_title' => '', // Bind the repeater block title to this field ID
//'static' => 2, // Set the number of repeater blocks to be output
//'limit' => 2, // Limit the number of repeater blocks a user can create
//'sortable' => false, // Allow the users to sort the repeater blocks or not
'fields' => array(
array(
'id' => 'title_field',
'type' => 'text',
'placeholder' => __( 'Title', 'redux-framework-demo' ),
),
array(
'id' => 'text_field',
'type' => 'text',
'placeholder' => __( 'Text Field', 'redux-framework-demo' ),
),
array(
'id' => 'select_field',
'type' => 'select',
'title' => __( 'Select Field', 'redux-framework-demo' ),
'options' => array(
'1' => __( 'Option 1', 'redux-framework-demo' ),
'2' => __( 'Option 2', 'redux-framework-demo' ),
'3' => __( 'Option 3', 'redux-framework-demo' ),
),
'placeholder' => __( 'Listing Field', 'redux-framework-demo' ),
),
)
)
)
);
but if I place the code inside functions.php, what will the $this variable refer to? It'll produce errors. So how to use the snippet so that I can retrieve the values from template files as well?
You have to tried an old version system to create a section. You can try the new version system, I'm not sure if this works or not, but you can try this way:
Redux::setSection($opt_name, array(
'title' => __('Ads Sections', 'cbnews'),
'id' => 'ads-sections',
'desc' => __('You can manage your ads', 'cbnews'),
'icon' => 'dashicons dashicons-dashboard',
'fields' => array(
array(
'id' => 'repeater-field-id',
'type' => 'repeater',
'title' => __( 'Title', 'redux-framework-demo' ),
'subtitle' => __( '', 'redux-framework-demo' ),
'desc' => __( '', 'redux-framework-demo' ),
//'group_values' => true, // Group all fields below within the repeater ID
//'item_name' => '', // Add a repeater block name to the Add and Delete buttons
//'bind_title' => '', // Bind the repeater block title to this field ID
//'static' => 2, // Set the number of repeater blocks to be output
//'limit' => 2, // Limit the number of repeater blocks a user can create
//'sortable' => false, // Allow the users to sort the repeater blocks or not
'fields' => array(
array(
'id' => 'title_field',
'type' => 'text',
'placeholder' => __( 'Title', 'redux-framework-demo' ),
),
array(
'id' => 'text_field',
'type' => 'text',
'placeholder' => __( 'Text Field', 'redux-framework-demo' ),
),
array(
'id' => 'select_field',
'type' => 'select',
'title' => __( 'Select Field', 'redux-framework-demo' ),
'options' => array(
'1' => __( 'Option 1', 'redux-framework-demo' ),
'2' => __( 'Option 2', 'redux-framework-demo' ),
'3' => __( 'Option 3', 'redux-framework-demo' ),
),
'placeholder' => __( 'Listing Field', 'redux-framework-demo' ),
),
)
)
)
));
I have a metabox in my post editor which allows me to pick a single category, I would like this changed to a checkbox where more than one can be picked. I have worked on the following, I would imagine it's a case of changing taxonomy_radio to something like taxonomy_checkbox, however that crashes the metabox completely:
$meta_boxes['test_metabox'] = array(
'id' => 'test_metabox',
'title' => __( 'TEST', 'cmb' ),
'pages' => array( 'post', ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
'fields' => array(
array(
'name' => __( 'Category', 'cmb' ),
'desc' => __( 'field description (optional)', 'cmb' ),
'id' => 'test-cat',
'type' => 'taxonomy_radio',
'taxonomy' => 'category'
),
Any help would be great.
Just found out that it needs to be changed to taxonomy_multicheck
i have this custom Metabox field group from https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress/wiki/Field-Types#group containing 3 fields and displayed on the Products page template, i want to call the latest posts the client add to display them on my front-page. here is the code of the custom fields.
/**
* Repeatable Field Groups
*/
$meta_boxes['field_group'] = array(
'id' => 'field_group',
'title' => __( 'Manage your products here', 'cmb2' ),
'show_on' => array( 'id' => array( 11, ) ), // Specific post IDs to display this metabox
'object_types' => array( 'page', ),
'fields' => array(
array(
'id' => $prefix . 'repeat_group',
'type' => 'group',
'options' => array(
'group_title' => __( 'Product {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Product', 'cmb2' ),
'remove_button' => __( 'Remove Product', 'cmb2' ),
'sortable' => true, // beta
),
'fields' => array(
array(
'name' => 'Product Name',
'id' => 'product_name',
'type' => 'text',
),
array(
'name' => 'Product Description',
'description' => 'Write a short description for this Product',
'id' => 'product_description',
'type' => 'textarea',
),
array(
'name' => 'Product Image',
'id' => 'product_image',
'type' => 'file',
),
),
),
),
);
UPDATE:
i found this http://codex.wordpress.org/Function_Reference/wp_get_recent_posts but i still can't figure out how to make it get the posts from the custom fields