WordPress代码判断移动端 让手机站样式独具风格

  • 2020-12-28 20:30:35
  • 2,081 次阅读
  • 稿源:天马行空

wordpress制作自适应网站,通过媒体查询功能,判断当前设备屏幕大小,显示模版不同的样式。这样就可以使用一个模版实现pc端,ipad端和移动端的切换。但是,如果想实现pc端和移动端是独立的两套模版,也就说在修改pc端某个css样式时,手机端的样式不受影响,换句话就是pc端和移动端的css样式互不干扰。这样就需要WordPress代码判断手机移动设备,然后再跳转到手机端。

WordPress代码判断移动端 让手机站样式独具风格

WordPress使用纯代码判断当前设备并跳转到手机端,步骤如下:

第一步,在主题functions.php中加入下列代码 ,基本上是常见的移动浏览器的useragent。

  1. function is_mobile() {
  2. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  3. $mobile_browser = Array(
  4. "mqqbrowser", //手机QQ浏览器
  5. "opera mobi", //手机opera
  6. "juc","iuc",//uc浏览器
  7. "fennec","ios","applewebKit/420","applewebkit/525","applewebkit/532","ipad","iphone","ipaq","ipod",
  8. "iemobile", "windows ce",//windows phone
  9. "240x320","480x640","acer","android","anywhereyougo.com","asus","audio","blackberry","blazer","coolpad" ,"dopod", "etouch", "hitachi","htc","huawei", "jbrowser", "lenovo","lg","lg-","lge-","lge", "mobi","moto","nokia","phone","samsung","sony","symbian","tablet","tianyu","wap","xda","xde","zte"
  10. );
  11. $is_mobile = false;
  12. foreach ($mobile_browser as $device) {
  13. if (stristr($user_agent, $device)) {
  14. $is_mobile = true;
  15. break;
  16. }
  17. }
  18. return $is_mobile;
  19. }

第二步,如果想制作index.php首页模版,使用下列代码判断

  1. <?php if( !is_mobile() ){ ?>
  2.  
  3. pc端index.php
  4.  
  5. <?php } else{ ?>
  6.  
  7. <?php get_template_part("indexForMobile");?>
  8.  
  9. <?php } ?>

第三步,如果想制作single.php模版页,使用如下:

  1. <?php if( !is_mobile() ){ ?>
  2. // pc端single.php
  3. <?php } else{ ?>
  4.  
  5. <?php get_template_part("singleForMobile");?>
  6.  
  7. <?php } ?>

