File: /www/wwwroot/q.autos58.cn/wp-content/themes/kong20/inc/functions/seo-functions.php
<?php
if (!defined('ABSPATH')) {die;} // Cannot access directly.
function _the_head() {
_keywords();
_description();
}
add_action('wp_head', '_the_head',1);
function _get_delimiter()
{
return _hua('connector') ? _hua('connector') : '-';
}
function _title($echo = true)
{
global $paged, $post;
$html = '';
$t = trim(wp_title('', false));
if ($t) {
$html .= $t . _get_delimiter();
}
if (get_query_var('page')) {
$html .= '第' . get_query_var('page') . '页' . _get_delimiter();
}
$html .= get_bloginfo('name');
$seo_opt = _hua('seo');
if (is_home()) {
$seo_intit = (!empty($seo_opt['web_title'])) ? $seo_opt['web_title'] : 'Huatheme.com企业主题开发_专注于wordpress高端企业主题模板制作';
if ($seo_intit) {
$html = $seo_intit;
if ($paged > 1) {
$html .= _get_delimiter() . '最新发布';
}
} else {
if ($paged > 1) {
$html .= _get_delimiter() . '最新发布';
} else if (get_bloginfo('name')) {
$html .= _get_delimiter() . get_bloginfo('name');
}
}
}
if (is_category()) {
$cat_ID = get_query_var('cat');
$seo_str = get_term_meta($cat_ID, 'seo-title', true);
$cat_tit = ($seo_str) ? $seo_str : _get_tax_meta($cat_ID, 'title');
if ($cat_tit) {
$html = $cat_tit;
$html .= _get_delimiter() . get_bloginfo('name');
}
} elseif (is_tag()) {
$tagName = single_tag_title('', false);
$tagObject = get_term_by('name', $tagName, 'post_tag');
$tagID = $tagObject->term_id;
$seo_str = get_term_meta($tagID, 'seo-title', true);
$html = ($seo_str) ? trim($seo_str) : $tagName;
$html .= _get_delimiter() . get_bloginfo('name');
} elseif (is_singular() && get_post_meta($post->ID, 'post_titie_s', true)) {
$html = get_post_meta($post->ID, 'post_titie', true);
}
if ($paged > 1) {
$html .= _get_delimiter() . '第' . $paged . '页';
}
return $html;
}
/**
* @return [type] [description]
*/
function _keywords() {
global $s, $post;
$keywords = '';
if (is_singular()) {
if (get_the_tags($post->ID)) {
foreach (get_the_tags($post->ID) as $tag) {
$keywords .= $tag->name . ',';
}
}
foreach (get_the_category($post->ID) as $category) {
$keywords .= ' ';
}
if (get_post_meta($post->ID, 'post_keywords_s', true)) {
$the = trim(get_post_meta($post->ID, 'keywords', true));
if ($the) {
$keywords = $the;
}
} else {
$keywords = substr_replace($keywords, '', -2);
}
} elseif (is_home()) {
$seo_opt = _hua('seo');
$keywords = (!empty($seo_opt['web_keywords'])) ? $seo_opt['web_keywords'] : '暂无关键词,huatheme';
} elseif (is_tag()) {
$tagName = single_tag_title('', false);
$tagObject = get_term_by('name', $tagName, 'post_tag');
$tagID = $tagObject->term_id;
$seo_str = get_term_meta($tagID, 'seo-keywords', true);
$keywords = ($seo_str) ? trim($seo_str) : $tagName;
} elseif (is_category()) {
global $wp_query;
$cat_ID = get_query_var('cat');
$seo_str = get_term_meta($cat_ID, 'seo-keywords', true);
$keywords = ($seo_str) ? trim($seo_str) : trim(wp_title('', false));
} elseif (is_search()) {
$keywords = esc_html($s, 1);
} else {
$keywords = trim(wp_title('', false));
}
if ($keywords) {
echo "<meta name=\"keywords\" content=\"$keywords\">\n";
}
}
/**
* @return [type] [description]
*/
function _description() {
global $s, $post;
$description = '';
$blog_name = get_bloginfo('name');
if (is_singular()) {
if (!empty($post->post_excerpt)) {
$text = $post->post_excerpt;
} else {
$text = $post->post_content;
}
$description = trim(str_replace(array("\r\n", "\r", "\n", " ", " "), " ", str_replace("\"", "'", strip_tags($text))));
$description = substr_ext(strip_tags(strip_shortcodes($description)), 0, 140, 'utf-8', '...');
if (!($description)) {
$description = $blog_name . "-" . trim(wp_title('', false));
}
if (get_post_meta($post->ID, 'post_description_s', true)) {
$the = trim(get_post_meta($post->ID, 'description', true));
if ($the) {
$description = $the;
}
}
} elseif (is_home()) {
$seo_opt = _hua('seo');
$description = (!empty($seo_opt['web_description'])) ? $seo_opt['web_description'] : 'wordpress企业主题开发_专注于WordPress高端企业主题制作';
} elseif (is_tag()) {
$tagName = single_tag_title('', false);
$tagObject = get_term_by('name', $tagName, 'post_tag');
$tagID = $tagObject->term_id;
$seo_str = get_term_meta($tagID, 'seo-description', true);
$description = ($seo_str) ? trim($seo_str) : trim(wp_title('', false));
} elseif (is_category()) {
global $wp_query;
$cat_ID = get_query_var('cat');
$seo_str = get_term_meta($cat_ID, 'seo-description', true);
$description = ($seo_str) ? trim($seo_str) : str_replace(array("<p>","","</p>", "\r", "\n"),"",category_description());
} elseif (is_archive()) {
$description = $blog_name . "-" . trim(wp_title('', false));
} elseif (is_search()) {
$description = $blog_name . ": '" . esc_html($s, 1) . "' " . __('的搜索結果', 'haoui');
} elseif (is_tag()) {
} else {
$description = $blog_name . "'" . trim(wp_title('', false)) . "'";
}
$description = mb_substr($description, 0, _get_description_max_length(), 'utf-8');
echo "<meta name=\"description\" content=\"$description\">\n";
}
function substr_ext($str, $start = 0, $length, $charset = 'utf-8', $suffix = '') {
if (function_exists('mb_substr')) {
return mb_substr($str, $start, $length, $charset) . $suffix;
}
if (function_exists('iconv_substr')) {
return iconv_substr($str, $start, $length, $charset) . $suffix;
}
$re['utf-8'] = '/[-]|[?-?][?-?]|[?-?][?-?]{2}|[?-?][?-?]{3}/';
$re['gb2312'] = '/[-]|[?-?][?-?]/';
$re['gbk'] = '/[-]|[?-?][@-?]/';
$re['big5'] = '/[-]|[?-?]([@-~]|?-?])/';
preg_match_all($re[$charset], $str, $match);
$slice = join('', array_slice($match[0], $start, $length));
return $slice . $suffix;
}
function _get_description_max_length() {
return 200;
}