新闻 |将function/filter应用于特定模板(特色图片功能过滤器)
Wordpress | Apply function/filter to specific template (featured image function filter)
我的调整插件中有以下代码。
add_filter( 'the_content', 'sqhse_news_featimgmove', 20 );
function sqhse_news_featimgmove( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID,'post-single', array( 'class' => "img-fluid img-rounded w-100")) . "<div class='clearfix' style='margin-bottom:10px;'></div>", $content, 1 );
return $content;
}
它的作用:
它在第一段之后添加了特色图片,这很棒,正是我需要的。
问题: 该代码适用于 single.php(很好,这正是我需要它的地方)但它也适用于单 training_courses.php(自定义 post 类型的模板)。
需要的帮助: 将代码应用到 single.php 而不是任何子单模板,例如 single-training_courses.php
这可以实现吗?如果可以,我该如何实现?
您可以使用 get_post_type()
WordPress 函数并将您的代码包装在 if 语句中,如下所示:
add_filter( 'the_content', 'sqhse_news_featimgmove', 20 );
function sqhse_news_featimgmove( $content ) {
if( get_post_type() == 'post' ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID,'post-single', array( 'class' => "img-fluid img-rounded w-100")) . "<div class='clearfix' style='margin-bottom:10px;'></div>", $content, 1 );
return $content;
}
return $content;
}
正如您所发现的,您正在使用的过滤器 the_content
将适用于所有内容区域。您需要添加一个条件来检查您所在的 post 类型并相应地进行调整。我的建议是使用 is_singular()
.
add_filter( 'the_content', 'sqhse_news_featimgmove', 20 );
function sqhse_news_featimgmove( $content ) {
if ( is_singular( 'post' ) ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID,'post-single', array( 'class' => "img-fluid img-rounded w-100")) . "<div class='clearfix' style='margin-bottom:10px;'></div>", $content, 1 );
}
return $content;
}
处理过滤器时,请确保始终 return 一个值。例如,如果您有条件,请将 return 语句放在它之外。
我的调整插件中有以下代码。
add_filter( 'the_content', 'sqhse_news_featimgmove', 20 );
function sqhse_news_featimgmove( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID,'post-single', array( 'class' => "img-fluid img-rounded w-100")) . "<div class='clearfix' style='margin-bottom:10px;'></div>", $content, 1 );
return $content;
}
它的作用: 它在第一段之后添加了特色图片,这很棒,正是我需要的。
问题: 该代码适用于 single.php(很好,这正是我需要它的地方)但它也适用于单 training_courses.php(自定义 post 类型的模板)。
需要的帮助: 将代码应用到 single.php 而不是任何子单模板,例如 single-training_courses.php
这可以实现吗?如果可以,我该如何实现?
您可以使用 get_post_type()
WordPress 函数并将您的代码包装在 if 语句中,如下所示:
add_filter( 'the_content', 'sqhse_news_featimgmove', 20 );
function sqhse_news_featimgmove( $content ) {
if( get_post_type() == 'post' ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID,'post-single', array( 'class' => "img-fluid img-rounded w-100")) . "<div class='clearfix' style='margin-bottom:10px;'></div>", $content, 1 );
return $content;
}
return $content;
}
正如您所发现的,您正在使用的过滤器 the_content
将适用于所有内容区域。您需要添加一个条件来检查您所在的 post 类型并相应地进行调整。我的建议是使用 is_singular()
.
add_filter( 'the_content', 'sqhse_news_featimgmove', 20 );
function sqhse_news_featimgmove( $content ) {
if ( is_singular( 'post' ) ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID,'post-single', array( 'class' => "img-fluid img-rounded w-100")) . "<div class='clearfix' style='margin-bottom:10px;'></div>", $content, 1 );
}
return $content;
}
处理过滤器时,请确保始终 return 一个值。例如,如果您有条件,请将 return 语句放在它之外。