WordPress里的the_content()函数分析

模板标签 the_content() 显示当前文章的内容。该标签必须在 WordPress 主循环(loop)中。若文章使用快速标签 <!--more--> 来截取摘要,the_content()标签将只在非单篇文章或非固定链接文章上显示 <!--more-->前的摘要部分。the_content()标签可包含一个规定 <!--more-->内容和样式的参数,该参数会生成“继续阅读全文”的链接。

关于 <!--more--> 标签有以下三条规定:

  • <!--more-->快速标签中的more前不得有空格。否则 <!--more-->将无法发挥作用。
  • <!--more-->快速标签无法在模板中运行(会被模板忽略),如single.php只会显示一篇文章。

改变“Read More”的样式

我们在博客首页显示日志摘要时,总希望读者能够点击日志标题或某个链接,继续阅读日志全文。WordPress就为我们提供了自定义“Read More(阅读全文)”链接样式的机会。

WordPress通过两种方法显示日志摘要。第一种,用模板标签 the_excerpt()代替 the_content(),之后我们在管理页面中“添加新文章”下的“摘要”中输入的内容就会出现在博客首页上。如果没有在“摘要”中输入内容,日志的前55个单词会作为摘要被显示在首页上。当访问者阅读摘要后,希望了解更多相关信息时,可以点击日志标题阅读全文。

而在WordPress中,显示日志的最常用方法是:保留the_content()模板标签,在日志中选择一个适当的位置(预设一个摘要截取位置),插入一个名为more的快速标签(quicktag)。

控制板“添加新文章”中编辑窗口上方的小图标被称为快速标签。这些图标包括加粗、斜体、添加链接等,以及大名鼎鼎的“插入more标签”。把光标放在日志中希望结束摘要的位置上,点击“(插入more标签)”。在日志被截断的地方会插入类似下面的代码:

and I told him that he should get moving or I‘d be
on him like a limpet.  He looked at me with shock on
his face and said
<!--more-->

代码后是日志的剩余部分,但如果在存档、类别、首页以及搜索结果等非单页型/非永久链接型页面上,日志会只显示“more”标签前的内容,作为摘要。

模板标签the_content()的参数如下:

<?php
	the_content( $more_link_text , $strip_teaser, $more_file );
?>

$more_link_text将链接文本设置为类似“Read More”的内容。第二个参数$strip_teaser决定是(TRUE)否(FALSE)隐藏“more”链接,该参数的默认值为FALSE——不隐藏more链接。最后一个参数$more_file将链接连接到我们所希望的“Read More”被链接到的内容中。默认情况下,$more_file链接到当前日志文件。

如果不希望显示摘要,那么可以将index.php中的the_content();更改为(例如让第二个参数控制属性值):

the_content(‘‘,FALSE,‘‘);

或者在在 <!--more-->后的日志正文中立即包含<!--noteaser-->。

默认情况下,访问者点击“Read More”链接时,网页会加载日志内容并将访问者带领到日志中<--more-->标签的位置上。如果我们不希望将访问者导向到这个位置,可以在主题的functions.php文件中加入以下代码:

