以编程方式在前端添加/删除标签

时间:2014-11-14 作者:alekstrust

我在前端工具栏上生成了一个链接,以显示一篇文章(添加了“特色”标签)。添加或删除都可以,但问题是显示帖子。当删除标记,然后在模板上使用\\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\' );

1 个回复
SO网友:alekstrust

在中找到解决方案https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/taxonomy.php#L0

wp\\u add\\u object\\u terms()使用wp\\u cache\\u delete(),但wp\\u remove\\u object\\u terms()不使用。只添加了以下内容:

wp_remove_object_terms( $post->ID, $tag_id, \'post_tag\' );
wp_cache_delete( $object_id, \'post_tag_relationships\' );

结束

相关推荐

插件管理菜单中的EDIT-TAGS.php在活动页面时隐藏

对于我正在构建的插件,我在admin中的自定义主菜单项中添加了一个子菜单作为子菜单。子菜单页是edit-tags.php 页这将按预期显示在主菜单项下。单击此子菜单项时,用户将进入正确的页面,但主菜单项会向下折叠,隐藏当前打开的子菜单项。我最初的感觉是父slug属性是错误的,但我添加的其他子菜单使用相同的父slug属性,并且它们工作正常。很明显,我在add_submenu_page() 函数,但我不知道是什么。下面是我注册子菜单的方式:$this->plugin_screen_dashboard =