WordPress 主题中修改 对应的header.php
在主题对应的 head 部分添加
<?php if( is_single() || is_page() ): ?> <?php // get the post object $post = get_post( get_the_ID() ); // we need just the content $content = $post->post_content; // we need a expression to match things $regex = '/src="([^"]*?)"/'; // we want all matches preg_match_all( $regex, $content, $matches ); // reversing the matches array $matches = array_reverse($matches); // we've reversed the array, so index 0 returns the result //images数量不对,仅允许提供0张,1张或者3张图 function get_Image($arr) { $ar = []; $arr_lg = count($arr); if ($arr_lg<=2) { echo '"'.$arr[0].'"'; } else if($arr_lg>=3){ $ar = array_chunk($arr,3); foreach($ar[0] as $k=>$v) { // print_r($ar); if($v != end($ar[0])) { // print_r($v); echo '"'.$v.'",'; } else{ // print_r($v); echo '"'.$v.'"'; } } } } ?> <script type="application/ld+json"> { "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld", "@id": "<?php the_permalink();?>", "appid": "[修改这个参数]", "title": "<?php the_title();?>", "images": [<?php echo get_Image($matches[0]);?>], "pubDate": "<?php echo get_the_time('Y-m-d')."T".get_the_time('H:i:s');?>" } </script> <?php endif; ?> <link rel="canonical" href="<?php the_permalink();?>"/> <script src="//msite.baidu.com/sdk/c.js?appid=[修改这个参数]"></script>
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
46
47
48
49
50
51
|
<?php
if
(
is_single
(
)
||
is_page
(
)
)
:
?>
<?php
// get the post object
$post
=
get_post
(
get_the_ID
(
)
)
;
// we need just the content
$content
=
$post
->
post_content
;
// we need a expression to match things
$regex
=
'/src="([^"]*?)"/'
;
// we want all matches
preg_match_all
(
$regex
,
$content
,
$matches
)
;
// reversing the matches array
$matches
=
array_reverse
(
$matches
)
;
// we've reversed the array, so index 0 returns the result
//images数量不对,仅允许提供0张,1张或者3张图
function
get_Image
(
$arr
)
{
$ar
=
[
]
;
$arr_lg
=
count
(
$arr
)
;
if
(
$arr_lg
<=
2
)
{
echo
'"'
.
$arr
[
0
]
.
'"'
;
}
else
if
(
$arr_lg
>=
3
)
{
$ar
=
array_chunk
(
$arr
,
3
)
;
foreach
(
$ar
[
0
]
as
$k
=
>
$v
)
{
// print_r($ar);
if
(
$v
!=
end
(
$ar
[
0
]
)
)
{
// print_r($v);
echo
'"'
.
$v
.
'",'
;
}
else
{
// print_r($v);
echo
'"'
.
$v
.
'"'
;
}
}
}
}
?>
<script
type
=
"application/ld+json"
>
{
"@context"
:
"https://ziyuan.baidu.com/contexts/cambrian.jsonld"
,
"@id"
:
"<?php the_permalink();?>"
,
"appid"
:
"[修改这个参数]"
,
"title"
:
"<?php the_title();?>"
,
"images"
:
[
<
?
php
echo
get_Image
(
$
matches
[
0
]
)
;
?
>
]
,
"pubDate"
:
"<?php echo get_the_time('Y-m-d')."
T
".get_the_time('H:i:s');?>"
}
</script>
<?php
endif
;
?>
<
link
rel
=
"canonical"
href
=
"<?php the_permalink();?>"
/
>
<script
src
=
"//msite.baidu.com/sdk/c.js?appid=[修改这个参数]"
>
</script>
|
效果如下
如果想实现自动提交的效果,可以看一下这篇文章
这是语段引用