function remove_more_jump_link($link)
{
	$offset = strpos($link, ‘#more-‘);
	if ($offset)
    {
		$end = strpos($link, ‘"‘,$offset);
	}
	if ($end)
    {
		$link = substr_replace($link, ‘‘, $offset, $end-$offset);
	}
	return $link;
}
add_filter(‘the_content_more_link‘, ‘remove_more_jump_link‘);

在WP 2.7.1以及之前的版本中,可以在wp-includes/post-template.php中编辑以下代码行,以此改变控制more指向内容的默认函数(注意:在WP 2.1之前,以下代码出现在 wp-includes/template-functions-post.php中)。

注意:升级WordPress时,该文件会被复原,因此我们需要保存所做修改,升级完毕后再重新修改文件。

我们需要将:

$output .= ‘ <a href="‘. get_permalink() ."#more-$id">$more_link_text</a>";

改为

$output .= ‘ <a href="‘. get_permalink() ."">$more_link_text</a>";

$output .= ‘ $more_link_text‘;

具体实现函数如下:

/**
 * Retrieve the post content.
 *
 * @since 0.71
 *
 * @param string $more_link_text Optional. Content for when there is more text.
 * @param string $stripteaser Optional. Teaser content before the more text.
 * @return string
 */
function get_the_content($more_link_text = null, $stripteaser = 0) {
	global $id, $post, $more, $page, $pages, $multipage, $preview, $pagenow;
	if ( null === $more_link_text )
		$more_link_text = __( ‘(more...)‘ );
	$output = ‘‘;
	$hasTeaser = false;
	// If post password required and it doesn‘t match the cookie.
	if ( post_password_required($post) ) {
		$output = get_the_password_form();
		return $output;
	}
	if ( $page > count($pages) ) // if the requested page doesn‘t exist
		$page = count($pages); // give them the highest numbered page that DOES exist
	$content = $pages[$page-1];
	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;
	$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" target="_blank">$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 $output;
}

如果想点击 more 时弹出新页面,可以修改以上函数,加入 target="_blank" 即可。

了解“Read More”的运行原理后,我们可以尝试把“Read More”内容变得更有趣些,激发访问者的阅读兴趣。

经过设计, the_content()标签包含了一个可以设计<!--more--> 内容和样式的参数,而<!--more--> 标签生成“continue reading(继续阅读全文)”的链接。

默认情况下,一个带有“more”的摘要可以是下面这样:

and I told him that he should get moving or I‘d be on him
like a limpet. He looked at me with shock on his face and
said more...

如果希望将more换成其它单词,只要在标签中输入新单词就可以了:

<?php the_content(‘Read on...‘); ?>

也可以将more换成幽默的句子:

<?php the_content(‘...on the edge of your seat? Click
here to solve the mystery.‘); ?>

还可以在标签中设计文本样式:

<?php the_content(‘<span class="moretext">...on the edge of
your seat? Click here to solve the mystery.</span>‘); ?>

之后在style.css样式表单中,将moretext设为自己想要显示的内容。下面的摘要示例使用了加粗字体与斜体,比默认文本字号稍小,示例用font-variant: small-caps强制摘要内容显示为小写字母:

and I told him that he should get moving or I‘d be on him
like a limpet. He looked at me with shock on his face and
said ...On the Edge of Your Seat? Click Here to Solve the
Mystery.

有些用户不希望在摘要中显示“Read More”等文本,他们希望用扩展字符或HTML字符实体将读者导向到日志全文。

<?php the_content(‘» » » »‘); ?>

模板标签the_content() 中还有一个参数可以在more的位置上显示日志标题。用the_title()标签包含日志标题:

<?php the_content("...continue reading the story
called " . get_the_title(‘‘, ‘‘, false)); ?>

根据上文的描述,我们通常从模板中调用带有标准文本的 the_content()。但我们可以为一些日志设置其它的“more”显示方式。在可视化编辑器中,输入<!--more Your custom text -->。

CSS为我们提供了无限的设计可能,WordPress也允许我们在很多模板标签中使用图片,包括more标签。有两种方法可以在more标签中插入图片。简单的方法是——在 模板标签the_content() 中展示图片。

<?php the_content(‘Read more...<img src="/images/leaf.gif"
alt="read more" title="Read more..." />‘); ?>
皇家娱乐城

注意:上述代码在图片标签中使用了ALT和TITLE属性。这是为了符合网络标准以及保证图片的可访问性,因为图片不仅是图片,同时也是一个链接。

我们甚至可以根据上一个章节中的描述进一步改造图片和more标签。如果只想使用图片二不想显示“Read More”,可以删除“Read More”字样。

第二个示例使用的是CSS背景图片。我们在之前的例子中以及论述过怎样使用样式类。这个例子稍微复杂一些。容器的样式必须设为允许背景图片显示在文本后。如果我们用上一个示例做背景图,其style.css样式表单应该显示为:

.moretext {
   width: 100px;
   height: 45px;
   background:url(/images/leaf.gif) no-repeat right middle;
   padding: 10px 50px 15px 5px}
}

页面右边50像素的内边距能够保证文本与图片间的距离,保证两者不相覆盖。而高度则保证容器有足够的宽度容纳图片,因为背景图并不是“实际存在”的,也不可能与容器的边框相抵。我们需要将图片的大小和形状都考虑进去,用最适当的方式显示图片。

掌握这其中的基本原理后,我们就可以随心所欲地开始设计了。

切记,网站首页 ( is_home() == TRUE )是不显示 <!--more-->标签的,除非我们用以下代码来激活显示:

<?php
global $more;
$more = 0;
?>

总结,模板标签-the_content()用法如下:

<?php the_content( $more_link_text, $strip_teaser, $more_file ); ?>
  • $more_link_text: (字符串)(可选)“more”链接的链接文本, 默认值: ‘(more...)‘
  • $strip_teaser:(布尔型)(可选)显示(FALSE)或隐藏(TRUE)more链接前的文本。默认值:FALSE
  • $more_file: (字符串)(可选)more链接所指向的文件 默认值:当前文件

使用以下方式在“More”中加入标题,在the_title()标签和display参数的帮助下,若文章使用 ,该示例可以显示"Continue reading ACTUAL POST TITLE"(继续阅读当前文章标题)。

<?php the_content("Continue reading " . the_title(‘‘, ‘‘, false)); ?>

如果the_content()不能按计划运行(如当你希望显示<!--more--> 标签前的内容,the_content()却显示了全文内容),你可以用全局变量$more改写这一行为:

<?php
global $more; // Declare global $more (before the loop).
$more = 0; // Set (inside the loop) to display content above the more tag.
the_content("More...");
?>

如果需要显示所有正文:

<?php
global $more; // Declare global $more (before the loop).
$more = 1; // Set (inside the loop) to display all content, including text below more.
the_content();
?>

你可以用get_the_content函数返回文章内容的值,而非直接输出文章内容。示例如下:

<?php $content = get_the_content(); ?>

请注意!get_the_content 无法进行以下操作,因此你最好手动添加以下代码,直到核心程序升级成功:

<?php
$content = apply_filters(‘the_content‘, $content);
$content = str_replace(‘]]>‘, ‘]]>‘, $content);
?>

