/**
* Gangwan Zhunong - SEO Enhancement Module
* 港湾助农平台 SEO 增强模块
* Open Graph + Twitter Card + 面包屑导航
*/
// ============================================
// 1. Open Graph Meta Tags(微信/微博/朋友圈)
// ============================================
function gw_add_open_graph_tags() {
// 仅在非管理员页面输出
if ( is_admin() ) {
return;
}
$site_name = get_bloginfo( 'name' );
$title = '';
$description = '';
$image = '';
$url = get_permalink();
$type = 'website';
if ( is_front_page() ) {
$title = $site_name . ' - 乡村振兴大宗采购平台';
$description = '港湾助农响应乡村振兴,源头直供野生蜂蜜、手工红糖、时令农产品,专注政企福利采购。';
$image = get_theme_file_uri( '/assets/images/og-home.jpg' );
$url = home_url( '/' );
} elseif ( is_singular( 'product' ) ) {
$title = get_the_title() . ' - ' . $site_name;
$description = wp_trim_words( get_the_excerpt(), 30, '...' );
$image = get_the_post_thumbnail_url( get_the_ID(), 'large' );
$type = 'product';
} elseif ( is_post_type_archive( 'product' ) ) {
$title = '助农产品中心 - ' . $site_name;
$description = '浏览所有源头直供助农产品,野生蜂蜜、手工红糖、时令农产品,政企大宗福利采购首选。';
$image = get_theme_file_uri( '/assets/images/og-products.jpg' );
} elseif ( is_author() ) {
$author_id = get_queried_object_id();
$author_name = get_the_author_meta( 'display_name', $author_id );
$title = $author_name . ' 的助农产品 - ' . $site_name;
$description = '查看 ' . $author_name . ' 发布的所有助农产品,源头直供,品质保障。';
$image = get_avatar_url( $author_id, array( 'size' => 400 ) );
$type = 'profile';
} else {
$title = wp_get_document_title();
$description = get_bloginfo( 'description' );
$image = get_theme_file_uri( '/assets/images/og-default.jpg' );
}
// 输出 Open Graph 标签
echo "\n\n";
echo ' ' . "\n";
echo ' ' . "\n";
echo ' ' . "\n";
echo ' ' . "\n";
echo ' ' . "\n";
if ( $image ) {
echo ' ' . "\n";
}
// 产品页额外信息
if ( is_singular( 'product' ) ) {
$price = get_post_meta( get_the_ID(), '_price', true );
if ( $price ) {
$unit = get_post_meta( get_the_ID(), '_unit', true );
echo ' ' . "\n";
echo ' ' . "\n";
}
}
}
add_action( 'wp_head', 'gw_add_open_graph_tags', 5 );
// ============================================
// 2. Twitter Card Meta Tags
// ============================================
function gw_add_twitter_card_tags() {
if ( is_admin() ) {
return;
}
$title = wp_get_document_title();
$description = get_bloginfo( 'description' );
$image = get_theme_file_uri( '/assets/images/twitter-card.jpg' );
if ( is_singular( 'product' ) ) {
$image = get_the_post_thumbnail_url( get_the_ID(), 'large' );
$description = wp_trim_words( get_the_excerpt(), 30, '...' );
}
echo "\n\n";
echo ' ' . "\n";
echo ' ' . "\n";
echo ' ' . "\n";
if ( $image ) {
echo ' ' . "\n";
}
}
add_action( 'wp_head', 'gw_add_twitter_card_tags', 6 );
// ============================================
// 3. 面包屑导航函数
// ============================================
function gw_breadcrumb( $echo = true ) {
$breadcrumb = array();
// 首页链接
$breadcrumb[] = '🌾 首页 ';
if ( is_singular( 'product' ) ) {
// 产品归档
$breadcrumb[] = '助农产品 ';
// 产品分类
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term = array_shift( $terms );
$breadcrumb[] = '' . esc_html( $term->name ) . ' ';
}
// 当前产品
$breadcrumb[] = '' . get_the_title() . ' ';
} elseif ( is_post_type_archive( 'product' ) ) {
$breadcrumb[] = '助农产品 ';
} elseif ( is_tax( 'product_cat' ) ) {
$breadcrumb[] = '助农产品 ';
$breadcrumb[] = '' . single_term_title( '', false ) . ' ';
} elseif ( is_author() ) {
$breadcrumb[] = '助农产品 ';
$breadcrumb[] = '' . get_the_author() . ' 的产品 ';
} elseif ( is_404() ) {
$breadcrumb[] = '页面未找到 ';
} elseif ( is_search() ) {
$breadcrumb[] = '搜索: ' . get_search_query() . ' ';
}
// 拼接 HTML
$html = '';
foreach ( $breadcrumb as $index => $item ) {
if ( $index < count( $breadcrumb ) - 1 ) {
$html .= '' . $item . ' › ';
} else {
$html .= '' . $item . ' ';
}
}
$html .= ' ';
if ( $echo ) {
echo $html;
}
return $html;
}
// ============================================
// 4. 面包屑 JSON-LD(SEO 结构化数据)
// ============================================
function gw_breadcrumb_json_ld() {
if ( is_admin() ) {
return;
}
$items = array();
$position = 1;
// 首页
$items[] = array(
'@type' => 'ListItem',
'position' => $position,
'name' => '首页',
'item' => home_url( '/' )
);
$position++;
if ( is_singular( 'product' ) ) {
// 产品归档
$items[] = array(
'@type' => 'ListItem',
'position' => $position,
'name' => '助农产品',
'item' => get_post_type_archive_link( 'product' )
);
$position++;
// 产品分类
$terms = get_the_terms( get_the_ID(), 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term = array_shift( $terms );
$items[] = array(
'@type' => 'ListItem',
'position' => $position,
'name' => $term->name,
'item' => get_term_link( $term )
);
$position++;
}
// 当前产品
$items[] = array(
'@type' => 'ListItem',
'position' => $position,
'name' => get_the_title(),
'item' => get_permalink()
);
}
if ( count( $items ) > 1 ) {
$json_ld = array(
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => $items
);
echo "\n\n";
}
}
add_action( 'wp_head', 'gw_breadcrumb_json_ld', 7 );
// ============================================
// 5. Canonical URL(避免重复内容)
// ============================================
function gw_add_canonical_url() {
if ( is_singular( 'product' ) ) {
echo ' ' . "\n";
} elseif ( is_post_type_archive( 'product' ) ) {
echo ' ' . "\n";
}
}
add_action( 'wp_head', 'gw_add_canonical_url', 1 );
// ============================================
// 6. 禁止搜索引擎索引敏感页面
// ============================================
function gw_noindex_sensitive_pages() {
if ( is_author() || is_search() || is_404() ) {
echo ' ' . "\n";
}
}
add_action( 'wp_head', 'gw_noindex_sensitive_pages', 0 );
// ============================================
// 7. 农户认证等级徽章
// ============================================
function gw_get_farmer_badge( $user_id = null ) {
if ( ! $user_id ) {
$user_id = get_the_author_meta( 'ID' );
}
$status = get_user_meta( $user_id, 'farmer_status', true );
$level = get_user_meta( $user_id, 'farmer_level', true ); // bronze/silver/gold
if ( $status !== 'approved' ) {
return '';
}
$badges = array(
'bronze' => array(
'icon' => '🥉',
'label' => '签约农户',
'color' => '#cd7f32'
),
'silver' => array(
'icon' => '🥈',
'label' => '优质农户',
'color' => '#c0c0c0'
),
'gold' => array(
'icon' => '🥇',
'label' => '金牌农户',
'color' => '#ffd700'
)
);
$badge = $badges[ $level ] ?? $badges['bronze'];
return sprintf(
'%s %s ',
esc_attr( $badge['color'] ),
$badge['icon'],
esc_html( $badge['label'] )
);
}
// ============================================
// 8. 产品评分聚合(预留接口)
// ============================================
function gw_get_product_rating( $post_id = null ) {
if ( ! $post_id ) {
$post_id = get_the_ID();
}
// 预留:从评论系统计算评分
$rating_value = get_post_meta( $post_id, '_avg_rating', true );
$rating_count = get_post_meta( $post_id, '_rating_count', true );
if ( ! $rating_value || ! $rating_count ) {
return false;
}
return array(
'value' => floatval( $rating_value ),
'count' => intval( $rating_count )
);
}
// ============================================
// 9. 产品评分 JSON-LD 注入
// ============================================
function gw_product_rating_json_ld() {
if ( ! is_singular( 'product' ) ) {
return;
}
$rating = gw_get_product_rating();
if ( ! $rating ) {
return;
}
$json_ld = array(
'@context' => 'https://schema.org',
'@type' => 'Product',
'name' => get_the_title(),
'aggregateRating' => array(
'@type' => 'AggregateRating',
'ratingValue' => $rating['value'],
'reviewCount' => $rating['count'],
'bestRating' => 5,
'worstRating' => 1
)
);
echo "\n\n";
}
add_action( 'wp_head', 'gw_product_rating_json_ld', 8 );
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-content\themes\zhunong\functions.php on line 14
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-content\themes\zhunong\functions.php on line 16
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-content\themes\zhunong\functions.php on line 18
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-content\themes\zhunong\functions.php on line 20
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-content\themes\zhunong\functions.php on line 22
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-includes\pluggable.php on line 1535
Warning : Cannot modify header information - headers already sent by (output started at C:\wwwroot\gangwan\wp-content\themes\zhunong\inc\seo-enhancements.php:1) in C:\wwwroot\gangwan\wp-includes\pluggable.php on line 1538