Wordpress相关记录 Fri, Dec 8, 2023
一直都喜欢简单清爽的界面,折腾来折腾去的,满意后就消停一段时间,但之后再折腾时,有些东西容易忘记,故记录一下。此wp主题是购买Simple,然后根据hugo主题“Hugo.386”修改的。
博客标题后面的蓝色小点,直接在header文件中相关位置添加:
<span class="logo_dot" style="background-color: #29d;font-size: 1.5em;display: inline-block;width: 8px;height: 8px;border-radius: 50%;"></span>
本站:247篇文章;856条评论;最后更新于2023-12-07;已成立6277天;代码为:
本站:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>篇文章
<?php echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?>条评论
最后更新于<?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y-m-d', strtotime($last[0]->MAX_m));echo $last; ?>
已成立<?php echo floor((time()-strtotime("2006-10-01"))/86400); ?>天
2023-11-30 星期四,代码为:
<?php the_time('Y-m-d l'); ?>
我的hugo博客也改成简单的风格,不过没加留言功能。我认为我是记录为主,有标题,内容,时间,能搜索,打开速度快就行,其它像分类和标签,对我而言就用不着了,直接搜索就行(搜索结果里面可以Ctrl+F很方便),况且Typora的本地搜索功能也很方便。hugo的生成速度极快,特别是在macbookpro中,对比我的windows电脑,速度快多了,简直就是瞬间完成,这应该跟电脑的处理器有关。
以下为2024年3月17日更新。自定义WordPress评论Cookie功能。将下面代码添加到当前主题functions.php中。
如果你的主题使用了自定义评论函数,评论模块中没出现记录评论Cookie的复选框,可以使用下面的代码添加该功能,默认记录Cookie并隐藏难看的复选框:
add_action(‘set_comment_cookies’,’coffin_set_cookies’,10,3); function coffin_set_cookies( $comment, $user, $cookies_consent){ $cookies_consent = true; wp_set_comment_cookies($comment, $user, $cookies_consent); }
2024年4月2日更新:首页单栏改成双栏css设置。(在index.php里面修改)
1,main添加 column-count: 2; column-gap: 20px; margin: 0 auto; 2,class=post添加 break-inside: avoid-column;
以下为2024年6月20日更新。博客首页显示rss源第一篇文章,但是不知道哪里的缓存问题,还是什么问题,文章更新后,需要1小时左右后才会在首页同步更新后显示。
<?php // 定义RSS源地址 $rss_url = ‘https://memos.dafeng.xyz/u/admin/rss.xml’; // 获取RSS源数据 $rss = fetch_feed($rss_url); if (!is_wp_error($rss)) : // 获取RSS源中的文章项 $maxitems = $rss->get_item_quantity(1); // 只需要第一篇 $rss_items = $rss->get_items(0, $maxitems); endif; // 检查是否获取到了文章项 if ($maxitems == 0) { echo ‘<p>没有找到文章。</p>’; } else { // 遍历文章项并显示第一篇 foreach ($rss_items as $item) : echo ‘<h2>’ . esc_html($item->get_title()) . ‘</h2>’; // 可以在这里添加其他文章信息的显示,比如发布日期、链接等 break; // 因为只取第一篇,所以跳出循环 endforeach; } ?>
2024年6月27日更新,禁止使用博主邮箱评论
function prevent_blog_owner_email_comment($comment_data) { if (get_option('邮箱地址') == $comment_data['comment_author_email']) { wp_die('您不能使用博主的邮箱进行评论。'); } return $comment_data; } add_filter('comment_post', 'prevent_blog_owner_email_comment', 1);
2024年7月3日更新,调用wordpress最旧第一篇文章的第一行文字到首页
<?php $args = array( ‘numberposts’ => 1, // 获取一篇文章 ‘post_status’ => ‘publish’, // 只获取已发布的文章 ‘orderby’ => ‘post_date’, // 按照日期排序 ‘order’ => ‘ASC’, // 升序,最新的文章排在最前面 ); $recent_posts = wp_get_recent_posts($args); if (!empty($recent_posts)) { // 获取最新文章的内容 $content = $recent_posts[0][‘post_content’]; // 将内容分割成行 $content_lines = explode(“\n”, $content); // 输出第一行 echo $content_lines[0]; } ?>
2024年7月29日更新,wordpress新建页面(选择RSS Feed Page),显示订阅的rss最新的2篇文章
<?php /* Template Name: RSS Feed Page */ ?> <?php get_header(); ?> <div id="main"> <?php // 定义RSS提要的URL数组 $feeds = array( '名称1' => 'rss地址1', '名称2' => 'rss地址2', // 添加更多的RSS提要URL ); foreach ($feeds as $feed_name => $feed_url) { // 获取RSS提要 $feed = fetch_feed($feed_url); // 确认提要是否成功获取 if (!is_wp_error($feed) && $feed->get_item_quantity() > 0) { // 获取最新的2篇文章 $items = $feed->get_items(0, 2); // 输出提要的标题 echo '<h2 style="background: #f77b83;padding: 10px;border-radius: 5px;color: #fff;">' . $feed->get_title() . '</h2>'; // 循环显示每篇文章的标题和链接 foreach ($items as $item) { $title = $item->get_title(); $link = $item->get_permalink(); $date = $item->get_date('Y-m-d'); echo "<p style='margin-left: 10px;'>{$date}  <a rel='nofollow' target='_blank' href='{$link}'>{$title}</a></p>"; } } } ?> </div> <?php get_footer(); ?>