贴几个最近用到 WordPress 的代码片段,代码均搜集于网络,使用方式都是添加到主题的functions.php 文件。 "Talk is cheap. Show me the code." —— Linus Torvalds 搜索结果页面关键字高亮 为搜索结果页中的标题和正文的关键字添加 highlight 类,然后你就可以通过 CSS 设定高亮样式了。 function c7sky_highlight_search_keywords($text){ if ( is_search() ) { $s = get_search_query(); $keys = explode(' ', $s); $text = preg_replace('/(' . implode('|', $keys) . ')/iu', '<strong class="highlight">$1</strong>', $text); } return $text; } add_filter( 'the_title', 'c7sky_highlight_search_keywords' ); add_filter( 'the_excerpt', 'c7sky_highlight_search_keywords' ); 搜索关键字为空时自动跳转到首页 默认情况下,如果关键字为空,WordPress 会列出所有的文章。谁会这么无聊… 不如自动跳转到首页。 function c7sky_redirect_blank_search( $query_variables ) { if ( isset( $_GET['s'] ) && empty( $_GET['s']) ) { wp_redirect( home_url() ); exit; } return $query_variables; } add_filter( 'request', 'c7sky_redirect_blank_search' ); 关闭文章的标签功能 用不到标签,留着碍眼?去掉吧,就这么简单粗暴。 function c7sky_unregister_post_tag() { unregister_taxonomy_for_object_type('post_tag', 'post'); } add_action( 'init', 'c7sky_unregister_post_tag' ); 清理 WordPress 菜单中的 classes WordPress 菜单默认会输出一堆然并卵的 classes。如果你有洁癖,可以只保留你觉得有用的classes,比如我觉得 current-menu-item 和 menu-item-has-children 最有用了。 function c7sky_cleanup_nav_menu_class( $classes ) { return array_intersect($classes, array( 'current-menu-item', 'menu-item-has-children' )); } add_filter( 'nav_menu_css_class', 'c7sky_cleanup_nav_menu_class' ); 自动设置文章的第一张图为特色图像 懒得每次手动设置特色图像?这段代码可以自动把文章中上传的第一张图片设置为特色图像。(不支持外链图片) function c7sky_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', 'c7sky_autoset_featured_image' ); add_action( 'save_post', 'c7sky_autoset_featured_image' ); add_action( 'draft_to_publish', 'c7sky_autoset_featured_image' ); add_action( 'new_to_publish', 'c7sky_autoset_featured_image' ); add_action( 'pending_to_publish', 'c7sky_autoset_featured_image' ); add_action( 'future_to_publish', 'c7sky_autoset_featured_image' ); 添加短代码 这段代码是把 [attachment id="1,2,3"] 输出为一个附件列表。 function c7sky_attachment_shortcode( $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="' . $url . '" target="_blank">' . basename($url) . '</a></li>'; } return '<div class="attachment-box"><h5 class="title">附件:</h5><ul>' . $html . '</ul></div>'; } add_shortcode( 'attachment', 'c7sky_attachment_shortcode' ); 获取文章的第一张图片 function c7sky_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; } 文章转载自:小影志(http://c7sky.com/) Loading... 贴几个最近用到 WordPress 的代码片段,代码均搜集于网络,使用方式都是添加到主题的<code>functions.php</code> 文件。 "Talk is cheap. Show me the code." —— Linus Torvalds<!--more--> <h2>搜索结果页面关键字高亮</h2> 为搜索结果页中的标题和正文的关键字添加 <code>highlight</code> 类,然后你就可以通过 CSS 设定高亮样式了。 <pre class="lang:default decode:true ">function c7sky_highlight_search_keywords($text){ if ( is_search() ) { $s = get_search_query(); $keys = explode(' ', $s); $text = preg_replace('/(' . implode('|', $keys) . ')/iu', '<strong class="highlight">$1</strong>', $text); } return $text; } add_filter( 'the_title', 'c7sky_highlight_search_keywords' ); add_filter( 'the_excerpt', 'c7sky_highlight_search_keywords' );</pre> <h2>搜索关键字为空时自动跳转到首页</h2> 默认情况下,如果关键字为空,WordPress 会列出所有的文章。谁会这么无聊… 不如自动跳转到首页。 <pre class="lang:default decode:true ">function c7sky_redirect_blank_search( $query_variables ) { if ( isset( $_GET['s'] ) && empty( $_GET['s']) ) { wp_redirect( home_url() ); exit; } return $query_variables; } add_filter( 'request', 'c7sky_redirect_blank_search' );</pre> <h2>关闭文章的标签功能</h2> 用不到标签,留着碍眼?去掉吧,就这么简单粗暴。 <pre class="lang:default decode:true ">function c7sky_unregister_post_tag() { unregister_taxonomy_for_object_type('post_tag', 'post'); } add_action( 'init', 'c7sky_unregister_post_tag' );</pre> <h2>清理 WordPress 菜单中的 classes</h2> WordPress 菜单默认会输出一堆然并卵的 <code>classes</code>。如果你有洁癖,可以只保留你觉得有用的<code>classes</code>,比如我觉得 <code>current-menu-item</code> 和 <code>menu-item-has-children</code> 最有用了。 <pre class="lang:default decode:true ">function c7sky_cleanup_nav_menu_class( $classes ) { return array_intersect($classes, array( 'current-menu-item', 'menu-item-has-children' )); } add_filter( 'nav_menu_css_class', 'c7sky_cleanup_nav_menu_class' );</pre> <h2>自动设置文章的第一张图为特色图像</h2> 懒得每次手动设置特色图像?这段代码可以自动把文章中上传的第一张图片设置为特色图像。(不支持外链图片) <pre class="lang:default decode:true ">function c7sky_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', 'c7sky_autoset_featured_image' ); add_action( 'save_post', 'c7sky_autoset_featured_image' ); add_action( 'draft_to_publish', 'c7sky_autoset_featured_image' ); add_action( 'new_to_publish', 'c7sky_autoset_featured_image' ); add_action( 'pending_to_publish', 'c7sky_autoset_featured_image' ); add_action( 'future_to_publish', 'c7sky_autoset_featured_image' );</pre> <h2>添加短代码</h2> 这段代码是把 <code>[attachment id="1,2,3"]</code> 输出为一个附件列表。 <pre class="lang:default decode:true ">function c7sky_attachment_shortcode( $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="' . $url . '" target="_blank">' . basename($url) . '</a></li>'; } return '<div class="attachment-box"><h5 class="title">附件:</h5><ul>' . $html . '</ul></div>'; } add_shortcode( 'attachment', 'c7sky_attachment_shortcode' );</pre> <h2>获取文章的第一张图片</h2> <pre class="lang:default decode:true ">function c7sky_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; }</pre> 文章转载自:小影志(<span class="external-link"><a class="no-external-link" href="http://c7sky.com/" target="_blank"><i data-feather="external-link"></i>http://c7sky.com/</a></span>) © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