如何使用the_content()显示WordPress文章的主要内容
WordPress the_content
函数是用于显示 WordPress 文章或页面内容的核心函数。它从数据库中检索内容并将其输出到网页上。此功能可用于显示文章或页面的主要内容,包括文本、图片、视频以及已添加到内容编辑器的任何其他媒体。
开发人员可以使用该the_content
功能轻松输出文章或页面的主要内容,而无需手动检索和显示。这可以在创建模板或自定义 WordPress 网站布局时节省时间和精力。
推荐:[最新版]WordPress SEO插件Rank Math Pro
the_content()基本语法
描述
WordPress PHP the_content 过滤器允许您在从数据库检索帖子内容之后并在其显示在屏幕上之前修改帖子内容。
用法
add_filter('the_content', 'your_custom_function');
function your_custom_function($content) {
// your custom code here
return $content;
}
$content
(字符串):当前帖子的内容。
the_content
函数没有返回值,始终使用is_singular()
、in_the_loop()
和is_main_query()
条件以避免在自定义循环中无意地过滤内容
the_content()函数
the_content()
是 WordPress 中用于显示文章或页面主要内容的函数。它通常在主题的模板文件中使用,比如 single.php
(用于显示单篇文章)或 page.php
(用于显示单个页面)。这个函数会输出存储在数据库中的内容,并且支持一些过滤器和短代码,使其功能更为强大。(源文件可参考这里)
add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 1 );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we're inside the main loop in a single Post.
if ( is_singular() && in_the_loop() && is_main_query() ) {
return $content . esc_html__( 'I’m filtering the content inside the main loop', 'wporg');
}
return $content;
}
推荐:使用wp_set_post_tags()为WordPress帖子设置标签
如何使用the_content()
如何显示 WordPress 帖子的内容,此代码片段仅使用该函数输出 WordPress 帖子的内容the_content
。它将显示帖子的主要内容,包括已添加到帖子中的任何文本、图像或其他媒体。
<?php
the_content();
?>
如何自定义内容的显示,此代码片段使用该函数检索 WordPress 文章的内容get_the_content
,然后使用该wp_trim_words
函数将内容限制为 40 个字,并在末尾添加省略号。这对于显示文章内容的简要预览非常有用。
<?php
$content = get_the_content();
echo wp_trim_words( $content, 40, '...' );
?>
如何根据帖子类型有条件地显示内容,此代码片段使用语句检查当前帖子类型是否为“post” if
。如果是,则使用函数显示帖子内容the_content
。这对于根据帖子类型自定义内容显示方式非常有用。
<?php
if ( 'post' == get_post_type() ) {
the_content();
}
?>
在每篇文章末尾附加自定义文本,此代码将“感谢您的阅读!”附加到每篇文章的末尾。
add_filter('the_content', 'append_custom_text');
function append_custom_text($content) {
if (is_singular('post') && in_the_loop() && is_main_query()) {
$content .= '<p><strong>Thank you for reading!</strong></p>';
}
return $content;
}
在帖子底部添加社交分享按钮,此代码在帖子内容下方添加了社交分享按钮。
add_filter('the_content', 'add_social_sharing_buttons');
function add_social_sharing_buttons($content) {
if (is_singular('post') && in_the_loop() && is_main_query()) {
$sharing_buttons = '<div class="social-sharing-buttons">...</div>';
$content .= $sharing_buttons;
}
return $content;
}
在第二段之后显示广告,此代码在每篇文章的第二段后插入广告。
add_filter('the_content', 'insert_ad_after_second_paragraph');
function insert_ad_after_second_paragraph($content) {
if (is_singular('post') && in_the_loop() && is_main_query()) {
$ad_code = '<div class="ad-placeholder">...</div>';
$content = preg_replace('/(<p>.*<\/p>){2}/', '$0' . $ad_code, $content);
}
return $content;
}
使用自定义类包装图像,此代码使用名为“my-custom-image-class”的自定义类包装帖子内容中的所有图像。
add_filter('the_content', 'wrap_images_with_custom_class');
function wrap_images_with_custom_class($content) {
if (is_singular() && in_the_loop() && is_main_query()) {
$content = preg_replace('/<img([^>]+)>/', '<div class="my-custom-image-class"><img$1></div>', $content);
}
return $content;
}
从帖子内容中删除所有短代码,此代码会在显示帖子内容之前删除其中的所有短代码。
add_filter('the_content', 'remove_all_shortcodes');
function remove_all_shortcodes($content) {
if (is_singular() && in_the_loop() && is_main_query()) {
$content = strip_shortcodes($content);
}
return $content;
}
结论
总而言之,该the_content
函数是操作 WordPress 帖子或页面内容的强大工具。通过使用此函数,开发人员可以轻松修改主内容区域的输出,从而实现自定义格式、过滤等功能。它提供了一种灵活的方式来自定义内容的显示,而无需修改核心主题文件。
该函数能够接受过滤器和钩子,the_content
为扩展和自定义 WordPress 网站上内容的显示方式提供了广泛的可能性。无论您是初学者还是经验丰富的开发人员,了解如何使用和利用此功能都可以极大地提升您创建动态且引人入胜的内容的能力。
该the_content
功能对于 WordPress 开发人员来说是一个宝贵的工具,它提供了一种轻松操作和自定义网站内容显示的方法。通过学习如何有效地使用此功能,开发人员可以将他们的 WordPress 开发技能提升到一个新的水平,从而创建更具吸引力和活力的网站。
推荐:[最新版]WP Speed of Light Pro插件WordPress速度优化插件