求助:WordPress(Divi)自定义文章类型阅读更多跳转指定链接
解决Divi中自定义文章类型“阅读更多”按钮跳转自定义链接的问题
1. 配置自定义文章类型与自定义链接字段
假设你的自定义文章类型名为external_link_post,先确保已为它添加存储外部链接的自定义字段(这里用代码实现,也可替换为ACF插件)。将以下代码添加到主题的functions.php中:
// 注册自定义文章类型 function create_external_link_cpt() { register_post_type('external_link_post', array( 'labels' => array( 'name' => __('外部链接文章'), 'singular_name' => __('外部链接文章') ), 'public' => true, 'has_archive' => true, 'supports' => array('title', 'date'), // 仅启用标题、日期字段 'rewrite' => array('slug' => 'external-links'), ) ); } add_action('init', 'create_external_link_cpt'); // 添加自定义链接元框 function add_external_link_meta_box() { add_meta_box( 'external_post_link_meta', '外部跳转链接', 'render_external_link_meta_box', 'external_link_post', 'normal', 'high' ); } add_action('add_meta_boxes', 'add_external_link_meta_box'); // 渲染链接输入框 function render_external_link_meta_box($post) { $value = get_post_meta($post->ID, 'external_post_link', true); echo '<input type="url" name="external_post_link" value="' . esc_attr($value) . '" style="width:100%; padding:8px;" placeholder="输入跳转链接">'; } // 保存自定义链接 function save_external_link_meta($post_id) { if (array_key_exists('external_post_link', $_POST)) { update_post_meta( $post_id, 'external_post_link', esc_url_raw($_POST['external_post_link']) ); } } add_action('save_post', 'save_external_link_meta');
2. 替换Divi“阅读更多”按钮的跳转目标
继续在functions.php中添加过滤器代码,仅针对目标自定义文章类型替换链接:
function custom_divi_read_more_link($url, $post) { // 判断当前文章是否为目标自定义类型 if ($post->post_type === 'external_link_post') { $external_link = get_post_meta($post->ID, 'external_post_link', true); // 存在自定义链接则替换默认地址 if (!empty($external_link)) { return $external_link; } } // 其他情况返回默认永久链接 return $url; } add_filter('et_pb_post_read_more_link_url', 'custom_divi_read_more_link', 10, 2);
3. 前端模块设置
在Divi编辑器中添加「Blog」模块,做如下配置:
- 文章类型选择你的自定义文章类型(如「外部链接文章」)
- 显示数量设为5
- 按需隐藏内容摘要,只保留标题、日期和「阅读更多」按钮
完成后,该模块的「阅读更多」按钮就会跳转到你在自定义文章中填写的外部链接,而非文章永久链接。
内容的提问来源于stack exchange,提问作者Nash
相关产品推荐
相关产品推荐

