我在前端工具栏上生成了一个链接,以显示一篇文章(添加了“特色”标签)。添加或删除都可以,但问题是显示帖子。当删除标记,然后在模板上使用\\u tags()或has\\u tags()时,帖子似乎仍然有它;我必须刷新页面才能看到结果。
但是当添加标记时,一切都按预期进行:添加了术语,并用简单的clic显示标记。
我做错了什么?可能是动作钩?是否存在任何类型的缓存?
function toolbar_add_link( $wp_admin_bar )
{
if ( is_single() )
{
$title = \'\';
$url = \'\';
if ( has_tag( \'featured\' ) )
{
$title = \'<span style="top: 2px;" class="ab-icon dashicons-heart"></span> \' . __( \'No destacar\', \'the_textdomain\' );
$url = wp_nonce_url( add_query_arg( \'the_action\', \'remove_feature_post\' ), \'prefix-remove_feature_post\' );
}
else
{
$title = \'<span style="top: 2px;" class="ab-icon dashicons-heart"></span> \' . __( \'Destacar evento\', \'the_textdomain\' );
$url = wp_nonce_url( add_query_arg( \'the_action\', \'add_feature_post\' ), \'prefix-add_feature_post\' );
}
$args = array(
\'id\' => \'feature-post\',
\'title\' => $title,
\'href\' => $url,
\'meta\' => array(
\'class\' => \'dashicons-edit\'
),
);
$wp_admin_bar->add_node( $args );
}
}
add_action( \'admin_bar_menu\', \'toolbar_add_link\', 999 );
function add_remove_tags()
{
global $post;
$action = isset( $_GET[\'the_action\'] ) ? $_GET[\'the_action\'] : \'\';
$wpnonce_action = \'prefix-\' . $action;
if ( ! ( isset( $_GET[\'_wpnonce\'] ) && wp_verify_nonce( $_GET[\'_wpnonce\'], $wpnonce_action ) && ( current_user_can( \'editor\' ) || current_user_can( \'administrator\' ) ) ) )
{
//echo \'invalid nonce\';
return;
}
$term = term_exists( \'featured\', \'post_tag \');
$tag_id = null;
if ( is_array( $term ) )
{
$tag_id = (int) $term[\'term_id\'];
}
/*
* If this was coming from the database or another source, we would need to make sure
* these where integers:
$cat_ids = array_map( \'intval\', $cat_ids );
$cat_ids = array_unique( $cat_ids );
*/
if ( $_GET[\'the_action\'] === \'add_feature_post\' )
{
wp_add_object_terms( $post->ID, $tag_id, \'post_tag\' );
}
elseif ( $_GET[\'the_action\'] === \'remove_feature_post\' )
{
wp_remove_object_terms( $post->ID, $tag_id, \'post_tag\' );
}
}
add_action( \'wp\', \'add_remove_tags\' );