Php wordpress-保存后自动插入术语slug作为标记
更新,代码正在运行 我有下面的代码,但它并没有像预期的那样工作。它应该采用分类法分类法名称中的术语,仅适用于自定义文章类型自定义文章类型和添加为标记Php wordpress-保存后自动插入术语slug作为标记,php,wordpress,Php,Wordpress,更新,代码正在运行 我有下面的代码,但它并没有像预期的那样工作。它应该采用分类法分类法名称中的术语,仅适用于自定义文章类型自定义文章类型和添加为标记 这是未经测试的,但它应该是朝着正确方向迈出的一步,我已经做了一些调整,并将发表评论: <?php add_action('save_post','add_tags_auto'); function add_tags_auto($post_id) { $post = get_post($post_id); // bail i
这是未经测试的,但它应该是朝着正确方向迈出的一步,我已经做了一些调整,并将发表评论:
<?php
add_action('save_post','add_tags_auto');
function add_tags_auto($post_id) {
$post = get_post($post_id);
// bail if this isn't a type of post we want to auto-tag
if($post->post_type != 'CUSTOM_POST_TYPE')
return false;
// --------------------------------------------------
// get the list of tag names from the post
// --------------------------------------------------
$tagNames = array();
$tags = get_the_tags($post_id);
foreach($tags as $tag)
$tagNames[] = $tag->name;
// --------------------------------------------------
// get the list slugs from terms in the post, any
// slugs that aren't alredy a tag, will be marked for
// addition automatically
// --------------------------------------------------
$tagsToAdd = array();
foreach(get_the_terms($post_id, 'TAXONOMY_NAME') as $term)
if(!in_array($term->slug, $tagNames))
$tagsToAdd[] = $term->slug;
// if we have some new tags to add, let's do that now
if(!empty($tagsToAdd))
wp_add_post_tags($post_id, implode(',', $tagsToAdd));
}
是否有限制标签数量的方法,您可以退出调用$tagsToAdd[]=$term->slug的foreach代码>。假设您有一个变量,称它为$iMaxTags
(例如,您可以从数据库中填充它),如果($i<$iMaxTags&&!in_array($term->slug,$tagNames)),循环中的条件将变为;然后还记得将foreach更改为为$i=>$term
。但是,请注意,这是一种相当粗糙的限制方法。一个实用的解决方案可能会尝试以某种方式更智能地过滤它们。
<?php
add_action('save_post','add_tags_auto');
function add_tags_auto($post_id) {
$post = get_post($post_id);
// bail if this isn't a type of post we want to auto-tag
if($post->post_type != 'CUSTOM_POST_TYPE')
return false;
// --------------------------------------------------
// get the list of tag names from the post
// --------------------------------------------------
$tagNames = array();
$tags = get_the_tags($post_id);
foreach($tags as $tag)
$tagNames[] = $tag->name;
// --------------------------------------------------
// get the list slugs from terms in the post, any
// slugs that aren't alredy a tag, will be marked for
// addition automatically
// --------------------------------------------------
$tagsToAdd = array();
foreach(get_the_terms($post_id, 'TAXONOMY_NAME') as $term)
if(!in_array($term->slug, $tagNames))
$tagsToAdd[] = $term->slug;
// if we have some new tags to add, let's do that now
if(!empty($tagsToAdd))
wp_add_post_tags($post_id, implode(',', $tagsToAdd));
}