在Wordpress中更改the_content的长度

时间:2013-08-16 15:14:16

标签: wordpress wordpress-theming

我正在为客户建立一个网站,几乎所有东西都需要不同的长度。 the_excerpt标记已缩短为15个字,但我需要在主“更新”页面上缩短the_content(),但需要在实际帖子页面上以完整形式(无限制)工作。

我该怎么做呢?有谁知道是否有办法改变the_content输出PER页面?

我怎样才能创建一个基本上执行以下操作的“功能”

the_content("short")用于博客着陆页, the_content();正常输出在single.php上正常工作?

3 个答案:

答案 0 :(得分:1)

将以下代码放入主题functions.php

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

而不是20使用您想要的摘录长度

答案 1 :(得分:0)

您可以通过在the_excerpt()中添加以下代码来更改functions.php的字符长度:

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length');

答案 2 :(得分:0)

两种解决方案: 第一个解决方案:将过滤器应用于过滤器挂钩the_content,如下所示:     add_filter('the_content','filter_function_name') (放在functions.php中)。然后创建函数filter_function_name以查找调用的上下文,并条件化缩短返回的长度。有关将过滤器应用于the_content的更多信息,请参阅此处:http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

第二种解决方案: 不要使用the_content(),而是使用自己的函数,如sp_content()。

function sp_content($length) {
    if ($length == "") { $content = get_the_content();  }
    else { $content = substr(get_the_content(), 0, $length) }
    return $content;
}

甚至更容易:当您需要长内容时,请使用the_content(),在任何其他情况下,只需在创建自己的函数后使用short_content()

short_content() {
    substr(get_the_content())... etc.
}
相关问题