the_content()位于wp-includes/post-template.php

时间: 2024-08-07 12:02:54

WordPress里的the_content()函数分析的相关文章

说说WordPress的主查询函数-query_posts()

今天说说WordPress 的主查询函数 -query_posts(),因为我正在制作的主题里面多次用到了这个函数 . query_posts()查询函数决定了哪些文章出现在WordPress 主 循环(loop)中,正因为如此,query_posts函数仅用于修改主页循环(Loop),而不是在页面上生成次级循环.如果你希望在主循环外另外生 成循环,应该新建独立的WP_Query对象,用这些对象生成循环.在主循环外的循环上使用query_posts会导致主循环运行偏差,并可能在页面上 显示出你不

(转)WordPress的主查询函数-query_posts()

今天说说WordPress 的主查询函数 -query_posts(),因为我正在制作的主题里面多次用到了这个函数 . query_posts()查询函数决定了哪些文章出现在WordPress 主 循环(loop)中,正因为如此,query_posts函数仅用于修改主页循环(Loop),而不是在页面上生成次级循环.如果你希望在主循环外另外生 成循环,应该新建独立的WP_Query对象,用这些对象生成循环.在主循环外的循环上使用query_posts会导致主循环运行偏差,并可能在页面上 显示出你不

如何验证一个地址可否使用—— MmIsAddressValid函数分析

又是一篇内核函数分析的博文,我个人觉得Windows的内核是最好的老师,当你想实现一个功能之前可以看看Windows内核是怎么做的,说不定就有灵感呢:) 首先看下官方的注释说明: /*++ Routine Description: For a given virtual address this function returns TRUE if no page fault will occur for a read operation on the address, FALSE otherwis

page_address()函数分析--如何通过page取得虚拟地址

由于X86平台上面,内存是划分为低端内存和高端内存的,所以在两个区域内的page查找对应的虚拟地址是不一样的. 一. x86上关于page_address()函数的定义 在include/linux/mm.h里面,有对page_address()函数的三种宏定义,主要依赖于不同的平台: 首先来看看几个宏的定义:CONFIG_HIGHMEM:顾名思义,就是是否支持高端内存,可以查看config文件,一般推荐内存超过896M的时候,才配置为支持高端内存.WANT_PAGE_VIRTUAL:X86平台

Oracle官网JNI简介和接口函数分析

第一章 概述 本章主要介绍JNI(Java Native Interface),JNI是一种本地编程接口.它允许运行在JAVA虚拟机中的JAVA代码和用其他编程语言,诸如C语言.C++.汇编,写的应用和库之间的交互操作. JNI的最大优势在于没有强加任何限制在JAVA虚拟机的下层实现上,因此,JAVA虚拟机供应商能够提供JNI的支持而不影响虚拟机的其他部分,程序员只需写出一个版本的本地应用和库,就可使之运行在一切支持JNI的JAVA虚拟机上. 本章包含了以下的要点: ? JNI概述 ? 目标 ?

linux 内核移植(七)——rest_init函数分析

代码在start_kernel函数运行的最后到了rest_init()函数中 1:rest_init()函数分析 (1)rest_init中调用kernel_thread函数启动了2个内核线程,分别是:kernel_init和kthreadd (2)调用schedule函数开启了内核的调度系统,从此linux系统开始转起来了. (3)rest_init最终调用cpu_idle函数结束了整个内核的启动.也就是说linux内核最终结束了一个函数cpu_idle.这个函数里面肯定是死循环. (4)简单

如何验证一个地址可否使用——MmIsAddressValid函数分析

又是一篇内核函数分析的博文,我个人觉得Windows的内核是最好的老师,当你想实现一个功能之前可以看看Windows内核是怎么做的,说不定就有灵感呢:) 首先看下官方的注释说明: /*++ Routine Description: For a given virtual address this function returns TRUE if no page fault will occur for a read operation on the address, FALSE otherwis

linux内核启动第二阶段之setup_arch()函数分析-2.6.36

执行setup_arch()函数 回到start_kernel当中,569行,调用setup_arch函数,传给他的参数是那个未被初始化的内部变量command_line.这个setup_arch()函数是start_kernel阶段最重要的一个函数,每个体系都有自己的setup_arch()函数,是体系结构相关的,具体编译哪个体系的setup_arch()函数,由顶层Makefile中的ARCH变量决定: 它首先通过检测出来的处理器类型进行处理器内核的初始化,然后通过 bootmem_init

rest_init函数分析(续)

copy from:https://yq.aliyun.com/articles/559628 本文主要阐述,内核态,怎么样启动到用户态的; 代码在start_kernel函数运行的最后到了rest_init()函数中 1:rest_init()函数分析 (1)rest_init中调用kernel_thread函数启动了2个内核线程,分别是:kernel_init和kthreadd (2)调用schedule函数开启了内核的调度系统,从此linux系统开始转起来了. (3)rest_init最终