My problem:
我正在尝试创建一个过滤器,将帖子的特色图像添加到\\u内容中,这样我就可以在该图像之前显示\\u内容的第一段。What I basicly want to achieve:
<p>First Paragraph of the_content</p>
<img>The Post\'s Featured Image</img>
<p>The rest of the_content</p>
我想有人能帮助我。提前感谢!
My problem:
我正在尝试创建一个过滤器,将帖子的特色图像添加到\\u内容中,这样我就可以在该图像之前显示\\u内容的第一段。What I basicly want to achieve:
<p>First Paragraph of the_content</p>
<img>The Post\'s Featured Image</img>
<p>The rest of the_content</p>
我想有人能帮助我。提前感谢!
您可以使用“the\\u content”筛选器执行此操作:
add_filter( \'the_content\', \'insert_featured_image\', 20 );
function insert_featured_image( $content ) {
$content = preg_replace( "/<\\/p>/", "</p>" . get_the_post_thumbnail($post->ID, \'post-single\'), $content, 1 );
return $content;
}
当然,您可以向\\u post\\u thumbnail()函数添加选项,以定义要使用的缩略图大小,等等。。。http://codex.wordpress.org/Function_Reference/the_post_thumbnail从技术上讲,最快的解决方案是在内容中使用短代码。否则,您需要对regex有一个良好的处理,以便使用过滤器在段落之间转储图像。
最好的方法是将其添加到函数中。php
<?php
function featured_image($post) {
if (has_post_thumbnail($post->id))
the_post_thumbnail(\'large\');
}
add_shortcode(\'featured_image\', \'featured_image\');
?>
在内容的第一段之后键入[featured_image]
.functions.php
文件,以便打印变量的内容$img
就在第一段之后(即</p>
标签)在您的帖子内容中。将当前帖子缩略图/特色图像值传递给$img将在第一段之后打印该图像。
// Goes into functions.php file
// Adds $img content after after first paragraph (!.e. after first `</p>` tag)
add_filter(\'the_content\', function($content)
{
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$img = \'<img src="\'.$url.\'" alt="" title=""/>\';
$content = preg_replace(\'#(<p>.*?</p>)#\',\'$1\'.$img, $content, 1);
return $content;
});
已修改this 在第一段后附加变量为了定制特色图片前的段落数,我修改了上述答案,并修改了广告布局解决方案中的代码。
要使用:将以下两个代码段添加到函数中。php和自定义指示的数字,以控制段落数。
// Adds $featured_img and $caption after n paragraph
add_filter( \'the_content\', \'insert_featured_image\' );
function insert_featured_image( $content ) {
if ( has_post_thumbnail($post->ID) ) {
if ( has_post_thumbnail($post->ID) ) {
$caption = get_the_post_thumbnail_caption( $post );
} else {
$caption = \'\';
}
$featured_image = \'<div>\' . get_the_post_thumbnail( $post->ID, \'full\' ) . \'</div>\'.\'<div>\' . $caption . \'</div>\';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $featured_image, 2, $content ); // The numeral here controls the number of pars. Customise it to your needs
}
}
return $content;
}
// Parent function
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 );
}
我正在学习如何使用PHP构建html表单,方法是以“simplr表单注册”插件为例。我正在查看以下代码:$form .= apply_filters(\'simplr-reg-instructions\', __(\'Please fill out this form to sign up for this site\', \'simplr-reg\')); 您能解释一下这里发生了什么吗?函数的作用是什么,为什么需要“simpr reg指令”和“simpr reg”?为什么这句话不能简单地说:$