补充参考:手机端可能用到的代码

  1. //面包屑
  2. function fairy_breadcrumbs() {
  3. $delimiter = '»';
  4. $home = '首页';
  5. $before = '<span>';
  6. $after = '</span>';
  7. if ( !is_home() && !is_front_page() || is_paged() ) {
  8. echo '<div id="crumbs">';
  9. global $post;
  10. $homeLink = get_bloginfo('url');
  11. echo '<a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
  12. if ( is_category() ) {
  13. global $wp_query;
  14. $cat_obj = $wp_query->get_queried_object();
  15. $thisCat = $cat_obj->term_id;
  16. $thisCat = get_category($thisCat);
  17. $parentCat = get_category($thisCat->parent);
  18. if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
  19. echo $before . ' "' . single_cat_title('', false) . '" 目录下的文章' . $after;
  20. } else if ( is_single() && !is_attachment() ) {
  21. if ( get_post_type() != 'post' ) {
  22. $post_type = get_post_type_object(get_post_type());
  23. $slug = $post_type->rewrite;
  24. echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> ' . $delimiter . ' ';
  25. echo $before . get_the_title() . $after;
  26. } else {
  27. $cat = get_the_category(); $cat = $cat[0];
  28. echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
  29. echo $before . get_the_title() . $after;
  30. }
  31. } else if ( !is_single() && !is_page() && get_post_type() != 'post' ) {
  32. $post_type = get_post_type_object(get_post_type());
  33. echo $before . $post_type->labels->singular_name . $after;
  34. }
  35. if ( get_query_var('paged') ) {
  36. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
  37. echo __('Page') . ' ' . get_query_var('paged');
  38. if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
  39. }
  40. echo '</div>';
  41. }
  42. }
  43. //上下一页
  44. function xiayiye( $before = '', $after = '', $p = 2 ) {
  45. if ( is_singular() ) return;
  46. global $wp_query, $paged;
  47. $max_page = $wp_query->max_num_pages;
  48. if ( $max_page == 1 ) return;
  49. if ( empty( $paged ) ) $paged = 1;
  50. echo $before.'<div><ul>'."\n";
  51. //echo '<span>Page: ' . $paged . ' of ' . $max_page . ' </span>';
  52. if ( $paged > 1 ) p_link( $paged - 1, '上一页', '上一页' );
  53. //if ( $paged > $p + 2 ) echo ' ';
  54. //if ( $paged < $max_page - $p - 1 ) echo '... ';
  55. if ( $paged < $max_page ) p_link( $paged + 1,'下一页', '下一页' );
  56. echo '</ul></div>'.$after."\n";
  57. }
  58. //自动关键词
  59. function auto_keywords() {
  60. global $s, $post;
  61. $keywords = '';
  62. if ( is_single() ) {
  63. if ( get_the_tags( $post->ID ) ) {
  64. foreach ( get_the_tags( $post->ID ) as $tag ) $keywords .= $tag->name . ', ';
  65. }
  66. foreach ( get_the_category( $post->ID ) as $category ) $keywords .= $category->cat_name . ', ';
  67. $keywords = substr_replace( $keywords , '' , -2);
  68. } elseif ( is_home () ) {
  69. $keywords = '首页关键词,需要自己填写在这里';
  70. } elseif ( is_tag() ) {
  71. $keywords = single_tag_title('', false);
  72. } elseif ( is_category() ) {
  73. $keywords = single_cat_title('', false);
  74. } elseif ( is_search() ) {
  75. $keywords = esc_html( $s, 1 );
  76. } else {
  77. $keywords = trim( wp_title('', false) );
  78. }
  79. if ( $keywords ) {
  80. echo '<meta name="keywords" content="'.$keywords.'" />\n';
  81. }
  82. }
  83. //自动描述
  84. function auto_description() {
  85. global $s, $post;
  86. $description = '';
  87. $blog_name = get_bloginfo('name');
  88. if ( is_singular() ) {
  89. if( !empty( $post->post_excerpt ) ) {
  90. $text = $post->post_excerpt;
  91. } else {
  92. $text = $post->post_content;
  93. }
  94. $description = trim( str_replace( array( "\r\n", "\r", "\n", " ", " "), " ", str_replace( "\"", "'", strip_tags( $text ) ) ) );
  95. if ( !( $description ) ) $description = $blog_name . " - " . trim( wp_title('', false) );
  96. } elseif ( is_home () ) {
  97. $description = $blog_name . " - 首页描述,需要自己填写在这里"; // 首頁要自己加
  98. } elseif ( is_tag() ) {
  99. $description = $blog_name . "有关 '" . single_tag_title('', false) . "' 的文章";
  100. } elseif ( is_category() ) {
  101. $description = $blog_name . "有关 '" . single_cat_title('', false) . "' 的文章";
  102. } elseif ( is_archive() ) {
  103. $description = $blog_name . "在: '" . trim( wp_title('', false) ) . "' 的文章";
  104. } elseif ( is_search() ) {
  105. $description = $blog_name . ": '" . esc_html( $s, 1 ) . "' 的搜索結果";
  106. } else {
  107. $description = $blog_name . "有关 '" . trim( wp_title('', false) ) . "' 的文章";
  108. }
  109. $description = mb_substr( $description, 0, 220, 'utf-8' ) . '..';
  110. echo '<meta name="description" content="'.$description.'" />\n';
  111. }
  112. //判断移动设备
  113. function is_mobile() {
  114. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  115. $mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
  116. $is_mobile = false;
  117. foreach ($mobile_agents as $device) {
  118. if (stristr($user_agent, $device)) {
  119. $is_mobile = true;
  120. break;
  121. }
  122. }
  123. return $is_mobile;
  124. }
  125. //统计浏览次数
  126. function getPostViews($postID){
  127. $count_key = 'post_views_count';
  128. $count = get_post_meta($postID, $count_key, true);
  129. if($count==''){
  130. delete_post_meta($postID, $count_key);
  131. add_post_meta($postID, $count_key, '0');
  132. return 0;
  133. }
  134. return $count;
  135. }
  136. function setPostViews($postID) {
  137. $count_key = 'post_views_count';
  138. $count = get_post_meta($postID, $count_key, true);
  139. if($count==''){
  140. $count = 0;
  141. delete_post_meta($postID, $count_key);
  142. add_post_meta($postID, $count_key, '0');
  143. }else{
  144. $count++;
  145. update_post_meta($postID, $count_key, $count);
  146. }
  147. }

喜欢 0

文章评论 (0)

表情

大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头