【问题标题】:WordPress: Displaying placeholder text by post formatWordPress:按帖子格式显示占位符文本
【发布时间】:2016-10-24 13:12:15
【问题描述】:

如果帖子的帖子格式为“画廊”,我想在“内容编辑器”中生成占位符文本。但我无法让它工作:

functions.php:

add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
function wpse57907_default_content( $content, $post ) {
    if ( 'post' == $post->post_type && has_post_format('gallery')) {
        $content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project&apos;s page. Please use the fields above for text.</i>';

    return $content;
    }
}

【问题讨论】:

    标签: php wordpress function post-format


    【解决方案1】:

    应该这样做:

    add_filter( 'default_content', 'wpse57907_default_content', 10, 2 );
    function wpse57907_default_content( $content, $post ) {
        $format = get_post_format($post->ID);
        if ( 'post' == $post->post_type && $format == 'gallery') {
            $content = '<i style="color:#999">Use this area to upload and edit images... any text put in here will NOT be generated on the project&apos;s page. Please use the fields above for text.</i>';
        }
        return $content;    
    }
    

    另外,请确保您的主题已设置为通过 add_theme_support 实际支持帖子格式,并且您的帖子具有正确的帖子格式(图库)。

    【讨论】:

    • 我似乎无法正常工作。现在,如果我将帖子格式更改为画廊,则不会出现任何消​​息。
    • 您是否尝试过在 if 语句之前执行 echo $format 以查看格式是否实际被正确提取?
    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2016-09-14
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多