我有一个名为“欧洲”的父类别,我需要在其中插入大约20多个子类别(法国、意大利、德国等)
要添加什么样的SQL语句(例如,法国到欧洲(类别ID=1))?
这是一个百分之百的新博客,没有帖子,没有自定义字段,什么都没有。
我有一个名为“欧洲”的父类别,我需要在其中插入大约20多个子类别(法国、意大利、德国等)
要添加什么样的SQL语句(例如,法国到欧洲(类别ID=1))?
这是一个百分之百的新博客,没有帖子,没有自定义字段,什么都没有。
通过原始SQL查询插入分类术语很困难,不鼓励使用核心wp_insert_term
而是函数。
假设您有一系列大陆/国家术语:
$countries_list = array(
\'Europe\' => array(
\'Austria\', \'France\', \'Italy\', \'Germany\', \'Spain\', \'United Kingdom\'
)
);
foreach ( $countries_list as $continent => $countries ) {
// get term object for continent
$parent = get_term_by( \'slug\', sanitize_title( $continent ), \'category\' );
if ( empty( $parent ) ) {
// continent term doesn\'t exist, let\'s create it
$parent_a = wp_insert_term( $continent, \'category\' );
if ( ! is_array( $parent_a ) ) continue;
// get term object for the just created continent term
$parent = get_term( $parent_a->term_id );
}
// loop countries to add the related terms
foreach ( $countries as $country ) {
// do nothing for current country if the related term already exists
// see http://codex.wordpress.org/Function_Reference/term_exists
if ( term_exists( sanitize_title( $country ), \'category\', $parent->term_id ) ) {
continue;
}
// insert the country term using continent as parent
wp_insert_term( $country, \'category\', array( \'parent\' => $parent->term_id ) );
}
}
当然,这是一个资源非常昂贵的代码,即使代码进行了检查以避免多次添加术语,也应该确保此代码只运行一次,以避免严重的性能问题。要设置一次运行脚本,请查看this Q/A.
我正在使用基于自定义帖子类型的过滤器制作一个公文包。该过滤器必须只显示公文包中显示的帖子的类别,因为用户可以在短代码中通过id指定它们-[公文包id=“1,2,3”],而我无法获得该类别id的列表。下面是一个简单的例子,说明我正在尝试做什么:来自快捷码的自定义帖子ID列表:$ids 相同ID的数组:$id_array = explode(\',\', $ids) 必须返回类别ID列表的感兴趣的变量:$cat_ids = ??? 接下来,我们只获取具有所需id的类别:$ca