Wordpress - 如何自动将标题中的单词插入标签

时间:2017-04-08 10:21:37

标签: wordpress

我想在帖子的标题中自动插入单词,并以标签中的#开头。并使用此功能。但是当我发布帖子时,所有单词都插入了。

function convert_to_tag( $post_id ) {
    $post       = get_post( $post_id );
    $tax        = isset( $types[$post->post_type] ) ? $types[$post->post_type] : 'post_tag';
    if( isset( $post->post_title ) ) :
        $title_words    = explode( ' ', $post->post_title );
        foreach( $title_words as $word ) :
            if (0 == strpos($word, '#')) :
                wp_insert_term(
                    $word,
                    $tax,
                    array()
                );
            endif;
        endforeach;
    endif;
}
add_action( 'save_post', 'convert_to_tag' );

0 个答案:

没有答案