显示功能的图像之前的文章标题

0

的问题

我一直想要得到的功能的图像来显示前后的内容。 我是新来的php这是我的代号:

function featured_image_before_title($title, $id) { 
 if (get_post_type($id) === 'post') {
        $featuredimage = get_the_post_thumbnail();
        $title = $featuredimage . $title;
        }
    return $title;
}

add_filter( 'the_title', 'featured_image_before_title', 10,2 );

上述工作在前端,但是当我去 管理>员额>所有员额标题 列显示一堆html markup前的实际员额的标题。

Html标记是缩小如下:

<img width="1280" height="150" src="http://example.com/wp-content/uploads/image.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="https://example.com/wp-content/uploads/image.jpg 1280w, https://example.com/wp-content/uploads/image-300x83.jpg 300w, https://example.com/wp-content/uploads/image-1024x284.jpg 1024w, https://example.com/wp-content/uploads/image-768x213.jpg 768w, https://example.com/wp-content/uploads/image-1080x300.jpg 1080w, https://example.com/wp-content/uploads/image-1280x356.jpg 1280w, https://example.com/wp-content/uploads/image-980x272.jpg 980w, https://example.com/wp-content/uploads/image-480x133.jpg 480w, https://example.com/wp-content/uploads/image-600x167.jpg 600w" sizes="(max-width: 1280px) 100vw, 1280px" /> POST TITLE 

我怎么可以插入功能的图像之前的文章标题?

php wordpress
2021-11-24 06:08:33
1

最好的答案

1

你必须使用 is_admin(). 试试下面的代码。

function featured_image_before_title($title, $id) { 

    if( is_admin() )
        return $title;

    if ( get_post_type( $id ) === 'post') {
        $featuredimage = get_the_post_thumbnail();
        $title = $featuredimage . $title;
    }
    return $title;
}
add_filter( 'the_title', 'featured_image_before_title', 10, 2 );
2021-11-24 06:26:15

谢谢! 这是因为管理使用的相同的职位类型的博客,因此需要区分他们吗?
PlainH2O

没有, the_title 还将呼吁管理方以及这就是为什么我们要补充的 is_admin() 条件。
Bhautik

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................