古登堡木块-带几种立柱类型的模板锁

时间:2020-05-25 作者:Jandon

我使用template_lock 锁定一些古腾堡街区。但我想在三种不同的帖子类型中使用它。

get_post_type_object(); 只允许字符串,不允许数组。

如何使用多个帖子类型而不重复三次相同的功能?

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\' );

1 个回复
最合适的回答,由SO网友:Welcher 整理而成

这是一个PHP问题,而不是WordPress问题,但您可以执行以下操作:

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\' );
希望有帮助!

相关推荐