/
functions.php
115 lines (106 loc) · 2.98 KB
/
functions.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* Custom template tags for this theme.
*
* Eventually, some of the functionality here could be replaced by core features.
*
* @package AME2016
* https://amged.me/
*/
if ( ! function_exists( 'build_jsonld' ) ) :
/*
* build json-ld for SEO
*/
function build_jsonld($return=false){
require_once(get_template_directory() . '/jsonld.php');
$output = jsonLD();
if ($return)
{
return json_encode($output);
}
else
{
echo '<script type="application/ld+json">' . json_encode($output) . '</script>';
}
}
endif;
add_action( 'wp_head', 'build_jsonld' );
if ( ! function_exists( 'build_jsonldBreadcrumb' ) ) :
/*
build json-ld for SEO items list
https://amged.me/
*/
function build_jsonldBreadcrumb(){
$jsonLD["@context"] = "http://schema.org/";
$jsonLD["@type"] = "BreadcrumbList";
//---------------------------------
// pre-define some variables
//---------------------------------
$home_title = get_bloginfo('name');
$home_url = esc_url( home_url( '/' ) );
$blog_url = get_permalink( get_option( 'page_for_posts' ) );
$blog_title = get_the_title( get_option('page_for_posts', true) );
$url = $url ? $url : get_permalink();
$title = $title ? $title : get_the_title();
$itemList = array();
//----------------------------------
// Output
//-----------------------------------
$itemList[] = array(
"@type" => "ListItem",
"position" => 1,
"item" => array('@id' => $home_url ,'name' => $home_title),
);
//----------------------------------------------
// figure out the next number
// <meta property="position" content="{NUM}">
//----------------------------------------------
$next = 2;
if ( is_single() or is_home())
{
//-------------------------------------
// we have a blog "post"
// so we added the blog url (not home)
// so next is 3 not 2!
//--------------------------------------
$itemList[] = array(
"@type" => "ListItem",
"position" => 2,
"item" => array('@id' => $blog_url ,'name' => $blog_title),
);
$next = 3;
}
if ( is_single() or is_page() or is_tag() or is_category()) {
//----------------------------------
// is it a tag?
//----------------------------------
if ( is_tag() )
{
$title = single_tag_title("", false);
$tag_id = get_term_by('name', $title, 'post_tag');
$url = get_tag_link($tag_id->term_id);
}
if ( is_category() )
{
$title = single_cat_title("", false);
$category_id = get_cat_ID( $title );
$url = get_category_link( $category_id );
}
$itemList[] = array(
"@type" => "ListItem",
"position" => $next,
"item" => array('@id' => $url, 'name' => $title),
);
}
$jsonLD["itemListElement"] = $itemList;
/*echo '<pre>';
print_r(json_encode($jsonLD));
echo '</pre>';*/
if ( (is_page() OR is_single() or is_home() or is_tag() or is_category()) AND (! is_front_page()) ) {
echo '<script type="application/ld+json">';
echo json_encode($jsonLD);
echo '</script>';
}
}
endif;
add_action( 'wp_head', 'build_jsonldBreadcrumb' );