diff --git "a/blog/2024/03-26-\345\273\272\350\256\276\347\275\221\347\253\231\347\232\204\346\210\220\346\234\254\346\265\201\346\260\264\350\264\246.md" "b/blog/2024/03-26-\345\273\272\350\256\276\347\275\221\347\253\231\347\232\204\346\210\220\346\234\254\346\265\201\346\260\264\350\264\246.md" index ccfbf0325..b8fc8db77 100644 --- "a/blog/2024/03-26-\345\273\272\350\256\276\347\275\221\347\253\231\347\232\204\346\210\220\346\234\254\346\265\201\346\260\264\350\264\246.md" +++ "b/blog/2024/03-26-\345\273\272\350\256\276\347\275\221\347\253\231\347\232\204\346\210\220\346\234\254\346\265\201\346\260\264\350\264\246.md" @@ -8,6 +8,8 @@ slug: 网站建设成本 3. 购买elementor pro分销 100¥ 4. 腾讯云轻量服务器; +购买的全部插件都已作废不用,目前一切功能都自己便捷。其实也挺简单。 + diff --git "a/blog/2024/04-12-\347\246\201\346\255\242\346\212\230\350\205\276\357\274\201.md" "b/blog/2024/04-12-\347\246\201\346\255\242\346\212\230\350\205\276\357\274\201.md" index c401bc0fa..9d47b3d32 100644 --- "a/blog/2024/04-12-\347\246\201\346\255\242\346\212\230\350\205\276\357\274\201.md" +++ "b/blog/2024/04-12-\347\246\201\346\255\242\346\212\230\350\205\276\357\274\201.md" @@ -9,6 +9,6 @@ slug: 禁止折腾! 去掉spetra插件。用尽量少的插件来运营,以便提高网速。 - +可以在本地WordPress测试站上先随便折腾,满足折腾爱好。 diff --git "a/blog/2024/06-09-csdn\346\230\257\344\270\252\346\212\204\350\242\255\345\244\247\346\234\254\350\220\245.md" "b/blog/2024/06-09-csdn\346\230\257\344\270\252\346\212\204\350\242\255\345\244\247\346\234\254\350\220\245.md" deleted file mode 100644 index e28d18959..000000000 --- "a/blog/2024/06-09-csdn\346\230\257\344\270\252\346\212\204\350\242\255\345\244\247\346\234\254\350\220\245.md" +++ /dev/null @@ -1,10 +0,0 @@ ---- -date: 2024-06-09 22:57 -slug: csdn是个抄袭大本营 ---- - - - -我的原创文章被CSDN作者全文抄袭, - - diff --git "a/blog/2024/06-16-wordpress\345\205\250\347\253\231\347\246\201\346\255\242\351\274\240\346\240\207\345\244\215\345\210\266\346\226\207\345\255\227\345\222\214\351\274\240\346\240\207\345\217\263\351\224\256.md" "b/blog/2024/06-16-wordpress\345\205\250\347\253\231\347\246\201\346\255\242\351\274\240\346\240\207\345\244\215\345\210\266\346\226\207\345\255\227\345\222\214\351\274\240\346\240\207\345\217\263\351\224\256.md" index 3fae27807..c88b14cd4 100644 --- "a/blog/2024/06-16-wordpress\345\205\250\347\253\231\347\246\201\346\255\242\351\274\240\346\240\207\345\244\215\345\210\266\346\226\207\345\255\227\345\222\214\351\274\240\346\240\207\345\217\263\351\224\256.md" +++ "b/blog/2024/06-16-wordpress\345\205\250\347\253\231\347\246\201\346\255\242\351\274\240\346\240\207\345\244\215\345\210\266\346\226\207\345\255\227\345\222\214\351\274\240\346\240\207\345\217\263\351\224\256.md" @@ -46,7 +46,7 @@ add_action('wp_footer', 'disable_mouse_clicks'); --- -上面是复杂化的方法。其实很简单,被JS代码放入网站就好了。可以直接放进``里,用elementor的自定义代码,放进去也可以。 +上面是复杂化的方法。其实很简单,把JS代码放入网站就好了。可以直接放进``里,用elementor的自定义代码,放进去也可以。用Astra hook代码或者可视化来实现更简单 ## 对未登录用户禁用左右键和下载按钮 diff --git "a/blog/2024/06-17-\347\273\231WordPress\346\240\207\351\242\230\345\211\215\351\235\242\345\212\240\344\270\252\344\270\200\350\211\262\345\235\227.md" "b/blog/2024/06-17-\347\273\231WordPress\346\240\207\351\242\230\345\211\215\351\235\242\345\212\240\344\270\252\344\270\200\350\211\262\345\235\227.md" index 8c46a6d83..8cf5daea1 100644 --- "a/blog/2024/06-17-\347\273\231WordPress\346\240\207\351\242\230\345\211\215\351\235\242\345\212\240\344\270\252\344\270\200\350\211\262\345\235\227.md" +++ "b/blog/2024/06-17-\347\273\231WordPress\346\240\207\351\242\230\345\211\215\351\235\242\345\212\240\344\270\252\344\270\200\350\211\262\345\235\227.md" @@ -33,6 +33,7 @@ slug: 给WordPress标题前面加个一色块 ``` #### 2. 创建并编辑自定义JavaScript文件 + 在您的子主题目录中创建一个JavaScript文件,例如 `custom.js`,并添加以下代码: ```javascript diff --git "a/blog/2024/12-17-WordPress\344\273\243\347\240\201\345\235\227\345\242\236\345\212\240\344\273\243\347\240\201\351\253\230\344\272\256\345\222\214\345\244\215\345\210\266\345\212\237\350\203\275.md" "b/blog/2024/12-17-WordPress\344\273\243\347\240\201\345\235\227\345\242\236\345\212\240\344\273\243\347\240\201\351\253\230\344\272\256\345\222\214\345\244\215\345\210\266\345\212\237\350\203\275.md" new file mode 100644 index 000000000..5146e1b8c --- /dev/null +++ "b/blog/2024/12-17-WordPress\344\273\243\347\240\201\345\235\227\345\242\236\345\212\240\344\273\243\347\240\201\351\253\230\344\272\256\345\222\214\345\244\215\345\210\266\345\212\237\350\203\275.md" @@ -0,0 +1,252 @@ +--- +date: 2024-12-17 17:53 +slug: WordPress代码块增加代码高亮和复制功能 +--- + + + +我理解您想要给代码块增加复制功能按钮,类似docusaurus的风格。以下是实现方案,使用jQuery: + + + +## 代码块复制按钮 + +```php +function add_code_copy_button() { + ?> + + + + + + +// PHP代码 + +``` + +或在Gutenberg编辑器中使用代码块,选择对应的语言。 + +支持的特性: +- 自动语言检测 +- 常用编程语言语法高亮 +- 响应式设计 +- 暗色主题 +- 良好的可读性 + +如果需要添加其他语言支持或修改样式,请告诉我。 diff --git "a/blog/2024/12-18-WordPress\345\210\266\344\275\234\347\224\250\346\210\267\344\270\252\344\272\272\346\224\266\350\227\217\344\270\255\345\277\203.md" "b/blog/2024/12-18-WordPress\345\210\266\344\275\234\347\224\250\346\210\267\344\270\252\344\272\272\346\224\266\350\227\217\344\270\255\345\277\203.md" new file mode 100644 index 000000000..918436f43 --- /dev/null +++ "b/blog/2024/12-18-WordPress\345\210\266\344\275\234\347\224\250\346\210\267\344\270\252\344\272\272\346\224\266\350\227\217\344\270\255\345\277\203.md" @@ -0,0 +1,309 @@ +--- +date: 2024-12-18 19:58 +slug: WordPress制作用户个人收藏中心 +--- + +整理完整的个人收藏中心实现流程和代码。 + + + +### 1. 在functions.php中添加以下代码: + +```php +// 1. 注册必要的脚本和样式 +function enqueue_favorite_scripts() { + wp_enqueue_script('jquery'); + wp_enqueue_script('favorite-script', get_stylesheet_directory_uri() . '/custom.js', array('jquery'), '1.0', true); + wp_localize_script('favorite-script', 'ajax_object', array( + 'ajax_url' => admin_url('admin-ajax.php') + )); +} +add_action('wp_enqueue_scripts', 'enqueue_favorite_scripts'); + +// 2. 添加收藏按钮到文章 +function add_favorite_button() { + if (is_single() && is_user_logged_in()) { + $post_id = get_the_ID(); + $user_id = get_current_user_id(); + $favorites = get_user_meta($user_id, 'user_favorites', true); + + if (!is_array($favorites)) { + $favorites = array(); + } + + $is_favorited = in_array($post_id, $favorites); + $button_text = $is_favorited ? '取消收藏' : '收藏文章'; + $button_class = $is_favorited ? 'favorited' : ''; + + echo ''; + } +} +add_action('astra_entry_content_after', 'add_favorite_button'); + +// 3. 处理AJAX收藏请求 +function handle_favorite_action() { + if (!is_user_logged_in()) { + wp_send_json_error('请先登录'); + } + + $post_id = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0; + $user_id = get_current_user_id(); + $favorites = get_user_meta($user_id, 'user_favorites', true); + + if (!is_array($favorites)) { + $favorites = array(); + } + + if (in_array($post_id, $favorites)) { + $favorites = array_diff($favorites, array($post_id)); + $action = 'removed'; + } else { + $favorites[] = $post_id; + $action = 'added'; + } + + update_user_meta($user_id, 'user_favorites', $favorites); + + wp_send_json_success(array( + 'action' => $action, + 'message' => $action === 'added' ? '收藏成功' : '已取消收藏' + )); +} +add_action('wp_ajax_handle_favorite', 'handle_favorite_action'); +``` + +### 2. 创建个人中心页面模板 +在主题目录下创建文件 `page-user-center.php`: + +```php + + +
+
+
+
+ ID, 'user_favorites', true); + ?> + + + +
+

