chaihongjun.me

wordpress学习笔记之文章页标题the_title

文章标题语法格式:

<?php the_title($before,$after,$echo); ?>

首先三个参数都不是必选的。before和after分别是文章标题前后的内容,echo是一个布尔值,默认是true,表示显示标题的内容,如果为false则不显示标题内容了(连before和after的内容都不显示)。那么一般文章页面的标题可以这样写:

<?php the_title( '<h1 class="title">', '</h1>' ); ?>

解析出来是这样:

<h1 class="title">这里是文章标题</h1>

如果想让标题带超链接:

<h1 class="title">
<a href="<?php the_permalink()?>"><?php the_title() ?></a>
</h1>

解析出来就是:

<h1 class="title">
<a href="这里是超链接">这里是文章标题</a>
</h1>

或者:

<a href="<?php the_permalink()?>">
<?php the_title('<h1 class="entry-title">','</h1>') ?>
</a>

解析出来就是:

<a href="这里是超链接">
<h1 class="entry-title">这里是文章标题</h1>
</a>

前面的代码那个

<?php the_permalink()?>

表示的是当前post的链接

首先来看实例图解:

wordpress学习笔记之文章页标题-1

这里代码用的是

<?php the_title( '<h1 class="entry-title">', '</h1>',false); ?>

the_title函数第三个参数使用了false所以不显示标题了,如下图

wordpress学习笔记之文章页标题-2

下面这里图片显示,文章标题正常显示了

wordpress学习笔记之文章页标题-3

标题代码部分the_title函数第三个参数使用的是默认true

wordpress学习笔记之文章页标题-4

下面的文章标题显示蓝色是因为鼠标滑过了A标签,给了标题超链接

wordpress学习笔记之文章页标题-5

给文章标题加了the_permalink函数

wordpress学习笔记之文章页标题-6

wordpress目前默认的主题之一twentysixteen的文章页模板single.php内容如下:

<?php
/**
 * The template for displaying all single posts and attachments
 *
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */
get_header(); ?>
<div id="primary" class="content-area">
	<main id="main" class="site-main" role="main">
		<?php
		// Start the loop.
		while ( have_posts() ) : the_post();

			// Include the single post content template.
			get_template_part( 'template-parts/content', 'single' );

			// If comments are open or we have at least one comment, load up the comment template.
			if ( comments_open() || get_comments_number() ) {
				comments_template();
			}

			if ( is_singular( 'attachment' ) ) {
				// Parent post navigation.
				the_post_navigation( array(
					'prev_text' => _x( '<span class="meta-nav">Published in</span><span class="post-title">%title</span>', 'Parent post link', 'twentysixteen' ),
				) );
			} elseif ( is_singular( 'post' ) ) {
				// Previous/next post navigation.
				the_post_navigation( array(
					'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'twentysixteen' ) . '</span> ' .
						'<span class="screen-reader-text">' . __( 'Next post:', 'twentysixteen' ) . '</span> ' .
						'<span class="post-title">%title</span>',
					'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'twentysixteen' ) . '</span> ' .
						'<span class="screen-reader-text">' . __( 'Previous post:', 'twentysixteen' ) . '</span> ' .
						'<span class="post-title">%title</span>',
				) );
			}

			// End of the loop.
		endwhile;
		?>
	</main><!-- .site-main -->
	<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

模板描述是该模板显示单个post或者附件的内容,模板又继续调用template-parts/content-single.php作为具体文章页内容模板。

content-single.php的内容如下:

<?php
/**
 * The template part for displaying single posts
 *
 * @package WordPress
 * @subpackage Twenty_Sixteen
 * @since Twenty Sixteen 1.0
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header">
	<?php the_title( '<h1 class="entry-title">', '</h1>'); ?> 
 <!--  自定义添加修改的内容  <h1 class="entry-title">
   <a href="<?php the_permalink()?>"><?php the_title() ?></a>
</h1>
-->
	</header><!-- .entry-header -->

	<?php twentysixteen_excerpt(); ?>

	<?php twentysixteen_post_thumbnail(); ?>

	<div class="entry-content">
		<?php
			the_content();

			wp_link_pages( array(
				'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentysixteen' ) . '</span>',
				'after'       => '</div>',
				'link_before' => '<span>',
				'link_after'  => '</span>',
				'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'twentysixteen' ) . ' </span>%',
				'separator'   => '<span class="screen-reader-text">, </span>',
			) );

			if ( '' !== get_the_author_meta( 'description' ) ) {
				get_template_part( 'template-parts/biography' );
			}
		?>
	</div><!-- .entry-content -->

	<footer class="entry-footer">
		<?php twentysixteen_entry_meta(); ?>
		<?php
			edit_post_link(
				sprintf(
					/* translators: %s: Name of current post */
					__( 'Edit<span class="screen-reader-text"> "%s"</span>', 'twentysixteen' ),
					get_the_title()
				),
				'<span class="edit-link">',
				'</span>'
			);
		?>
	</footer><!-- .entry-footer -->
</article><!-- #post-## -->

根据wordpress的文档描述,结合上面默认主题模板,发现使用the_title()有几种方式。前提条件是要在The Loop中使用。

while ( have_posts() ) : the_post();




知识共享许可协议本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。作者:柴宏俊»