不久前,我在跟随Tutsplus视频教程创建自定义帖子类型,因此我为自己制作了两个类似的小型自定义帖子类型。
今天我在探索如何优化我的网站,所以我安装了一个插件(查询监视器),显示每个页面上有多少sql查询。我惊讶地发现,在每个页面(frontpage.php、single.php等)上,我都有200多个sql查询:甚至在只有一个图像的附件页面上!当我禁用插件时,大约有60个sql查询。
class branje_biljaka {
public $months = array(\'...\');
public $terms = array(\'...\');
public function __construct()
{
$this->register_post_type();
$this->register_months();
$this->add_meta_box();
}
public function register_post_type()
{
$args = array(
\'labels\' => array(
\'name\' => \'Kalendar branja\',
),
\'query_var\' => \'branje\',
\'rewrite\' => array(
\'slug\' => \'kalendar-branja\'
),
\'public\' => true,
\'publicly_queryable\' => true,
\'has_archive\' => true,
\'menu_position\' => 5,
\'hierarchical\' => false,
\'exclude_from_search\' => false,
\'supports\' => array(\'title\', \'excerpt\')
);
register_post_type(\'branje_biljaka\', $args);
}
public function register_months()
{
foreach($this->months as $month => $month_var)
{
$args = array(
\'labels\' => array(\'name\' => $month),
\'hierarchical\' => true,
\'sort\' => true,
\'query_var\' => $month_var
);
register_taxonomy($month_var, \'branje_biljaka\', $args);
$this->registerTerms($month_var);
}
}
public function registerTerms($taxonomy)
{
foreach($this->terms as $term)
{
wp_insert_term($term, $taxonomy);
}
}
}
add_action(\'init\', function(){
new my_custom_post_type();
});
我只需要在自定义的post类型页面(archive-ctp.php,single-ctp.php)上显示数据,我想问题在于
add_action(\'init\', function());
这也是我在法典中看到的
https://codex.wordpress.org/Function_Reference/register_post_type我如何限制注册和有关此帖子类型的所有内容仅在需要时使用?