Home › Forums › Backend Issues (wp-admin) › Using ACF to create CPT in back-end
Hello
I want to create UI with ACF for creating CPT. So I create repeater, inside of it I added CPT code. Now for editing the ‘name’ and others labels I want use text field. So this is my code:
if( have_rows('additional-post-type', 'option') ):
while ( have_rows('additional-post-type', 'option') ) : the_row();
function custom_post_type() {
$labels = array(
'name' => _x( the_sub_field('cpt-name'), 'Post Type General Name', 'twentythirteen' ),
// others
);
$args = array(
// options
);
register_post_type( 'movies', $args );
}
add_action( 'init', 'custom_post_type', 0 );
endwhile;
endif;
Repeater works fine, the CPT is create but I have problem with get_sub_field(‘cpt-name’) field – it’s blank in ‘name’ label. Where is my mistake? This field is inside this repeater, repeater is on option page.
Thanks!
Hi Damian P. Try using get_sub_field(‘cpt-name’) instead of the_sub_field(‘cpt-name’)
<?php
if( have_rows('additional-post-type', 'option') ):
while ( have_rows('additional-post-type', 'option') ) : the_row();
function custom_post_type() {
$labels = array(
'name' => _x( get_sub_field('cpt-name'), 'Post Type General Name', 'twentythirteen' ),
// others
);
$args = array(
// options
);
register_post_type( 'movies', $args );
}
add_action( 'init', 'custom_post_type', 0 );
endwhile;
endif;
Thanks for responding, @lucaspulliese!
Unfortunately get_sub_field doesn’t work too. I checked this code without CPT and this fields works, but stops with CPT. Any ideas?
You’re welcome!
I tried with your code and didn’t work for me too, so I did a few changes and now works for me.
In this code I use a function called slugify (https://stackoverflow.com/questions/2955251/php-function-to-make-slug-url-string), to create the CPT slug.
Tell me if it works for you
if( have_rows('additional-post-type', 'option') ) {
function register_post_types() {
while ( have_rows('additional-post-type', 'option') ) { the_row();
$cpt_name = get_sub_field('cpt-name');
$cpt_slug = slugify($cpt_name);
// new custom post type
register_post_type(
$cpt_slug , array(
'public' => true,
'labels' => array(
'name' => __($cpt_name, 'theme'),
'singular_name' => __($cpt_name, 'theme'),
),
'has_archive' => true,
'supports' => array(
'title',
'thumbnail',
),
)
);
}
}
add_action( 'init', 'register_post_types' );
}
Ok, it’s makes sense. But unfortunately not working… take a look:
if( have_rows('additional-post-type', 'option') ):
function custom_post_type() {
while ( have_rows('additional-post-type', 'option') ) : the_row();
$cpt_name = get_sub_field('cpt-name');
$cpt_slug = slugify($cpt_name);
register_post_type( $cpt_slug , array(
'label' => __( 'movies', 'twentythirteen' ),
'description' => __( 'Movie news and reviews', 'twentythirteen' ),
'labels' => array(
'name' => _x( $cpt_name, 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Movies', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Movies', 'twentythirteen' ),
'parent_item_colon' => __( 'Parent Movie', 'twentythirteen' ),
'all_items' => __( 'All Movies', 'twentythirteen' ),
'view_item' => __( 'View Movie', 'twentythirteen' ),
'add_new_item' => __( 'Add New Movie', 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit Movie', 'twentythirteen' ),
'update_item' => __( 'Update Movie', 'twentythirteen' ),
'search_items' => __( 'Search Movie', 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
)
);
add_action( 'init', 'custom_post_type', 0 );
endwhile;
}
endif;
Ok, try this, I changed the location of the add_action hook and “movie” texts with $cpt_name
if( have_rows('additional-post-type', 'option') ):
function custom_post_type() {
while ( have_rows('additional-post-type', 'option') ) : the_row();
$cpt_name = get_sub_field('cpt-name');
$cpt_slug = slugify($cpt_name);
register_post_type( $cpt_slug , array(
'label' => __( $cpt_slug, 'twentythirteen' ),
'description' => __( $cpt_slug.' news and reviews', 'twentythirteen' ),
'labels' => array(
'name' => _x( $cpt_name, 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( $cpt_name, 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( $cpt_name, 'twentythirteen' ),
'parent_item_colon' => __( 'Parent '.$cpt_name, 'twentythirteen' ),
'all_items' => __( 'All '.$cpt_name, 'twentythirteen' ),
'view_item' => __( 'View '.$cpt_name, 'twentythirteen' ),
'add_new_item' => __( 'Add New '.$cpt_name, 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit '.$cpt_name, 'twentythirteen' ),
'update_item' => __( 'Update '.$cpt_name, 'twentythirteen' ),
'search_items' => __( 'Search '.$cpt_name, 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
)
);
endwhile;
}
add_action( 'init', 'custom_post_type', 0 );
endif;
Ok, try this, I changed the location of the add_action hook and changed “movie” texts with $cpt_name
if( have_rows('additional-post-type', 'option') ):
function custom_post_type() {
while ( have_rows('additional-post-type', 'option') ) : the_row();
$cpt_name = get_sub_field('cpt-name');
$cpt_slug = slugify($cpt_name);
register_post_type( $cpt_slug , array(
'label' => __( $cpt_slug, 'twentythirteen' ),
'description' => __( $cpt_slug.' news and reviews', 'twentythirteen' ),
'labels' => array(
'name' => _x( $cpt_name, 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( $cpt_name, 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( $cpt_name, 'twentythirteen' ),
'parent_item_colon' => __( 'Parent '.$cpt_name, 'twentythirteen' ),
'all_items' => __( 'All '.$cpt_name, 'twentythirteen' ),
'view_item' => __( 'View '.$cpt_name, 'twentythirteen' ),
'add_new_item' => __( 'Add New '.$cpt_name, 'twentythirteen' ),
'add_new' => __( 'Add New', 'twentythirteen' ),
'edit_item' => __( 'Edit '.$cpt_name, 'twentythirteen' ),
'update_item' => __( 'Update '.$cpt_name, 'twentythirteen' ),
'search_items' => __( 'Search '.$cpt_name, 'twentythirteen' ),
'not_found' => __( 'Not Found', 'twentythirteen' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentythirteen' ),
),
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'hierarchical' => false,
'public' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
)
);
endwhile;
}
add_action( 'init', 'custom_post_type', 0 );
endif;
Still, the same problem. It this working for you? I’m starting to thinking maybe there is some conflict with other my code…
Yep, that worked for me. What are the names of the custom post types? I’m asking you this because the maximum length of the custom post type slug is 20 characters.
Send me an email to [email protected] if you want, I will try to help you
The topic ‘Using ACF to create CPT in back-end’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.