我遇到的问题是自定义帖子类型register_post_type
必须在init
和functions.php
在这之后很久才读到。
那不是真的。functions.php
之前已读取init
因为您可以在其中声明函数,然后将它们挂接到init
.
由于您希望避免设置页面,并且希望用户修改代码(风险自负),因此可以使用以下标志:
add_action(\'init\', \'wpse_109719_post_types\');
function wpse_109719_post_types(){
$post_type1 = true;
$post_type2 = true;
$post_type3 = false; // your users don\'t want this one
if( $post_type1 ){
register_post_type( \'your_post_type_1\', $args );
}
if( $post_type2 ){
register_post_type( \'your_post_type_2\', $args );
}
if( $post_type3 ){
// won\'t be registered since $post_type3 was declared as false
register_post_type( \'your_post_type_3\', $args );
}
}
未测试,但您可以使用类似的方法来收集帖子类型:
$val = array ();
$options_array = apply_filters( \'my_unique_filter\', $val );
print_r( $options_array );
// make a loop and register_post_type here. don\'t forget to hook at init
在插件中:
function filter1( $val ){
$val[] = \'post_type1\';
return $val;
}
function filter2( $val ){
$val[] = \'post_type2\';
return $val;
}
function filter3( $val ){
$val[] = \'post_type3\';
return $val;
}
add_filter(\'my_unique_filter\', \'filter1\');
add_filter(\'my_unique_filter\', \'filter2\');
add_filter(\'my_unique_filter\', \'filter3\');