以下內容属于 ‘WordPress’ 分类:

wordpress 时间命名图片 发现小时 分钟秒数对不上

wordpress 时间命名图片 发现小时 分钟秒数对不上 。 起源于《给WordPress上传的附件加个时间戳 减少数据库查询次数》,本来想弄高级一点,看起来有点深度技术的感觉,结果发现时间对不上,总是相差8个小时左右。 我还以为我时区设置那边有问题,去设置那边一看,有个“协调世界时”,而重命名的图片时间正好跟这个吻合。本来我们应该是UTC+8,而wordpress函数取值用了UTC时间。 发现很多是去修改程序文件,感觉以后更新也麻烦。 于是发现干脆在函数上加点料。 我之前是这样写的 12345add_filter('wp_handle_upload_prefilter', function($file){     $time = date("YmdHis");     $file['name'] = $time.'-'.$file['name'];     return $file; }); 现在改成这样 12345add_filter('wp_handle_upload_prefilter', function($file){     $time = date("YmdHis",time() + 8*3600);     $file['name'] = $time.'-'.$file['name'];     return $file; }); 变化在第二行。 (more...)

给WordPress上传的附件加个时间戳 减少数据库查询次数

最近在优化网站,主要是1M的带宽,小水管,又想用户访问快一点,考虑到以后万一做成了一个过万IP的大站,那么就需要优化。早点安排上吧~ :d 看到附件有时候有多个同名前缀的情况,这样的情况不利于数据库查询。 1234add_filter('wp_handle_upload_prefilter', function($file){     $file['name'] = time().'-'.$file['name'];     return $file; }); 如果你想得到类似 20221018……文件名.jpg 这样的效果,可以这样。 12345add_filter('wp_handle_upload_prefilter', function($file){     $time = date("YmdHis");     $file['name'] = $time.'-'.$file['name'];     return $file; }); 这样有个好处,便于知道附件的时间信息。 如果有强迫症,需要小时时间分钟秒数能对上。参考下《wordpress 时间命名图片 发现小时 分钟秒数对不上 》 (more...)

在wordpress中禁用google fonts谷歌字体

放到functions.php用的代码 123456789101112** * Plugin Name: Disable & Remove Google Fonts * Plugin URI: https://wordpress.org/plugins/disable-remove-google-fonts/ * Description: Optimize frontend performance by disabling Google Fonts. GDPR-friendly. * Author: Fonts Plugin * Author URI: https://fontsplugin.com * Version: 1.3.9 * License: GPLv2 or later * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt * * @package disable-remove-google-fonts */ // Exit if accessed directly. if […] (more...)

WordPress获取第一张图作为缩略图

