You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress的Yoku-child子主题中自定义LearnPress插件

将LearnPress自定义迁移到子主题的解决方案

1. 模板文件重写(覆盖插件默认模板)

LearnPress支持WordPress原生的模板重写机制,无需修改插件核心文件:

  • 找到plugins/learnpress/templates目录下你已修改的模板(比如single-course.php、loop/course.php等)
  • 在子主题Yoku-child根目录创建learnpress文件夹,严格按照插件内的目录结构复制模板文件。例如插件内路径是templates/single-course.php,子主题内就对应放Yoku-child/learnpress/single-course.php
  • 后续插件更新时,子主题里的模板会优先加载,不会被覆盖

2. 功能逻辑迁移(用钩子替代核心修改)

如果之前改了插件的PHP功能代码,全部改用LearnPress提供的动作(Action)和过滤器(Filter)钩子实现,写在子主题的functions.php里:

  • 示例:修改课程标题显示逻辑
add_filter( 'learn_press_course_title', 'yoku_child_custom_course_title', 10, 2 );
function yoku_child_custom_course_title( $title, $course ) {
    // 这里写入你的自定义逻辑,比如给标题添加前缀
    return '[专属课程] ' . $title;
}
  • 若自定义代码较多,可在子主题创建inc/learnpress-custom.php单独存放,再在functions.php引入:
// 加载子主题LearnPress自定义功能文件
require_once get_stylesheet_directory() . '/inc/learnpress-custom.php';

3. 样式覆盖(替换插件CSS)

修改插件样式直接在子主题里处理,无需改动插件CSS文件:

  • 直接在子主题style.css中写入针对LearnPress元素的样式,比如调整课程卡片样式:
.learn-press-course .course-title {
    font-size: 18px;
    color: #2c3e50;
    font-weight: 600;
}
  • 若需要单独管理样式文件,可在子主题创建css/learnpress-custom.css,然后在functions.php注册加载:
add_action( 'wp_enqueue_scripts', 'yoku_child_enqueue_lp_styles' );
function yoku_child_enqueue_lp_styles() {
    // 依赖插件前端样式,确保自定义样式优先级更高
    wp_enqueue_style( 'yoku-child-lp', get_stylesheet_directory_uri() . '/css/learnpress-custom.css', array( 'learn-press-frontend' ), wp_get_theme()->get('Version') );
}

4. 自定义页面/短码迁移

如果之前做了自定义页面或短码,直接放到子主题中:

  • 自定义页面模板:在子主题创建template-lp-custom.php,开头添加模板标识:
<?php
/*
Template Name: 自定义课程列表页
*/
// 这里写入你的模板代码,可调用LearnPress的课程查询函数
?>
  • 自定义短码:在子主题functions.php注册,比如创建自定义课程列表短码:
add_shortcode( 'yoku_custom_course_grid', 'yoku_child_custom_course_grid' );
function yoku_child_custom_course_grid() {
    $courses = learn_press_get_courses( array( 'posts_per_page' => 8 ) );
    ob_start();
    foreach ( $courses as $course ) {
        echo '<div class="custom-course-grid-item">' . get_the_title( $course->ID ) . '</div>';
    }
    return ob_get_clean();
}

内容的提问来源于stack exchange,提问作者amulraj WIlliam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 08:40:31