Products
GG网络技术分享 2025-03-18 16:15 0
在主题的search.php中添加以下代码,注意是在get_search_query()
函数之间添加。
//用户搜索词自动添加为标签,并自动关联前10篇文章$query = get_search_query(); $tag = sanitize_title( $query ); $tag_id = wp_insert_term( $query, 'post_tag', array( 'slug' => $tag )); $args = array( 's' => $query, 'post_type' => 'post', 'posts_per_page' => 10, ); $query = new WP_Query( $args ); while ( $query->have_posts() ) { $query->the_post(); wp_set_post_tags( get_the_ID(), $tag_id, true ); } wp_reset_postdata();
此代码片段用于在 WordPress 中执行以下功能:
$query
中。$tag
中。$tag_id
变量中。search.php
文件中的 get_search_form()
和 get_search_query()
函数之间添加,以确保正确处理查询字符串。
Demand feedback