最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Distinguish between page and post in function

programmeradmin1浏览0评论

I'm trying to apply some text to the "Featured Image" box. It works fine for a custom post type, but I can't get a specific piece of text appearing on pages rather than posts.

add_filter( 'admin_post_thumbnail_html', 'foo' );
function foo( $content ) {
    global $post_type;
    if ( $post_type = 'page') {
        $content = '<p>foobar</p>' . $content;
     }
    else{
        $content = '<p>barfoo</p>' . $content;
     }
     return $content;
}

How can I get WordPress to identify if it is just a page?

I'm trying to apply some text to the "Featured Image" box. It works fine for a custom post type, but I can't get a specific piece of text appearing on pages rather than posts.

add_filter( 'admin_post_thumbnail_html', 'foo' );
function foo( $content ) {
    global $post_type;
    if ( $post_type = 'page') {
        $content = '<p>foobar</p>' . $content;
     }
    else{
        $content = '<p>barfoo</p>' . $content;
     }
     return $content;
}

How can I get WordPress to identify if it is just a page?

Share Improve this question edited Oct 21, 2019 at 14:03 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Oct 21, 2019 at 13:12 JohnGJohnG 3443 silver badges17 bronze badges 2
  • 1 You're missing an = sign in the if statement. – WebElaine Commented Oct 21, 2019 at 13:59
  • I am indeed - thanks very much! – JohnG Commented Oct 24, 2019 at 9:17
Add a comment  | 

1 Answer 1

Reset to default 0

By using post id you can get post type of current post/page. try below code. i have tested and it is working fine for me.

add_filter( 'admin_post_thumbnail_html', 'foo',10,3 );
function foo( $content, $post_id, $thumbnail_id ) {
   if ( get_post_type( $post_id ) == 'page' ) {
        $content = '<p>foobar</p>' . $content;
     }
    else{
        $content = '<p>barfoo</p>' . $content;
     }
     return $content;
}

Let me know if this works for you!

发布评论

评论列表(0)

  1. 暂无评论