I'm a newbie Wordpress developer & I can't figure out how to get the options chosen in my Custom Meta Boxes to populate on my posts. For example, this is how my admin panel looks when creating new posts:
http://i.stack.imgur.com/9RGz8.jpg
To give you an example of my code for this section that is in my meta.php file... It's:
//POST META BOXES
'post'=> array(
array(
'name' => 'Home Type',
'id' => PEXETO_META_PREFIX.'text',
'type' => 'select',
'options' => array( array( 'name'=>'1', 'id'=>'1' ),
array( 'name'=>'2', 'id'=>'2' ),
array( 'name'=>'3', 'id'=>'3' ),
array( 'name'=>'4', 'id'=>'4' ),
array( 'name'=>'5', 'id'=>'5' ) ),
'desc' => '...'
),
array(
'name' => 'Listing Status',
'id' => PEXETO_META_PREFIX.'text',
'type' => 'select',
'options' => array( array( 'name'=>'1', 'id'=>'1' ),
array( 'name'=>'2', 'id'=>'2' ),
array( 'name'=>'3', 'id'=>'3' ) ),
'desc' => '...'
),
array(
'name' => 'Lot Size',
'id' => PEXETO_META_PREFIX.'text',
'type' => 'text',
'desc' => '...'
),
What I'm trying to do is set it up so that each meta box will generate a two column row in my actual post. The left side would show the title of the box, and the right side would show the answer given by the user. An example of this is below:
http://i.stack.imgur.com/suJTq.jpg
Any help with this one would be greatly appreciated as I'm stuck...
Thanks.
You should look into get_post_meta()
Then something like this to echo those metas out in a table:
$metas = get_post_meta( get_the_ID() );
echo '<table>';
foreach( $metas as $key => $meta ) {
printf('<tr><th>%s</th><td>%s</td></tr>',
$key, $meta[0] );
}
echo '</table>';
Related
I've tried a bunch of different functions and approaches but so far I haven't been able to get it working.
The goal is to add an Advanced Custom Field group to the backend of Wordpress with some PHP-code. In the best scenario we add the PHP-code to a method of a class.
public function create_group( $group_name ) {
if ( $this->does_group_already_exists( $group_name ) ) {
return false;
}
acf_add_local_field_group( array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
) );
return true;
}
Nothing gets added with the code above. I also tried adding it to functions.php and it with a add_action() function like so:
add_action( 'acf/init', array( $this, 'create_group' ) );
But again, no results.
Hope some one can share a working solution.
Today I finally discovered a solution for adding a ACF group to the backend dynamically with PHP-code.
It can be done by adding a new post directly with the acf-field-group post type. Here is my implementation for those awesome people from the future that are interested:
public function create_form( $form_name ) {
$new_post = array(
'post_title' => $form_name,
'post_excerpt' => sanitize_title( $form_name ),
'post_name' => 'group_' . uniqid(),
'post_date' => date( 'Y-m-d H:i:s' ),
'comment_status' => 'closed',
'post_status' => 'publish',
'post_type' => 'acf-field-group',
);
$post_id = wp_insert_post( $new_post );
return $post_id;
}
Where $form_name is the name of the ACF group. It works. And there was no need for using a specific hook. I could just call this method directly.
Actually you can create such code over ACF in the WP-Backend itself (not sure if this only works in ACF Pro). Under Admin -> Custom Fields -> Tools -> Export -> Create PHP. The generated code is a great starting point for a programmatic ACF integration.
It should look something like this:
acf_add_local_field_group(array(
'key' => 'group_5d146d18eeb92',
'title' => 'My Group Title',
'fields' => array(
array(
'key' => 'field_5d146d1f27577',
'label' => 'My Field Title',
'name' => 'my_field_name',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'message' => '',
'default_value' => 0,
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'my_custom_post_type',
),
),
),
'menu_order' => 0,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
Check out the ACF page for registering fields via PHP.
Action acf/init is available for pro version only, maybe that was the reason it did not work at the first place..
For basic version you have to use acf/register_fields to register your custom fields.
I'm trying to add fields to an options page through PHP, and I can't get it to work, I've tried nearly everything by now, but it just won't work.
I hope you can help, my php looks like this:
if (function_exists('acf_add_options_page')) {
$option_page = acf_add_options_page(array(
'page_title' => 'Indstillinger',
'menu_title' => 'Indstillinger',
'menu_slug' => 'options',
'capability' => 'edit_posts',
'redirect' => false
));
}
function my_acf_add_local_field_groups() {
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'My Group',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => 'text',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'options',
),
),
),
));
}
add_action('acf/init', 'my_acf_add_local_field_groups');
What am I doing wrong here?
I have found out.
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'theme-general-settings',
),
),
),
Here in the value field just add the "menu slug" that you have registered for the options page. Like 'theme-header-settings', 'theme-general-settings' are you requirement. I hope you understand.
I think you want
'location' => array(
array(
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'options',
),
),
),
The way you have added Option page, it seems that you have installed ACF plugin.
You can add any type of field on your option page using ACF plugin as well.
Check my attached screen shot, You need to set following condition for the field groups:
I am building a website in where I want to show the tag selected from the custom fields (mainly from radio button). I have setup the cmb2 as like below the codes..
add_action('cmb2_admin_init', 'custom_metaboxes');
function custom_metaboxes() {
$metabox = new_cmb2_box( array(
'object_types' => array( 'post'), //for the post
'title' => 'Additional Fields',
'id' => 'additional'
)
);
// showing in the admin panel
$metabox -> add_field( array(
'name' => 'Taxonomy List',
'desc' => 'This get the list of taxonomy',
'id' => 'taxonomy_list',
'type' => 'taxonomy_radio',
'taxonomy' => 'post_tag',
'default' => 'ami'
)
);
}
Ok, that is working in the post section. My tags are shown in the radio buttons, That's working. But when I tried to show the the selected tag in the front-end using
echo get_post_meta( get_the_id(), 'taxonomy_list', true )// returns nothing
nothing is echoing. Then tried var_dump function it returns string(0) "". What are the problems are working behind the scene.
Anyone please find out what the problems are.
The problem can be solved by using cmb2-2.0.2 version. And here is the code:
<?php
add_action( 'cmb2_init', 'yourprefix_register_demo_metabox' );
function yourprefix_register_demo_metabox() {
$prefix = '_yourprefix_demo_';
$cmb_demo = new_cmb2_box( array(
'id' => $prefix . 'additional',
'title' => 'Additional Fields',
'object_types' => array('page')
) );
$cmb_demo->add_field( array(
'name' => 'Taxonomy List',
'desc' => 'This get the list of taxonomy',
'id' => $prefix . 'taxonomy_list',
'type' => 'taxonomy_radio',
'taxonomy' => 'post_tag'
) );
}
?>
And in frontend you need to write:
<?php
$prefix = '_yourprefix_demo_';
echo get_tag(get_post_meta(get_the_ID(), $prefix.'taxonomy_list', true)[0])->name;
?>
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 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'
)
)
);