Support

Account

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) ?>