Gutenberg blocks - template_lock with several post type

I use template_lock to lock some Gutenberg blocks. But I would like to use it in three differents post type.get_post_typ

I use template_lock to lock some Gutenberg blocks. But I would like to use it in three differents post type.

get_post_type_object(); admit only string, no array.

How to use several post type without repeat three times the same function please ?

function wp_template_lock_subject_imposed() {
    $post_type_object = get_post_type_object( 'subject-imposed' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_subject_imposed' );

function wp_template_lock_subject_free() {
    $post_type_object = get_post_type_object( 'subject-free' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_subject_free' );

function wp_template_lock_dissertation() {
    $post_type_object = get_post_type_object( 'dissertation' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_dissertation' );

I use template_lock to lock some Gutenberg blocks. But I would like to use it in three differents post type.

get_post_type_object(); admit only string, no array.

How to use several post type without repeat three times the same function please ?

function wp_template_lock_subject_imposed() {
    $post_type_object = get_post_type_object( 'subject-imposed' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_subject_imposed' );

function wp_template_lock_subject_free() {
    $post_type_object = get_post_type_object( 'subject-free' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_subject_free' );

function wp_template_lock_dissertation() {
    $post_type_object = get_post_type_object( 'dissertation' );

    $post_type_object->template = array(
        array( 'acf/text-introduction'),
        array( 'acf/text-paragraph')
    );

    $post_type_object->template_lock = false;
}

add_action( 'init', 'wp_template_lock_dissertation' );
Share Improve this question edited May 25, 2020 at 11:06 Jandon asked May 25, 2020 at 10:56 JandonJandon 1471 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This is more of a PHP question than a WordPress one but you could do the following:

function wp_template_lock_example() {
    // Make an array of your post types
    $post_types = array('subject-imposed', 'subject-free', 'dissertation');

    // Loop the array and apply the template details to each.
    foreach ( $post_types as $post_type ) {
        $post_type_object = get_post_type_object( $post_type );
        $post_type_object->template = array(
            array( 'acf/text-introduction'),
            array( 'acf/text-paragraph')
        );

       $post_type_object->template_lock = false;
    }
}
add_action( 'init', 'wp_template_lock_example' );

Hope it helps!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742399361a4436594.html

相关推荐

  • Gutenberg blocks - template_lock with several post type

    I use template_lock to lock some Gutenberg blocks. But I would like to use it in three differents post type.get_post_typ

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信