WordPress获取PC端对应的移动端的网址[wordpress 移动站改造]

获取wordpress 当前网页的网址

Python
<?php // 说明:获取完整URL function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; } return $pageURL; } ?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// 说明:获取完整URL
function curPageURL ( )
{
     $pageURL = 'http' ;
     if ( $_SERVER [ "HTTPS" ] == "on" )
     {
         $pageURL . = "s" ;
     }
     $pageURL . = "://" ;
     if ( $_SERVER [ "SERVER_PORT" ] != "80" )
     {
         $pageURL . = $_SERVER [ "SERVER_NAME" ] . ":" . $_SERVER [ "SERVER_PORT" ] . $_SERVER [ "REQUEST_URI" ] ;
     }
     else
     {
         $pageURL . = $_SERVER [ "SERVER_NAME" ] . $_SERVER [ "REQUEST_URI" ] ;
     }
     return $pageURL ;
}
?>
 

上面的函数放入 functions.php 文件中

在page headers 调用

Python
<?php $www_url = curPageURL(); // 获取当前pc的url $m_url = str_replace(':443','',str_replace('www','m',$www_url)); // 获取对应的手机端url ?> <meta name="mobile-agent" content="format=html5;url=<?php echo $m_url;?>" /> // 告诉百度,那个才是移动端
1
2
3
4
5
6
7
8
<?php $www_url = curPageURL ( ) ;
     // 获取当前pc的url
       $m_url = str_replace ( ':443' , '' , str_replace ( 'www' , 'm' , $www_url ) ) ;
       // 获取对应的手机端url
?>
< meta name = "mobile-agent" content = "format=html5;url=<?php  echo $m_url;?>" / >
/ / 告诉百度,那个才是移动端
 

加入手机进入pc 网页跳转到手机端的代码

Python
<link rel="alternate" media="only screen and(max-width:640px)" href="<?php echo $m_url;?>"/> <link rel="alternate" media="handheld" href="<?php echo $m_url;?>" />
1
2
3
< link rel = "alternate" media = "only screen and(max-width:640px)" href = "<?php  echo $m_url;?>" / >
< link rel = "alternate" media = "handheld" href = "<?php echo $m_url;?>" / >
 

移动端加入

Python
<?php $m_url = curPageURL(); $www_url = str_replace(':443','',str_replace('m.','www.',$m_url)); ?> <link rel="canonical" href="<?php echo $www_url;?>" />
1
2
3
4
5
     <?php $m_url = curPageURL ( ) ;
           $www_url = str_replace ( ':443' , '' , str_replace ( 'm.' , 'www.' , $m_url ) ) ;
     ?>
     < link rel = "canonical" href = "<?php echo $www_url;?>" / >
 
Python
技术支持 1. META标记 <meta name="mobile-agent"content="format=[wml|xhtml|html5]; url=url"> Meta声明示例: 站长需要将Meta声明放在PC页源代码内部,如下: <meta name="mobile-agent" content="format=html5;url=“pc页面对应的移动页面” > <meta name="mobile-agent" content="format=html5;url=http://m.xxx.com/"> 2. 跳转适配规则 鉴于移动化大潮的汹涌和H5页的炫丽普及,百度针对PC页与H5页的跳转适配方式推出了最优方案:在pc版网页上,添加指向对应移动版网址的特殊链接rel="alternate"标记,这有助于百度发现网站的移动版网页所在的位置;同时在移动版网页上,添加指向对应pc版网址的链接rel="canonical"标记。   例如:   pc版网址:http://www.example.com/page-1   移动版网址:http://m.example.com/page-1     那么此示例中的注释如下所示:   在pc版网页(http://www.example.com/page-1) 上,添加: <link rel="alternate" media="only screen and(max-width: 640px)" href="http://m.example.com/page-1" > 注意:640px是百度,及谷歌给的参考值   在移动版网页(http://m.example.com/page-1) 上,所需的注释应为:   <link rel="canonical" href="http://www.example.com/page-1" > ------网站转码--------- 在 pc 移动端 head添加 <meta name="mobile-agent" content="format=html5;url="pc页面对应的移动页面" > <link rel="alternate" media="only screen and(max-width: 640px)" href="pc页面对应的移动页面" > <meta name="applicable-device"content="pc"> <meta http-equiv="Cache-Control" content="no-transform" /> <meta http-equiv="Cache-Control" content="no-siteapp" /> 移动端添加 <meta name="applicable-device"content="mobile"> <meta http-equiv="Cache-Control" content="no-transform" /> <meta http-equiv="Cache-Control" content="no-siteapp" /> <link rel="canonical" href="移动端对于pc页面" >
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
技术支持
 
1.    META标记
 
< meta name = "mobile-agent" content = "format=[wml|xhtml|html5]; url=url" >
Meta声明示例:
站长需要将 Meta声明放在 PC页源代码内部,如下:
< meta  name = "mobile-agent"  content = "format=html5;url=“pc页面对应的移动页面” >
 
<meta name=" mobile - agent " content=" format = html5 ; url = http : / / m . xxx . com / ">
 
2. 跳转适配规则
 
鉴于移动化大潮的汹涌和H5页的炫丽普及,百度针对PC页与H5页的跳转适配方式推出了最优方案:在pc版网页上,添加指向对应移动版网址的特殊链接rel=" alternate "标记,这有助于百度发现网站的移动版网页所在的位置;同时在移动版网页上,添加指向对应pc版网址的链接rel=" canonical "标记。
 
  例如:
  pc版网址:http://www.example.com/page-1
  移动版网址:http://m.example.com/page-1
 
  那么此示例中的注释如下所示:
  在pc版网页(http://www.example.com/page-1) 上,添加:
<link rel=" alternate " media=" only  screen  and ( max - width :  640px ) " href=" http : / / m . example . com / page - 1 " >
注意:640px是百度,及谷歌给的参考值
  在移动版网页(http://m.example.com/page-1) 上,所需的注释应为:
  <link rel=" canonical " href=" http : / / www . example . com / page - 1 " >
 
------网站转码---------
 
在 pc 移动端 head添加
 
<meta name=" mobile - agent " content=" format = html5 ; url = "pc页面对应的移动页面" >
< link  rel = "alternate"  media = "only screen and(max-width: 640px)"  href = "pc页面对应的移动页面"  >
< meta name = "applicable-device" content = "pc" >
< meta http - equiv = "Cache-Control" content = "no-transform" / >
< meta http - equiv = "Cache-Control" content = "no-siteapp" / >
 
 
移动端添加
 
< meta name = "applicable-device" content = "mobile" >
< meta http - equiv = "Cache-Control" content = "no-transform" / >
< meta http - equiv = "Cache-Control" content = "no-siteapp" / >
< link  rel = "canonical"  href = "移动端对于pc页面"  >
 
 



  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值