convert to plugins+theme
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* 供主题模板调用的内容助手函数(权重、字数、摘要、归档 URL)。
|
||||
* 函数名保持与主题历史一致,模板正在按原名调用它们。
|
||||
* 抽离自主题 functions.php。
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
function itstudio_get_post_weight($post_id, $field_name = 'itstudio_weight') {
|
||||
$post_id = (int) $post_id;
|
||||
if ($post_id <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!function_exists('get_field')) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$raw = get_field($field_name, $post_id, false);
|
||||
if ($raw === '' || $raw === null || !is_numeric($raw)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (int) $raw;
|
||||
}
|
||||
|
||||
function itstudio_get_post_char_count($post_id) {
|
||||
$post_id = (int) $post_id;
|
||||
if ($post_id <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$content = (string) get_post_field('post_content', $post_id);
|
||||
$plain_text = trim(wp_strip_all_tags($content));
|
||||
if ($plain_text === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (function_exists('mb_strlen')) {
|
||||
return (int) mb_strlen($plain_text, 'UTF-8');
|
||||
}
|
||||
|
||||
return (int) strlen($plain_text);
|
||||
}
|
||||
|
||||
function itstudio_get_post_excerpt_chars($post_id, $limit = 200) {
|
||||
$post_id = (int) $post_id;
|
||||
$limit = max(1, (int) $limit);
|
||||
if ($post_id <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$excerpt = (string) get_post_field('post_excerpt', $post_id);
|
||||
if (trim($excerpt) === '') {
|
||||
$excerpt = (string) get_post_field('post_content', $post_id);
|
||||
}
|
||||
|
||||
$excerpt = trim(wp_strip_all_tags($excerpt));
|
||||
if ($excerpt === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return wp_html_excerpt($excerpt, $limit, '...');
|
||||
}
|
||||
|
||||
function itstudio_get_archive_url_by_post_type($post_type) {
|
||||
$post_type = sanitize_key((string) $post_type);
|
||||
if ($post_type === '') {
|
||||
return home_url('/');
|
||||
}
|
||||
|
||||
$archive_url = get_post_type_archive_link($post_type);
|
||||
if ($archive_url) {
|
||||
return $archive_url;
|
||||
}
|
||||
|
||||
if ($post_type === 'news') {
|
||||
return home_url('/news/');
|
||||
}
|
||||
|
||||
if ($post_type === 'announcement') {
|
||||
return home_url('/announcement/');
|
||||
}
|
||||
|
||||
return home_url('/');
|
||||
}
|
||||
|
||||
function itstudio_get_archive_tag_filter_url($term, $post_type) {
|
||||
$term_obj = null;
|
||||
if ($term instanceof WP_Term) {
|
||||
$term_obj = $term;
|
||||
} else {
|
||||
$term_obj = get_term($term, 'post_tag');
|
||||
}
|
||||
|
||||
if (!($term_obj instanceof WP_Term) || is_wp_error($term_obj)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$post_type = sanitize_key((string) $post_type);
|
||||
if (!in_array($post_type, array('news', 'announcement'), true)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$archive_url = itstudio_get_archive_url_by_post_type($post_type);
|
||||
return add_query_arg(
|
||||
array(
|
||||
'tag' => $term_obj->slug,
|
||||
),
|
||||
$archive_url
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user