我的收藏

+ $favorites, + 'post_type' => 'post', + 'posts_per_page' => -1 + ); + + $favorite_query = new WP_Query($args); + + if ($favorite_query->have_posts()) { + echo '
'; + while ($favorite_query->have_posts()) { + $favorite_query->the_post(); + ?> +
+ + + + +

+
+ +
+ '; + wp_reset_postdata(); + } + } else { + echo '

暂无收藏文章

'; + } + ?> +
+
+
+
+
+ + +``` + +### 3. 在style.css中添加样式: + +```css +/* 收藏按钮样式 */ +.favorite-button { + padding: 8px 15px; + background: #f0f0f0; + border: 1px solid #ddd; + border-radius: 3px; + cursor: pointer; + margin: 15px 0; + transition: all 0.3s ease; +} + +.favorite-button.favorited { + background: #ff6b6b; + color: white; + border-color: #ff6b6b; +} + +/* 用户中心样式 */ +.user-center-container { + max-width: 800px; + margin: 0 auto; + padding: 20px; +} + +.user-info { + text-align: center; + margin-bottom: 30px; + padding: 20px; + background: #f9f9f9; + border-radius: 8px; +} + +.user-info img { + border-radius: 50%; + margin: 10px 0; +} + +.favorite-post-item { + display: flex; + align-items: center; + padding: 15px; + border-bottom: 1px solid #eee; + transition: background 0.3s ease; +} + +.favorite-post-item:hover { + background: #f9f9f9; +} + +.favorite-post-item img { + width: 100px; + height: auto; + margin-right: 15px; + border-radius: 4px; +} + +.favorite-post-item h4 { + margin: 0; + flex: 1; +} + +.remove-favorite { + padding: 5px 10px; + background: #ff6b6b; + color: white; + border: none; + border-radius: 3px; + cursor: pointer; + transition: background 0.3s ease; +} + +.remove-favorite:hover { + background: #ff5252; +} + +.no-favorites { + text-align: center; + padding: 30px; + background: #f9f9f9; + border-radius: 8px; +} +``` + +### 4. 创建custom.js文件: + +```javascript +jQuery(document).ready(function($) { + // 收藏按钮点击事件 + $('.favorite-button').click(function() { + var button = $(this); + var postId = button.data('post-id'); + + $.ajax({ + url: ajax_object.ajax_url, + type: 'POST', + data: { + action: 'handle_favorite', + post_id: postId + }, + success: function(response) { + if (response.success) { + if (response.data.action === 'added') { + button.addClass('favorited'); + button.text('取消收藏'); + } else { + button.removeClass('favorited'); + button.text('收藏文章'); + } + } + } + }); + }); + + // 删除收藏按钮点击事件 + $('.remove-favorite').click(function() { + var button = $(this); + var postId = button.data('post-id'); + var postItem = button.closest('.favorite-post-item'); + + $.ajax({ + url: ajax_object.ajax_url, + type: 'POST', + data: { + action: 'handle_favorite', + post_id: postId + }, + success: function(response) { + if (response.success) { + postItem.fadeOut(300, function() { + $(this).remove(); + if ($('.favorite-post-item').length === 0) { + $('.favorite-list').html('

暂无收藏文章

'); + } + }); + } + } + }); + }); +}); +``` + +### 使用步骤: + +1. 将所有代码添加到相应文件中 +2. 在WordPress后台创建新页面,选择"用户中心"模板 +3. 发布页面,记录页面链接 +4. 可以在菜单中添加用户中心链接 + +### 功能说明: + +1. 登录用户可以在文章页面收藏/取消收藏文章 +2. 用户可以在个人中心查看所有收藏的文章 +3. 可以直接在个人中心删除收藏 +4. 未登录用户会自动跳转到登录页面 +5. 响应式设计,适配移动设备 + +如需添加其他功能或修改,请告诉我。 diff --git a/static/wordpress HTML block demo.html b/static/wordpress HTML block demo.html index 9a2cf41fd..f0a9baf10 100644 --- a/static/wordpress HTML block demo.html +++ b/static/wordpress HTML block demo.html @@ -1,3 +1,6 @@ +

本网页整理收集一些HTML区块代码小片段,以供学习备忘

+
+

⚠️ 重要提示:这是一个突出显示的信息框