在内容运营中,我们经常遇到这样的需求:将一系列相关文章组织起来,形成一个完整的知识体系或内容专题。比如:
WordPress默认的分类和标签无法满足这种结构化内容聚合的需求。本文将带你从零构建一个强大的WordPress专题系统。
// 专题自定义分类法
function register_feature_taxonomy() {
$labels = array(
'name' => '专题',
'singular_name' => '专题',
'menu_name' => '专题管理'
);
$args = array(
'hierarchical' => true, // 支持父子层级
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'feature'),
'show_in_rest' => true, // 支持Gutenberg编辑器
);
register_taxonomy('feature', array('post'), $args);
}
add_action('init', 'register_feature_taxonomy');// 为专题添加自定义字段
function add_feature_meta_fields() {
register_term_meta('feature', 'feature_cover', array(
'type' => 'string',
'description' => '专题封面图',
'single' => true,
'show_in_rest' => true
));
register_term_meta('feature', 'feature_status', array(
'type' => 'string',
'description' => '专题状态',
'single' => true,
'show_in_rest' => true
));
register_term_meta('feature', 'feature_order', array(
'type' => 'integer',
'description' => '文章排序序号',
'single' => true,
'show_in_rest' => true
));
}
add_action('init', 'add_feature_meta_fields');class Feature_Manager {
/**
* 添加文章到专题
*/
public static function add_post_to_feature($post_id, $feature_id, $order = 0) {
// 设置文章到专题
$result = wp_set_post_terms($post_id, array($feature_id), 'feature', true);
if (!is_wp_error($result)) {
// 保存排序信息
update_post_meta($post_id, '_feature_order_'.$feature_id, $order);
return true;
}
return false;
}
/**
* 获取专题文章列表(按自定义排序)
*/
public static function get_feature_posts($feature_id, $args = array()) {
$default_args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'feature',
'field' => 'term_id',
'terms' => $feature_id
)
),
'meta_key' => '_feature_order_'.$feature_id,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1
);
$final_args = wp_parse_args($args, $default_args);
return get_posts($final_args);
}
/**
* 计算专题完成进度
*/
public static function calculate_progress($feature_id) {
$feature = get_term($feature_id, 'feature');
$total_posts = $feature->count;
// 获取已发布的专题文章数量
$published_posts = get_posts(array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'feature',
'field' => 'term_id',
'terms' => $feature_id
)
),
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1
));
$published_count = count($published_posts);
return $total_posts > 0 ? round(($published_count / $total_posts) * 100) : 0;
}
}// 专题归档页面模板
function feature_archive_template($template) {
if (is_tax('feature')) {
$new_template = locate_template(array('taxonomy-feature.php'));
if (!empty($new_template)) {
return $new_template;
}
}
return $template;
}
add_filter('template_include', 'feature_archive_template');
// 专题页面内容展示
function display_feature_content($feature_id) {
$feature = get_term($feature_id, 'feature');
$cover_url = get_term_meta($feature_id, 'feature_cover', true);
$progress = Feature_Manager::calculate_progress($feature_id);
$posts = Feature_Manager::get_feature_posts($feature_id);
ob_start();
?>
<div class="feature-header">
<?php if ($cover_url): ?>
<div class="feature-cover">
<img src="<?php echo esc_url($cover_url); ?>" alt="<?php echo esc_attr($feature->name); ?>">
</div>
<?php endif; ?>
<div class="feature-info">
<h1><?php echo esc_html($feature->name); ?></h1>
<div class="feature-description">
<?php echo wp_kses_post($feature->description); ?>
</div>
<div class="feature-progress">
<div class="progress-bar">
<div class="progress-fill" style="width: <?php echo esc_attr($progress); ?>%"></div>
</div>
<span class="progress-text">完成度:<?php echo esc_html($progress); ?>%</span>
</div>
</div>
</div>
<div class="feature-posts">
<?php foreach ($posts as $post): ?>
<article class="feature-post">
<h2><a href="<?php the_permalink($post->ID); ?>"><?php echo esc_html($post->post_title); ?></a></h2>
<div class="post-meta">
<time datetime="<?php echo get_the_date('c', $post->ID); ?>">
<?php echo get_the_date('', $post->ID); ?>
</time>
</div>
</article>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}// 在专题列表中添加自定义列 https://www.qqe9.com/
function add_feature_custom_columns($columns) {
$new_columns = array();
foreach ($columns as $key => $value) {
$new_columns[$key] = $value;
if ($key === 'name') {
$new_columns['feature_cover'] = '封面图';
$new_columns['feature_progress'] = '完成进度';
$new_columns['post_count'] = '文章数';
}
}
return $new_columns;
}
add_filter('manage_edit-feature_columns', 'add_feature_custom_columns');
// 填充自定义列数据
function manage_feature_custom_columns($content, $column_name, $term_id) {
switch ($column_name) {
case 'feature_cover':
$cover_url = get_term_meta($term_id, 'feature_cover', true);
if ($cover_url) {
return '<img src="'.esc_url($cover_url).'" style="width: 60px; height: 40px; object-fit: cover;">';
}
return '无封面';
case 'feature_progress':
$progress = Feature_Manager::calculate_progress($term_id);
return '<div class="progress-bar"><div style="width:'.esc_attr($progress).'%"></div></div>'.esc_html($progress).'%';
case 'post_count':
$term = get_term($term_id, 'feature');
return $term->count;
default:
return $content;
}
}
add_filter('manage_feature_custom_column', 'manage_feature_custom_columns', 10, 3);// 在专题编辑页面添加自定义字段
function feature_add_custom_fields($term) {
$cover_url = get_term_meta($term->term_id, 'feature_cover', true);
$status = get_term_meta($term->term_id, 'feature_status', true);
?>
<tr class="form-field">
<th scope="row">
<label for="feature_cover">专题封面图</label>
</th>
<td>
<input type="url" name="feature_cover" id="feature_cover" value="<?php echo esc_attr($cover_url); ?>">
<button type="button" class="button feature-upload-cover">选择图片</button>
<p class="description">建议尺寸:1200×400像素</p>
</td>
</tr>
<tr class="form-field">
<th scope="row">
<label for="feature_status">专题状态</label>
</th>
<td>
<select name="feature_status" id="feature_status">
<option value="planning" <?php selected($status, 'planning'); ?>>规划中</option>
<option value="ongoing" <?php selected($status, 'ongoing'); ?>>进行中</option>
<option value="completed" <?php selected($status, 'completed'); ?>>已完结</option>
</select>
</td>
</tr>
<?php
}
add_action('feature_edit_form_fields', 'feature_add_custom_fields');
add_action('feature_add_form_fields', 'feature_add_custom_fields');
// 保存自定义字段
function save_feature_custom_fields($term_id) {
if (isset($_POST['feature_cover'])) {
update_term_meta($term_id, 'feature_cover', sanitize_url($_POST['feature_cover']));
}
if (isset($_POST['feature_status'])) {
update_term_meta($term_id, 'feature_status', sanitize_text_field($_POST['feature_status']));
}
}
add_action('edited_feature', 'save_feature_custom_fields');
add_action('create_feature', 'save_feature_custom_fields');// 专题页面模板
function feature_page_template() {
if (is_tax('feature')) {
$feature_id = get_queried_object_id();
echo display_feature_content($feature_id);
}
}
// 专题小工具
class Feature_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'feature_widget',
'专题展示',
array('description' => '显示热门专题列表')
);
}
public function widget($args, $instance) {
$features = get_terms(array(
'taxonomy' => 'feature',
'hide_empty' => true,
'number' => $instance['number'] ?? 5,
'meta_query' => array(
array(
'key' => 'feature_status',
'value' => 'ongoing',
'compare' => '='
)
)
));
echo $args['before_widget'];
echo $args['before_title'] . '进行中专题' . $args['after_title'];
echo '<ul class="feature-widget-list">';
foreach ($features as $feature) {
$progress = Feature_Manager::calculate_progress($feature->term_id);
echo '<li>';
echo '<a href="'.get_term_link($feature).'">'.esc_html($feature->name).'</a>';
echo '<div class="progress">'.$progress.'%</div>';
echo '</li>';
}
echo '</ul>';
echo $args['after_widget'];
}
}
// 注册小工具
function register_feature_widget() {
register_widget('Feature_Widget');
}
add_action('widgets_init', 'register_feature_widget');// 专题数据缓存
class Feature_Cache {
public static function get_feature_data($feature_id) {
$cache_key = 'feature_data_' . $feature_id;
$cached_data = wp_cache_get($cache_key, 'features');
if (false === $cached_data) {
$feature_data = array(
'term' => get_term($feature_id, 'feature'),
'meta' => array(
'cover' => get_term_meta($feature_id, 'feature_cover', true),
'status' => get_term_meta($feature_id, 'feature_status', true)
),
'progress' => Feature_Manager::calculate_progress($feature_id),
'post_count' => Feature_Manager::get_feature_posts($feature_id, array('fields' => 'ids'))
);
wp_cache_set($cache_key, $feature_data, 'features', 12 * HOUR_IN_SECONDS);
return $feature_data;
}
return $cached_data;
}
}通过本文介绍的WordPress专题功能开发方案,你可以:
✅ 构建结构化的内容聚合系统
✅ 提供更好的用户体验和内容发现
✅ 增强网站的粘性和用户参与度
✅ 为内容营销提供强有力的技术支持
这个专题系统已经在我们多个客户网站中成功应用,显著提升了内容组织和用户留存效果。你可以根据实际需求进一步扩展功能,打造更适合自己业务的专题系统。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。