WordPress 7 个实用代码片段,添至主题 functions.php 文件

2023-05-2110:44:06网站管理维护Comments1,001 views字数 2743阅读模式

几个最近用到 WordPress 的代码片段,代码均搜集于网络,使用方式都是添加到主题的 functions.php 文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wg/40909.html

"Talk is cheap. Show me the pre." —— Linus Torvalds文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wg/40909.html

搜索结果页面关键字高亮 为搜索结果页中的标题和正文的关键字添加 highlight 类,然后你就可以通过 CSS 设定高亮样式了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wg/40909.html

function wp_highlight_search_keywords($text){
    if ( is_search() ) {
        $s = get_search_query();
        $keys = explode(' ', $s);
        $text = preg_replace('/('%20. implode('|', $keys) . ')/iu', '<strong class="highlight">$1', $text);
    }
    return $text;
}
add_filter( 'the_title', 'wp_highlight_search_keywords'%20);
add_filter( 'the_excerpt', 'wp_highlight_search_keywords'%20);

搜索关键字为空时自动跳转到首页 默认情况下,如果关键字为空,WordPress 会列出所有的文章。谁会这么无聊… 不如自动跳转到首页。

function wp_redirect_blank_search( $query_variables ) {
    if ( isset( $_GET['s'] ) && empty( $_GET['s']) ) {
        wp_redirect( home_url() );
        exit;
    }
    return $query_variables;
}
add_filter( 'request', 'wp_redirect_blank_search'%20);

关闭文章的标签功能 用不到标签,留着碍眼?去掉吧,就这么简单粗暴。

function wp_unregister_post_tag() {
    unregister_taxonomy_for_object_type('post_tag', 'post');
}
add_action( 'init', 'wp_unregister_post_tag'%20);

清理 WordPress 菜单中的 classes WordPress 菜单默认会输出一堆然并卵的 classes。如果你有洁癖,可以只保留你觉得有用的 classes,比如我觉得 current-menu-item 和 menu-item-has-children 最有用了。

function wp_cleanup_nav_menu_class( $classes ) {
    return array_intersect($classes, array(
        'current-menu-item',
        'menu-item-has-children'
    ));
}
add_filter( 'nav_menu_css_class', 'wp_cleanup_nav_menu_class'%20);

自动设置文章的第一张图为特色图像 懒得每次手动设置特色图像?这段代码可以自动把文章中上传的第一张图片设置为特色图像。(不支持外链图片)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wg/40909.html

function wp_autoset_featured_image() {
    global $post;
    if (!is_object($post)) return;
    $already_has_thumb = has_post_thumbnail($post->ID);
    if (!$already_has_thumb)  {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
        if ($attached_image) {
            foreach ($attached_image as $attachment_id => $attachment) {
                set_post_thumbnail($post->ID, $attachment_id);
            }
        }
    }
}
add_action( 'the_post', 'wp_autoset_featured_image'%20);
add_action( 'save_post', 'wp_autoset_featured_image'%20);
add_action( 'draft_to_publish', 'wp_autoset_featured_image'%20);
add_action( 'new_to_publish', 'wp_autoset_featured_image'%20);
add_action( 'pending_to_publish', 'wp_autoset_featured_image'%20);
add_action( 'future_to_publish', 'wp_autoset_featured_image'%20);

添加短代码 这段代码是把 [attachment id="1,2,3"] 输出为一个附件列表。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wg/40909.html

function wp_attachment_shortpre( $atts ) {
    $ids = explode(',', $atts['ids']);
    $html = '';

    foreach ($ids as $id) {
        $url = wp_get_attachment_url( $id );
        $name = basename($url);
        $html .= '<li><a class="file" href="'%20. $url . '" target="_blank">'%20. basename($url) . '</a></li>';
    }

    return '<div class="attachment-box"><h5 class="title">附件:</h5><ul>'%20. $html . '</ul></div>';
}
add_shortpre( 'attachment', 'wp_attachment_shortpre'%20);

获取文章的第一张图片 好吧,这个是来凑数的...

function wp_get_first_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('//i', $post->post_content, $matches);
    $first_img = $matches[1][0];

    if ( empty($first_img) ) {
        $first_img = "/path/to/default.png";
    }
    return $first_img;
}
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/wg/40909.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/wg/40909.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定