【问题标题】:Change Excerpt Length for Specific Page ID更改特定页面 ID 的摘录长度
【发布时间】:2013-12-19 02:36:00
【问题描述】:

我正在使用一个主题,该主题使用以下函数来设置所有页面上的帖子摘录长度:

//excerpt length

if ( !function_exists('custom_excerpt_length')):
    function custom_excerpt_length( $length ) { 
        return 90;
    }

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

function new_excerpt_more( $more ) {
    return '...';
}

add_filter('excerpt_more', 'new_excerpt_more');

endif;

我添加了新功能:

add_filter( 'get_the_excerpt', 'so20668477_get_the_excerpt', 10, 1 );
function so20668477_get_the_excerpt( $excerpt )
{
if( is_page( 39370 ) )
    $excerpt = apply_filters( 'the_content', get_the_content() );

return $excerpt;
}

但是,它不起作用,仍然在第 39370 页上显示帖子摘录。我只需要在页面 ID = 39370 上显示完整的帖子。不知道如何添加条件。任何帮助深表感谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    首先,我们强烈建议您始终使用大括号,即使在技术上是可选的情况下也是如此。

    不要这样做:

    if ( condition )
      return false;
    

    改为:

    if ( condition ) {
      return false;
    }
    

    关于您的问题:

    这个函数apply_filters( 'the_content', get_the_content() );得到的是内容的echo而不是内容,所以你的条件是正确的。 试试这个代码:

    if( is_page( 39370 ) ){
        $content = get_the_content();
        $content = apply_filters('the_content', $content);
        $excerpt = explode("[newpage]", $content);
    }
    
    return $excerpt;
    

    或者还有其他方法:

    if( is_page( 39370 ) ){
      ob_start();
      the_content();
      $content = ob_get_clean();
    }
    
     return $excerpt;
    

    我希望这个解决方案对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多