Home › Forums › Add-ons › Options Page › Same Option page for different CPT › Reply To: Same Option page for different CPT
Thanks for bringing me on the right way 🙂
Now I can create a CPT including an options page with a Repeater. That’s super handy for me. Just the field group needs to be linked again from the acf settings to the new option page (is there any easy solution for automation?).
For anyone looking for something similar, here the code from the functions.php. No clue if it’s perfect, but it works well for me.
// CREATES CUSTOM POSTTYPE WITH OPTION PAGES
function instrument_manager() {
if( have_rows('instrument_manager', 'option') ):
// add specific Option Pages for each CPT
if( function_exists('acf_add_options_page') ) {
while( have_rows('instrument_manager', 'option') ) : the_row();
$post_type_title = get_sub_field('instrument_title', 'option'); //can be every title
$post_type_slug = get_sub_field('instrument_slug', 'option'); //no special chars, no caps
acf_add_options_sub_page(array(
'page_title' => 'Settings ' . $post_type_title,
'menu_title' => 'Settings ' . $post_type_title,
'parent_slug' => 'edit.php?post_type=' . $post_type_slug,
'post_id' => 'options_' . $post_type_slug,
));
endwhile;
}
// create Custom Post Types
function create_posttype() {
while( have_rows('instrument_manager', 'option') ) : the_row();
$post_type_title = get_sub_field('instrument_title', 'option');
$post_type_slug = get_sub_field('instrument_title', 'option');
$lables = array('name' => __( $post_type_title ), 'singular_name' => __( $post_type_title ));
$args = array(
'labels' => $lables,
'public' => true,
'publicly_queryable' => true,
'supports' => array( 'title', 'editor', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag' ),
);
register_post_type( $post_type_slug , $args );
endwhile;
}
endif;
}
add_action('init', 'instrument_manager');
add_action( 'init', 'create_posttype' );
and to read out the settings in my archive.php I used this line:
$post_type = 'options_' . get_query_var('post_type');
the_field('field-name', $post_type) ?>
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.