【问题标题】:limit text in multiple <p> tags in wordpress在wordpress中限制多个<p>标签中的文本
【发布时间】:2014-08-24 17:24:28
【问题描述】:

我正在写博客,想用“...”限制文本。但在管理帖子页面中,它不允许
标签只有 P 标签分隔段落。我的观点是,即使有 10 个 P 标签,我也想限制我的文本。用户可以转到该页面并查看完整的帖子,但在登录页面上它只显示有限的文字。

请帮忙。

【问题讨论】:

  • 这里的信息太少,无法理解您想要做什么并制定解决方案。请通过研究更新您的问题以及您迄今为止所做的尝试。

标签: jquery html wordpress tags


【解决方案1】:

嗯,您可以使用多种方法。其中之一显然是使用 the_excerpt();在您的模板中。如果您想在该摘录中添加更多字符(默认值除外) 那些)在你的functions.php文件中尝试这段代码

function new_excerpt_length($length) {
    return 50;
}
add_filter('excerpt_length', 'new_excerpt_length');

function the_titlesmall($before = '', $after = '', $echo = true, $length = false) { $title = get_the_title();

    if ( $length && is_numeric($length) ) {
        $title = substr( $title, 0, $length );
    }

    if ( strlen($title)> 0 ) {
        $title = apply_filters('the_titlesmall', $before . $title . $after, $before, $after);
        if ( $echo )
            echo $title;
        else
            return $title;
    }
}

只需将return 50 更改为您想要的任何长度

另一种选择:在模板中使用 content() 函数,并在 functions.php 页面中使用:

function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).' <a class="seemore" href="'. get_permalink($post->ID) . '">More</a>';
  } else {
    $content = implode(" ",$content);
  } 
  $content = preg_replace('/\[.+\]/','', $content);
  $content = apply_filters('the_content', $content); 
  $content = str_replace(']]>', ']]&gt;', $content);
  return $content;
}

现在你可以在你的模板中使用&lt;?php echo content(50); ?&gt;(或者用你想要的任何长度替换那50个)

最后,您还可以在帖子/页面编辑屏幕中使用MORE 标签

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-13
    • 2015-06-20
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多