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;
?>
Related
I'm creating custom ACF Gutenberg blocks my a site and have successfully managed to register my own blocks. Now, I have a custom post type called Blog. I do not want blog to show all my ACF Gutenberg blocks, I want to create a separate batch for custom post type use only. I have enabled show_in_rest, but even the default Gutenberg blogs do not show for me?
Here is my approach:
1. Registering the post type (theme/functions.php)
<?php
register_post_type('Blog', theme_build_post_args('Blog', 'Blog', 'Blog', array(
'show_in_rest' => true,
'menu_icon' => 'dashicons-edit',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array(
'editor',
'title',
'author',
'revisions',
'excerpt',
'thumbnail'
) ,
)));
?>
2. Registering the ACF Gutenberg blocks for pages (theme/inc/acf-blocks/blocks.php)
Here are the blocks that I've registered for use on pages (not on the blog post type):
<?php
$hero = array(
'name' => 'hero',
'title' => __('Hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
) ,
);
$blocks = [$hero];
return $blocks;
?>
Registering the ACF Gutenberg blocks for blog post type (theme/inc/acf-blocks/blog-blocks.php)
<?php
$blog_hero = array(
'name' => 'blog_hero',
'title' => __('Blog hero') ,
'description' => __('') ,
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
) ,
);
$blog_blocks = [$blog_hero];
return $blog_blocks;
?>
Register all blocks (theme/inc/acf-blocks/functions.php)
<?php
/*
* loop though array and register each block type
*/
function block_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blocks.php';
$blocks = require($path);
foreach($blocks as $block) {
acf_register_block_type($block);
}
}
function blog_acf_init(){
$path = get_template_directory().'/inc/acf-blocks/blog-blocks.php';
$blog_blocks = require($path);
foreach($blog_blocks as $blog_block) {
acf_register_block_type($blog_block);
}
}
// Check if function exists, and hook into setup
if( function_exists('acf_register_block_type') ) {
add_action('acf/init', 'block_acf_init');
add_action('acf/init', 'blog_acf_init');
}
?>
Current results:
When creating a post on the blog custom post type, I do not have the ability to add any blocks, let alone see if blog_hero block appears:
On pages, I can see all my created blocks, however, the blog hero block shows on the page side, when I only want it for the custom post type:
Probably this way could solve the problem:
Specifying post_types param for Blog Hero block to blog
$blog_hero = array(
'name' => 'blog_hero',
'title' => __( 'Blog hero', 'Context' ),
'description' => __( '', 'Context' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero',
'blog'
),
'post_types' => array( 'blog' ),
);
And analogically specifying all post types except blog for Hero block.
$all_post_types = get_post_types();
$hero_block_post_types = array_diff( $all_post_types, array( 'blog' ) );
$hero = array(
'name' => 'hero',
'title' => __( 'Hero', 'Domain' ),
'description' => __( '', 'Domain' ),
'render_callback' => 'block_render',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array(
'hero'
),
'post_types' => $hero_block_post_types
);
$blocks = [ $hero ];
return $blocks;
Notice:
Consider adding Domain for your __ function.
Good practice to use __ function with Domain.
I am building a website using Wordpress in where I want to show the Metadata from the custom fields.
I have setup the cmb2 in my function.php as like below the codes..
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
function cmb2_sample_metaboxes() {
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$cmb->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'yourprefix_text',
'type' => 'text',
'show_on_cb' => 'cmb2_hide_if_no_cats',
) );
}
Ok, that is working in the post section, That's working. But when I tried to show Metadata in the front-end using
<?php
$text = get_post_meta( get_the_ID(), '_yourprefix_text', true );
echo esc_html( $text );
?>
nothing is echoing.
Anyone please find out what the problems are.
Looks like you've just got a typo. The id does not match the meta_key: 'yourprefix_text' vs '_yourprefix_text'
Fixed:
<?php
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
echo esc_html( $text );
?>
I want cmb2 to be displayed in posts that have a movie template
I use this code :
$cmb_review = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( "My title", 'cmb2' ),
'object_types' => array( 'post'),
'show_on' => array( 'key' => 'page-template', 'value' => 'movie.php' )
));
but that doesn't work
I also tried this code but it didn't work :
function maybe_show($cmb_review ) {
$template = get_post_meta( $cmb_review ->object_id(), 'page-template', true );
if ( 'movie.php' === $template ) {
return true;
}
return false;
}
$cmb_review = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( "My title", 'cmb2' ),
'object_types' => array( 'post'),
'show_on_cb' => 'maybe_show'
));
Your meta box object type is post and is your template page for post too?
Make sure both match.
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>';
I have created a custom meta box using option tree framework. It is showing for every page. I want that it will show only for a specific page. How can i do that?
I have written this code to functions.php file
add_action( 'admin_init', 'custom_meta_boxes' );
function custom_meta_boxes() {
$latest_work = array(
'id' => 'latest_work',
'title' => 'latest-work Meta Box',
'desc' => '',
'pages' => array( 'page' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'label' => 'latest-work',
'id' => 'latest-work',
'type' => 'text',
'desc' => 'Tell about your latest work',
'std' => '',
'rows' => '',
'post_type' => '',
'taxonomy' => '',
'class' => ''
)
)
);
ot_register_meta_box( $latest_work );
}
Please tell me how can i do that?
add_action( 'admin_menu', 'remove_post_meta_boxes' );
function remove_post_meta_boxes() {
// lets assume blog page has the id 23
// let's remove the meta box from blog
if( isset($_GET['post']) && $_GET['post'] == 23 ){
remove_meta_box( 'latest_work', 'page', 'normal' );
}
}
take a look for more information remove_meta_box()
Use the following.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// checks for post/page ID for example i have added 7
if ($post_id == '7'){
/.add_meta box code inside
}