今天看到一篇不错的文章wordpress二次开发技巧-functions.php篇及转载过来给一些新人学习,为什么说转载推荐这个让大家学习呢,这个是一个主题完整的functions.php篇,不然新人就低不断搜索每项了。

widgets sidebar 侧边栏小工具


/** widgets sidebar 侧边栏小工具*/
if( function_exists('register_sidebar') ) {
  register_sidebar(array(
    'name' => 'First_sidebar',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h4>',
    'after_title' => '</h4>'
  ));
  register_sidebar(array(
    'name' => 'Second_sidebar',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h4>',
    'after_title' => '</h4>'
  ));
  register_sidebar(array(
    'name' => 'Third_sidebar',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h4>',
    'after_title' => '</h4>'
  ));
  register_sidebar(array(
    'name' => 'Fourth_sidebar',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '<h4>',
    'after_title' => '</h4>'
  ));
}

register_nav_menus(array("primary" => "Primary Navigation"));

2. 后台支持自定义菜单


/*nav  后台自定义菜单*/
if(function_exists('register_nav_menus')){
  register_nav_menus(
    array(
    'header-menu' => __( '导航自定义菜单' ),
    'footer-menu' => __( '页角自定义菜单' ),
    'sider-menu' => __('侧边栏菜单'),
    'primary' => __( 'Primary Navigation', 'lee' ),
    )
  );
}
//输出菜单,在如header.php插入显示菜单的代码
<?php wp_nav_menu( array( 'container' => '' ) ); ?>

3. 面包屑导航


// 面包屑导航注册代码
function wheatv_breadcrumbs() {
  $delimiter = '<i>></i>';
  $name = '首页'; //text for the 'Home' link
  $currentBefore = '';
  $currentAfter = '';

  if ( !is_home() && !is_front_page() || is_paged() ) {

  echo '';
  global $post;
  // $home = get_bloginfo('url');
  $home = get_option('home');
  echo '<a href="'.$home.'" >'. $name . ' </a>' . $delimiter . ' ';

  if ( is_category() ) {
    global $wp_query;
    $cat_obj = $wp_query->get_queried_object();
    $thisCat = $cat_obj->term_id;
    $thisCat = get_category($thisCat);
    $parentCat = get_category($thisCat->parent);
    if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
      echo $currentBefore . '';
      single_cat_title();
      echo '' . $currentAfter;

    } elseif ( is_day() ) {
      echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';
      echo '' . get_the_time('F') . ' ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('d') . $currentAfter;

    } elseif ( is_month() ) {
      echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('F') . $currentAfter;

    } elseif ( is_year() ) {
      echo $currentBefore . get_the_time('Y') . $currentAfter;

    } elseif ( is_single() ) {
      $cat = get_the_category(); $cat = $cat[0];
      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_page() && !$post->post_parent ) {
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_page() && $post->post_parent ) {
      $parent_id = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '' . get_the_title($page->ID) . '';
        $parent_id = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_search() ) {
      echo $currentBefore . '搜索结果' . get_search_query() . '' . $currentAfter;

    } elseif ( is_tag() ) {
      echo $currentBefore . '搜索标签: ';
      single_tag_title();
      echo '' . $currentAfter;

    } elseif ( is_author() ) {
      global $author;
      $userdata = get_userdata($author);
      echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;

    } elseif ( is_404() ) {
      echo $currentBefore . 'Error 404' . $currentAfter;
  }

  if ( get_query_var('paged') ) {
    if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo __('第') . '' . get_query_var('paged') . '页';
    if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  }
  echo '';
  }
}
//显示面包屑导航(category.php或single.php等)
<?php wheatv_breadcrumbs(); ?>

4. 文章访问量(点击数)


//文章点击数
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count;
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
//显示点击量(如category.php或single.php)
<?php echo getPostViews(get_the_ID()); ?>

5. 文章中所有链接新窗口中打开


//为文章中所有链接添加target="_blank"属性
function autoblank($content) {
    $content = preg_replace("/<a(.*?)>/", "<a$1 target=\"_blank\">", $content);
    return $content;
}
add_filter('the_content', 'autoblank');

6. 清除wp自带无用头部信息


//清除头部信息
//remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
//remove_action( 'wp_head', 'locale_stylesheet' );
remove_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 );
//remove_action( 'wp_head', 'noindex', 1 );
//remove_action( 'wp_head', 'wp_print_styles', 8 );
//remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_generator' );
//remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_footer', 'wp_print_footer_scripts' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
global $wp_widget_factory;
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}

7. 自动保存和文章修订功能


//自动保存和文章修订功能
define('AUTOSAVE_INTERVAL', 120 );  // 设置自动保存间隔,单位是秒,默认60
define('WP_POST_REVISIONS', false); // 如果不禁用自动修订,最多允许保存的版本数,3表示最多保存3个修订版

8. 彩色静态标签云


// 彩色静态标签云 Color Tag Cloud
function colorCloud($text) {
  $text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
  return $text;
}
function colorCloudCallback($matches) {
  $text = $matches[1];
  $color = dechex(rand(0,16777215));
  $pattern = '/style=(\'|\")(.*)(\'|\")/i';
  $text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
  return "<a $text>";
}
add_filter('wp_tag_cloud', 'colorCloud', 1);
//输出标签
<?php wp_tag_cloud('smallest=10&largest=14&number=32&order=RAND') ?>

9. 搜索结果关键词高亮显示


// 搜索结果关键词高亮显示
function lee_set_query() {
  $query  = attribute_escape(get_search_query());
  if(strlen($query) > 0){
    echo '
      <script type="text/javascript">
        var lee_query  = "'.$query.'";
      </script>
    ';
  }
}
function lee_init_jquery() {
 wp_enqueue_script('jquery');
}
add_action('init', 'lee_init_jquery');
add_action('wp_print_scripts', 'lee_set_query');