内链是网站内部页面之间的相互链接,用于优化结构、传递权重和提升用户体验。
首先确保已安装Advanced Custom Fields插件。
在你的主题的functions.php文件中添加以下代码:
functions.php
// 注册内链词库自定义文章类型 function register_internal_links_cpt() { $args = array( 'public' => true, 'label' => '内链词库', 'menu_icon' => 'dashicons-admin-links', 'supports' => array('title'), 'show_in_menu' => true, 'capability_type' => 'post', 'exclude_from_search' => true, 'publicly_queryable' => false, 'show_in_nav_menus' => false, ); register_post_type('internal_links', $args); } add_action('init', 'register_internal_links_cpt'); // 使用ACF创建内链字段 if(function_exists('acf_add_local_field_group')) { acf_add_local_field_group(array( 'key' => 'group_internal_links', 'title' => '内链设置', 'fields' => array( array( 'key' => 'field_keyword', 'label' => '关键词', 'name' => 'keyword', 'type' => 'text', 'instructions' => '输入需要添加内链的关键词', 'required' => 1, ), array( 'key' => 'field_link_url', 'label' => '链接URL', 'name' => 'link_url', 'type' => 'url', 'instructions' => '输入关键词对应的链接地址', 'required' => 1, ), array( 'key' => 'field_max_links', 'label' => '最大链接数', 'name' => 'max_links', 'type' => 'number', 'instructions' => '每篇文章中该关键词最多添加几次链接(0表示不限制)', 'min' => 0, 'default_value' => 1, ), array( 'key' => 'field_open_new_tab', 'label' => '新标签页打开', 'name' => 'open_new_tab', 'type' => 'true_false', 'instructions' => '是否在新标签页打开链接', 'default_value' => 0, ) ), 'location' => array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'internal_links', ), ), ), )); }
在functions.php中添加以下代码:
// 自动链接替换功能 function auto_link_keywords($content) { // 如果是后台或feed,直接返回内容 if(is_admin() || is_feed()) return $content; // 获取所有内链设置 $args = array( 'post_type' => 'internal_links', 'posts_per_page' => -1, 'post_status' => 'publish' ); $links = get_posts($args); if(empty($links)) return $content; // 按关键词长度排序,先替换长关键词 usort($links, function($a, $b) { $len_a = strlen(get_field('keyword', $a->ID)); $len_b = strlen(get_field('keyword', $b->ID)); return $len_b - $len_a; }); foreach($links as $link) { $keyword = get_field('keyword', $link->ID); $url = get_field('link_url', $link->ID); $max_links = get_field('max_links', $link->ID); $new_tab = get_field('open_new_tab', $link->ID); if(empty($keyword) || empty($url)) continue; // 准备替换参数 $target = $new_tab ? ' target="_blank"' : ''; $replace_count = is_numeric($max_links) ? $max_links : 1; // 使用正则表达式进行替换 $pattern = '/\b(' . preg_quote($keyword, '/') . ')\b/iu'; // 检查内容中是否包含关键词 if(preg_match($pattern, $content)) { $replacement = '<a href="' . esc_url($url) . '" title="' . esc_attr($keyword) . '"' . $target . '>$1</a>'; // 限制替换次数 if($replace_count > 0) { $content = preg_replace_callback( $pattern, function($matches) use ($url, $keyword, $target, &$replace_count) { if($replace_count > 0) { $replace_count--; return '<a href="' . esc_url($url) . '" title="' . esc_attr($keyword) . '"' . $target . '>' . $matches[1] . '</a>'; } return $matches[0]; }, $content, $replace_count ); } else { $content = preg_replace($pattern, $replacement, $content); } } } return $content; } add_filter('the_content', 'auto_link_keywords', 999); // 高优先级确保最后执行
为了更方便地在后台管理内链,可以添加自定义列:
// 添加内链列表自定义列 function add_internal_links_columns($columns) { $new_columns = array( 'cb' => $columns['cb'], 'title' => $columns['title'], 'keyword' => '关键词', 'link_url' => '链接URL', 'date' => $columns['date'] ); return $new_columns; } add_filter('manage_internal_links_posts_columns', 'add_internal_links_columns'); // 显示内链列表内容 function display_internal_links_columns($column, $post_id) { switch($column) { case 'keyword': echo get_field('keyword', $post_id); break; case 'link_url': echo '<a href="' . get_field('link_url', $post_id) . '" target="_blank">' . get_field('link_url', $post_id) . '</a>'; break; } } add_action('manage_internal_links_posts_custom_column', 'display_internal_links_columns', 10, 2);
add_filter
这个方案提供了完整的后台管理界面,让你可以轻松添加和管理内链规则,同时保持代码的高效性和灵活性。