记录WordPress获取第一张图作为缩略图。 首先如果有设置缩略图,则从设置中获取缩略图,没有的话,就从文章中获取第一张图片作为缩略图。 将其添加到functions.php或制作功能插件: 12345678910111213function catch_that_image() {   global $post, $posts;   $first_img = '';   ob_start();   ob_end_clean();   $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);   $first_img = $matches[1][0];     if(empty($first_img)) {     $first_img = "/path/to/default.png";   }   return $first_img; } 前端循环输出代码 123456789101112131415if ( get_the_post_thumbnail($post_id) != '' ) {     echo '<a […] (more...)

WordPress搬家水印插件DX-Watermark出错不能用解决办法

DX-Watermark是一个很好用的插件。 https://cn.wordpress.org/plugins/dx-watermark/ 不过这个插件已经有七八年没维护了,由于我用的还是老版本WP 4.9.4 所有依然还能使用,官方目前提示的也是最近3个版本不能用。 所以在遇到启用插件报错后,我怀疑是不是设置问题,去设置页,发现以前能预览的基础图片也不显示。 网上说是路径问题,于是在数据库修改了链接。 wp_options这张表 这两行里,点击编辑。 由于搬家之前我使用的是/home/wwwroot/www.pianpai.com/uploads/dw-uploads/waterok.png 而使用宝塔面板后,我的FTP路径默认变成了这个。 /www/wwwroot/www.pianpai.com/uploads/dw-uploads/waterok.png 所以编辑后在里面直接修改,修改后点执行。就改好了。 DX-Watermark-options和DX-Watermark-options-preview都需要点击“编辑”进行修改。 修改后发现还是不行啊。预览水印,插件还是报错。 想到都改了水印图片的路径了,应该也没问题了呀。 去插件页,发现插件是1.0.3版本,这个插件确实很久没更新过,但是最后的版本是1.0.4。 于是试下看看1.0.4能不能用。 下载后FTP上传覆盖后,发现能预览了。问题就解决掉了。 总结一下,两个问题,一个是搬家后网站的目录地址发生改变,数据库依然是保存的之前的路径地址。然后是1.0.3版本惹得错。 切记注意。 如果你也是遇到这个插件,不会操作修改。我可以提供付费服务。30元。 顺便提供一下最终版的DX-Watermark 1.0.4下载。 点此下载dx-watermark (more...)

WordPress免插件实现相关文章的代码

实现WordPress相关文章有很多的插件,有些主题也自带,刚好喜欢的一个主题没有这个功能,那么我们可以尝试自己来添加。 我们要实现的效果图如下: 代码分为两部分,一部分是放到主题文章需要显示的地方,一般放在single.php。 插入到主题的代码 1234567891011121314151617181920212223242526272829303132333435<div class="relatedposts"> <h3>Related posts</h3> <?php   $orig_post = $post;   global $post;   $tags = wp_get_post_tags($post->ID);       if ($tags) {   $tag_ids = array();   foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;   $args=array(   'tag__in' => $tag_ids,   'post__not_in' => array($post->ID),   'posts_per_page'=>4, // Number of related posts to […] (more...)

WordPress获取附件的名字和大小

WordPress获取附件信息,比如附件的名字和大小。在网上找了一圈,不太适合我要用的场景,后面在stackoverflow上面找到了合胃口的。记录一下,也分享给你。 12345678910111213141516// retrieve file of the custom field  $file = get_post_meta(get_the_ID(), 'wp_custom_attachment', true);  //get the url  $url = $file['url'];  //Replace url to directory path   $path = str_replace( site_url('/'), ABSPATH, esc_url( $url) );   if ( is_file( $path ) ){     $filesize = size_format( filesize( $path ) );     $filename = basename($path);   […] (more...)

WordPress 通过指定文章ID获取该文章的第一张图片

百度了一堆都不太好用,主要是我要在自定义文章类型(custom_type)的文章中调用默认文章类型(post)的第一张图片。其实我是为了扩充版面,让内容看起来不至于空虚,所以调用任意一张都可以。 Google了一下,找到了,这里记录一下。 123456789101112function get_first_image( $post_id ) {     $attach = get_children( array(         'post_parent'    => $post_id,         'post_type'      => 'attachment',         'post_mime_type' => 'image',         'order'          => 'DESC',         'numberposts'    => […] (more...)

点击按钮实现复制指定文本

特别是做资源站,有提取码之类的字段 还是挺管用。分享给大家。 复制文本框中的文字 HTML代码部分 1234<div>      <input type="text" value="The Text to Copy" id="copyMe">      <button onclick="copyMyText()">复制到剪贴板</button> </div> JavaScript 部分 12345678910<script> function copyMyText() {      //select the element with the id "copyMe", must be a text box      var textToCopy = document.getElementById("copyMe");      //select the text in the text box   […] (more...)

wordpress去除掉链接的category

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566//去除分类标志代码 add_action( 'load-themes.php',  'no_category_base_refresh_rules'); add_action('created_category', 'no_category_base_refresh_rules'); add_action('edited_category', 'no_category_base_refresh_rules'); add_action('delete_category', 'no_category_base_refresh_rules'); function no_category_base_refresh_rules() {     global $wp_rewrite;     $wp_rewrite -> flush_rules(); } // register_deactivation_hook(__FILE__, 'no_category_base_deactivate'); // function no_category_base_deactivate() { //  remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules'); //  // We don't want to insert our custom rules again //  no_category_base_refresh_rules(); // } // Remove category base add_action('init', 'no_category_base_permastruct'); function […] (more...)