php - WordPress - 如何在 get_the_content() 的第一段之后添加内容
问题描述
有人可以告诉我如何更改我的代码以使用 get_the_content() 函数吗?我试图将“the_content”更改为“get_the_content”,但它不起作用。
//Insert ads after first paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
// Parent Function that makes the magic happen
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
解决方案
推荐阅读
- sql-server - 在“选择”中更好地查询“选择”
- javascript - Angular formatDate() 将我的值设置为以前的值
- c# - 为什么 Visual Studio 会自动使用所有异步方法创建 ApiController?如何在其中创建带有同步方法的 ApiController?
- ios - 为什么在 64 位架构的 iOS 中,数字指针的地址增加了 1 个字节,而不是 8 个字节?
- java - 尝试使用 thymeleaf 进行 Spring Boot 表单验证,但出现我无法弄清楚的错误
- javascript - 如何更改按钮中的文本颜色
- amazon-web-services - 如何提取从 AWS EventBridge 中继到 ECS Fargate 的事件
- optimization - CPLEX 和 MOSEK 与 YALMIP 给出不同的结果
- c# - 可以在不使用属性的情况下获取元组值吗?
- python - 尝试使用网络摄像头时 yolo 编译出现问题