你可以写一个 PHP 函数,根据用户行为和内容特征生成推荐列表,然后通过短代码显示。
// 模拟概率权重函数
function ai_probability_score($user_interest, $content_popularity){
$alpha = 0.7;
$beta = 0.3;
return $alpha * $user_interest + $beta * $content_popularity;
}
// 获取推荐内容
function ai_feed_recommend($atts){
$atts = shortcode_atts(array(
‘top’ => 3, // Top-N
), $atts);
// 模拟用户兴趣
$user_interest = array(
‘sports’ => 0.8,
‘tech’ => 0.5
);
// 获取最新文章
$args = array(
‘posts_per_page’ => 10,
‘post_status’ => ‘publish’
);
$posts = get_posts($args);
$scores = array();
foreach($posts as $post){
// 模拟内容特征:使用分类热度(可自己定义)
$categories = wp_get_post_categories($post->ID);
$popularity = 0.7; // 可以改成真实数据,如阅读量
$category_name = get_cat_name($categories[0]);
$interest_score = isset($user_interest[strtolower($category_name)]) ? $user_interest[strtolower($category_name)] : 0.5;
$score = ai_probability_score($interest_score, $popularity);
$scores[] = array(‘post_id’=>$post->ID, ‘score’=>$score);
}
// 按概率排序
usort($scores, function($a, $b){ return $b[‘score’] <=> $a[‘score’]; });
// 输出 Top-N
$output = ‘<ul class=”ai-feed”>’;
for($i=0; $i<$atts[‘top’] && $i<count($scores); $i++){
$post_id = $scores[$i][‘post_id’];
$title = get_the_title($post_id);
$link = get_permalink($post_id);
$output .= “<li><a href=’$link‘>$title</a></li>”;
}
$output .= ‘</ul>’;
return $output;
}
// 注册短代码
add_shortcode(‘ai_feed’, ‘ai_feed_recommend’);
?>
使用方法:
这个短代码会在文章或页面显示 Top-5 推荐内容。