/ 在文档加载完毕的时候遍历所有匹配文章的元素
jQuery(document).ready(function(){
jQuery('div.post').each(function() {
// 如果元素相应位置是文章 ID
var id = jQuery(this).attr('id');
if(/^post\-[0-9]+$/.test(id)) {
// 则为每个文章添加一个展开/折叠按钮
...
}
});
});
toggle.toggle(function() { // 展开
// 如果文章内容为空, 加载文章内容
if(jQuery('#' + id + ' .content').text() == '') {
...
}
// 显示文章内容, 并切换按钮样式
jQuery('#' + id + ' .content').slideDown();
jQuery(this).removeClass('collapse').addClass('expand');
},
function() { // 折叠
// 隐藏文章内容, 并切换按钮样式
jQuery('#' + id + ' .content').slideUp();
jQuery(this).removeClass('expand').addClass('collapse');
// 将按钮追加到文章标题前方
}).prependTo(jQuery('#' + id + ' h2'));
// 取得文章 ID
var postId = id.slice(5);
// 使用 AJAX 获取并处理文章内容
jQuery.ajax({
type: 'GET'
,url: '?action=load_post&id=' + postId
,cache: false
,dataType: 'html'
,contentType: 'application/json; charset=utf-8'
// 取得返回内容之前显示加载信息
,beforeSend: function(data){loadPostContent(id, '<p class="ajax-loader">Loading...</p>');}
// 获取文章内容成功, 更新文章内容
,success: function(data){loadPostContent(id, data);}
// 获取文章内容失败, 显示出错提示
,error: function(data){loadPostContent(id, '<p>Oops, failed to load data.</p>');}
});
function get_the_content($more_link_text = null, $stripteaser = 0) {
global $id, $post, $more, $page, $pages, $multipage, $preview;
// 设定 "查看全文" 的链接文案
if ( null === $more_link_text )
$more_link_text = __( '(more...)' );
// 返回内容
$output = '';
// More 标签是否存在的标记位
$hasTeaser = false;
// 如果文章要求输入密码, 并且在 Cookie 中找不到处理过的信息, 则返回要求输入密码的查看表单
if ( post_password_required($post) ) {
$output = get_the_password_form();
return $output;
}
// 请求的文章片段对应的页面大于最大页数 (即文章片段不存在), 则返回最大页码的文章片段
if ( $page > count($pages) )
$page = count($pages);
// 文章内容是最后分页中的文章片段
$content = $pages[$page-1];
// 如果文中有 More 标签, 要求切断文章并输出 "查看全文" 链接, 则重定义文章内容, 标记 More 标签存在
if ( preg_match('/<!--more(.*?)?-->/', $content, $matches) ) {
$content = explode($matches[0], $content, 2);
if ( !empty($matches[1]) && !empty($more_link_text) )
$more_link_text = strip_tags(wp_kses_no_null(trim($matches[1])));
$hasTeaser = true;
} else {
$content = array($content);
}
// 如果进行了文章切断处理, 且不存在分页要求,
if ( (false !== strpos($post->post_content, '<!-- noteaser -->') && ((!$multipage) || ($page==1))) )
$stripteaser = 1;
// 获取文章内容的第一部分; 如果在独立文章存在 Read more 和切断处理, 则文章内容为空
$teaser = $content[0];
if ( ($more) && ($stripteaser) && ($hasTeaser) )
$teaser = '';
$output .= $teaser;
// 如果文章分为多个片段, 在独立文章中拼接上第二部分, 摘要内容中显示 "阅读全文" 链接
if ( count($content) > 1 ) {
if ( $more ) {
$output .= '<span id="more-' . $id . '"></span>' . $content[1];
} else {
if ( ! empty($more_link_text) )
$output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>", $more_link_text );
$output = force_balance_tags($output);
}
}
if ( $preview ) // preview fix for javascript bug with foreign languages
$output = preg_replace_callback('/\%u([0-9A-F]{4})/', create_function('$match', 'return "" . base_convert($match[1], 16, 10) . ";";'), $output);
// 返回文章内容
return $preview;
}
function load_post() {
// 如果 action ID 是 load_post, 并且传入的必须参数存在, 则执行响应方法
if($_GET['action'] == 'load_post' && $_GET['id'] != '') {
$id = $_GET["id"];
$output = '';
// 获取文章对象
global $wpdb, $post;
$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id));
// 如果指定 ID 的文章存在, 则对他进行格式化
if($post) {
$content = $post->post_content;
$output = balanceTags($content);
$output = wpautop($output);
}
// 打印文章内容并中断后面的处理
echo $output;
die();
}
}
// 将接口加到 init 中
add_action('init', 'load_post');
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有