本文整理汇总了PHP中TimberHelper::trim_words方法的典型用法代码示例。如果您正苦于以下问题:PHP TimberHelper::trim_words方法的具体用法?PHP TimberHelper::trim_words怎么用?PHP TimberHelper::trim_words使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TimberHelper
的用法示例。
在下文中一共展示了TimberHelper::trim_words方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_preview
/**
* ## get a preview of your post, if you have an excerpt it will use that,
* ## otherwise it will pull from the post_content
* <p>{{post.get_preview(50)}}</p>
*/
function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true)
{
$text = '';
$trimmed = false;
if (isset($this->post_excerpt) && strlen($this->post_excerpt)) {
if ($force) {
$text = TimberHelper::trim_words($this->post_excerpt, $len, false);
$trimmed = true;
} else {
$text = $this->post_excerpt;
}
}
if (!strlen($text)) {
$text = TimberHelper::trim_words($this->get_content(), $len, false);
$trimmed = true;
}
if (!strlen(trim($text))) {
return $text;
}
if ($strip) {
$text = trim(strip_tags($text));
}
if (strlen($text)) {
$text = trim($text);
$last = $text[strlen($text) - 1];
if ($last != '.' && $trimmed) {
$text .= ' … ';
}
if (!$strip) {
$last_p_tag = strrpos($text, '</p>');
if ($last_p_tag !== false) {
$text = substr($text, 0, $last_p_tag);
}
if ($last != '.' && $trimmed) {
$text .= ' … ';
}
}
if ($readmore) {
$text .= ' <a href="' . $this->get_permalink() . '" class="read-more">' . $readmore . '</a>';
}
if (!$strip) {
$text .= '</p>';
}
}
return $text;
}
示例2: add_timber_filters
/**
*
*
* @param Twig_Environment $twig
* @return Twig_Environment
*/
function add_timber_filters($twig)
{
/* image filters */
$twig->addFilter(new Twig_SimpleFilter('resize', array('TimberImageHelper', 'resize')));
$twig->addFilter(new Twig_SimpleFilter('retina', array('TimberImageHelper', 'retina_resize')));
$twig->addFilter(new Twig_SimpleFilter('letterbox', array('TimberImageHelper', 'letterbox')));
$twig->addFilter(new Twig_SimpleFilter('tojpg', array('TimberImageHelper', 'img_to_jpg')));
/* debugging filters */
$twig->addFilter(new Twig_SimpleFilter('docs', 'twig_object_docs'));
$twig->addFilter(new Twig_SimpleFilter('get_class', 'get_class'));
$twig->addFilter(new Twig_SimpleFilter('get_type', 'get_type'));
$twig->addFilter(new Twig_SimpleFilter('print_r', function ($arr) {
return print_r($arr, true);
}));
$twig->addFilter(new Twig_SimpleFilter('print_a', function ($arr) {
return '<pre>' . self::object_docs($arr, true) . '</pre>';
}));
/* other filters */
$twig->addFilter(new Twig_SimpleFilter('stripshortcodes', 'strip_shortcodes'));
$twig->addFilter(new Twig_SimpleFilter('array', array($this, 'to_array')));
$twig->addFilter(new Twig_SimpleFilter('excerpt', 'wp_trim_words'));
$twig->addFilter(new Twig_SimpleFilter('function', array($this, 'exec_function')));
$twig->addFilter(new Twig_SimpleFilter('pretags', array($this, 'twig_pretags')));
$twig->addFilter(new Twig_SimpleFilter('sanitize', 'sanitize_title'));
$twig->addFilter(new Twig_SimpleFilter('shortcodes', 'do_shortcode'));
$twig->addFilter(new Twig_SimpleFilter('time_ago', array($this, 'time_ago')));
$twig->addFilter(new Twig_SimpleFilter('wpautop', 'wpautop'));
$twig->addFilter(new Twig_SimpleFilter('relative', function ($link) {
return TimberURLHelper::get_rel_url($link, true);
}));
$twig->addFilter(new Twig_SimpleFilter('date', array($this, 'intl_date')));
$twig->addFilter(new Twig_SimpleFilter('truncate', function ($text, $len) {
return TimberHelper::trim_words($text, $len);
}));
/* actions and filters */
$twig->addFunction(new Twig_SimpleFunction('action', function ($context) {
$args = func_get_args();
array_shift($args);
$args[] = $context;
call_user_func_array('do_action', $args);
}, array('needs_context' => true)));
$twig->addFilter(new Twig_SimpleFilter('apply_filters', function () {
$args = func_get_args();
$tag = current(array_splice($args, 1, 1));
return apply_filters_ref_array($tag, $args);
}));
$twig->addFunction(new Twig_SimpleFunction('function', array(&$this, 'exec_function')));
$twig->addFunction(new Twig_SimpleFunction('fn', array(&$this, 'exec_function')));
$twig->addFunction(new Twig_SimpleFunction('shortcode', 'do_shortcode'));
/* TimberObjects */
$twig->addFunction(new Twig_SimpleFunction('TimberPost', function ($pid, $PostClass = 'TimberPost') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $PostClass($p);
}
return $pid;
}
return new $PostClass($pid);
}));
$twig->addFunction(new Twig_SimpleFunction('TimberImage', function ($pid, $ImageClass = 'TimberImage') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $ImageClass($p);
}
return $pid;
}
return new $ImageClass($pid);
}));
$twig->addFunction(new Twig_SimpleFunction('TimberTerm', function ($pid, $TermClass = 'TimberTerm') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $TermClass($p);
}
return $pid;
}
return new $TermClass($pid);
}));
$twig->addFunction(new Twig_SimpleFunction('TimberUser', function ($pid, $UserClass = 'TimberUser') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $UserClass($p);
}
return $pid;
}
return new $UserClass($pid);
}));
/* TimberObjects Alias */
$twig->addFunction(new Twig_SimpleFunction('Post', function ($pid, $PostClass = 'TimberPost') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $PostClass($p);
}
return $pid;
}
//.........这里部分代码省略.........
示例3: get_preview
/**
* get a preview of your post, if you have an excerpt it will use that,
* otherwise it will pull from the post_content.
* If there's a <!-- more --> tag it will use that to mark where to pull through.
* @api
* @example
* ```twig
* <p>{{post.get_preview(50)}}</p>
* ```
* @param int $len The number of words that WP should use to make the tease. (Isn't this better than [this mess](http://wordpress.org/support/topic/changing-the-default-length-of-the_excerpt-1?replies=14)?). If you've set a post_excerpt on a post, we'll use that for the preview text; otherwise the first X words of the post_content
* @param bool $force What happens if your custom post excerpt is longer then the length requested? By default (`$force = false`) it will use the full `post_excerpt`. However, you can set this to true to *force* your excerpt to be of the desired length
* @param string $readmore The text you want to use on the 'readmore' link
* @param bool $strip Strip tags? yes or no. tell me!
* @return string of the post preview
*/
function get_preview($len = 50, $force = false, $readmore = 'Read More', $strip = true)
{
$text = '';
$trimmed = false;
if (isset($this->post_excerpt) && strlen($this->post_excerpt)) {
if ($force) {
$text = TimberHelper::trim_words($this->post_excerpt, $len, false);
$trimmed = true;
} else {
$text = $this->post_excerpt;
}
}
if (!strlen($text) && preg_match('/<!--\\s?more(.*?)?-->/', $this->post_content, $readmore_matches)) {
$pieces = explode($readmore_matches[0], $this->post_content);
$text = $pieces[0];
if ($force) {
$text = TimberHelper::trim_words($text, $len, false);
$trimmed = true;
}
$text = do_shortcode($text);
}
if (!strlen($text)) {
$text = TimberHelper::trim_words($this->get_content(), $len, false);
$trimmed = true;
}
if (!strlen(trim($text))) {
return trim($text);
}
if ($strip) {
$text = trim(strip_tags($text));
}
if (strlen($text)) {
$text = trim($text);
$last = $text[strlen($text) - 1];
if ($last != '.' && $trimmed) {
$text .= ' …';
}
if (!$strip) {
$last_p_tag = strrpos($text, '</p>');
if ($last_p_tag !== false) {
$text = substr($text, 0, $last_p_tag);
}
if ($last != '.' && $trimmed) {
$text .= ' … ';
}
}
$read_more_class = apply_filters('timber/post/get_preview/read_more_class', "read-more");
if ($readmore && isset($readmore_matches) && !empty($readmore_matches[1])) {
$text .= ' <a href="' . $this->get_permalink() . '" class="' . $read_more_class . '">' . trim($readmore_matches[1]) . '</a>';
} elseif ($readmore) {
$text .= ' <a href="' . $this->get_permalink() . '" class="' . $read_more_class . '">' . trim($readmore) . '</a>';
}
if (!$strip && $last_p_tag && (strpos($text, '<p>') || strpos($text, '<p '))) {
$text .= '</p>';
}
}
return trim($text);
}
示例4: add_twig_filters
/**
* @param Twig_Environment $twig
* @return Twig_Environment
*/
function add_twig_filters($twig)
{
/* image filters */
$twig->addFilter('resize', new Twig_Filter_Function(array('TimberImageHelper', 'resize')));
$twig->addFilter('letterbox', new Twig_Filter_Function('wp_resize_letterbox'));
$twig->addFilter('tojpg', new Twig_Filter_Function(array('TimberImageHelper', 'img_to_jpg')));
$twig->addFilter('get_src_from_attachment_id', new Twig_Filter_Function('twig_get_src_from_attachment_id'));
/* debugging filters */
$twig->addFilter('docs', new Twig_Filter_function('twig_object_docs'));
$twig->addFilter('get_class', new Twig_Filter_Function('twig_get_class'));
$twig->addFilter('get_type', new Twig_Filter_Function('twig_get_type'));
$twig->addFilter('print_r', new Twig_Filter_Function('twig_print_r'));
$twig->addFilter('print_a', new Twig_Filter_Function('twig_print_a'));
/* other filters */
$twig->addFilter('stripshortcodes', new Twig_Filter_Function('strip_shortcodes'));
$twig->addFilter('array', new Twig_Filter_Function(array($this, 'to_array')));
$twig->addFilter('excerpt', new Twig_Filter_Function('twig_make_excerpt'));
$twig->addFilter('function', new Twig_Filter_Function(array($this, 'exec_function')));
$twig->addFilter('path', new Twig_Filter_Function('twig_get_path'));
$twig->addFilter('pretags', new Twig_Filter_Function(array($this, 'twig_pretags')));
$twig->addFilter('sanitize', new Twig_Filter_Function('sanitize_title'));
$twig->addFilter('shortcodes', new Twig_Filter_Function('twig_shortcodes'));
$twig->addFilter('time_ago', new Twig_Filter_Function('twig_time_ago'));
$twig->addFilter('twitterify', new Twig_Filter_Function(array('TimberHelper', 'twitterify')));
$twig->addFilter('twitterfy', new Twig_Filter_Function(array('TimberHelper', 'twitterify')));
$twig->addFilter('wp_body_class', new Twig_Filter_Function('twig_body_class'));
$twig->addFilter('wpautop', new Twig_Filter_Function('wpautop'));
$twig->addFilter('relative', new Twig_Filter_Function(function ($link) {
return TimberHelper::get_rel_url($link, true);
}));
$twig->addFilter('truncate', new Twig_Filter_Function(function ($text, $len) {
return TimberHelper::trim_words($text, $len);
}));
/* actions and filters */
$twig->addFunction(new Twig_SimpleFunction('action', function () {
call_user_func_array('do_action', func_get_args());
}));
$twig->addFilter(new Twig_SimpleFilter('apply_filters', function () {
$args = func_get_args();
$tag = current(array_splice($args, 1, 1));
return apply_filters_ref_array($tag, $args);
}));
$twig->addFunction(new Twig_SimpleFunction('function', array(&$this, 'exec_function')));
$twig->addFunction(new Twig_SimpleFunction('fn', array(&$this, 'exec_function')));
/* TimberObjects */
$twig->addFunction(new Twig_SimpleFunction('TimberPost', function ($pid, $PostClass = 'TimberPost') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $PostClass($p);
}
return $pid;
}
return new $PostClass($pid);
}));
$twig->addFunction(new Twig_SimpleFunction('TimberImage', function ($pid, $ImageClass = 'TimberImage') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $ImageClass($p);
}
return $pid;
}
return new $ImageClass($pid);
}));
$twig->addFunction(new Twig_SimpleFunction('TimberTerm', function ($pid, $TermClass = 'TimberTerm') {
if (is_array($pid) && !TimberHelper::is_array_assoc($pid)) {
foreach ($pid as &$p) {
$p = new $TermClass($p);
}
return $pid;
}
return new $TermClass($pid);
}));
/* bloginfo and translate */
$twig->addFunction('bloginfo', new Twig_SimpleFunction('bloginfo', function ($show = '', $filter = 'raw') {
return get_bloginfo($show, $filter);
}));
$twig->addFunction('__', new Twig_SimpleFunction('__', function ($text, $domain = 'default') {
return __($text, $domain);
}));
$twig = apply_filters('get_twig', $twig);
return $twig;
}
示例5: meta_description
/**
* add meta_description
*
* hook after post has loaded to add a unique meta-description
*
*/
public static function meta_description()
{
$post = new \TimberPost();
// check for custom field
$description = wptexturize($post->get_field('meta_description'));
// else use preview
if (empty($description)) {
$description = str_replace('', "'", $post->get_preview(40, true, false, true));
}
// finally use the blog description
if (empty($description)) {
$description = get_bloginfo('description', 'display');
}
$description = esc_attr($description);
// limit to SEO recommended length
if (strlen($description) > 155) {
$description = substr($description, 0, 155);
$description = \TimberHelper::trim_words($description, str_word_count($description) - 1);
}
return $description;
}
示例6: meta_description
/**
* add meta_description
*
* hook after post has loaded to add a unique meta-description
*
*/
public static function meta_description()
{
$post = new \TimberPost();
// check for custom field
$description = wptexturize($post->get_field('meta_description'));
if (is_tax()) {
if ($temp = term_description(get_queried_object(), get_query_var('taxonomy'))) {
$description = $temp;
}
} elseif (is_post_type_archive()) {
if ($temp = get_the_archive_description()) {
$description = $temp;
}
}
// else use preview
if (empty($description)) {
$description = str_replace('', "'", $post->get_preview(40, true, false, true));
}
// finally use the blog description
if (empty($description)) {
$description = get_bloginfo('description', 'raw');
}
$description = esc_attr(wp_strip_all_tags($description));
// limit to SEO recommended length
if (strlen($description) > 155) {
$description = substr($description, 0, 155);
$description = \TimberHelper::trim_words($description, str_word_count($description) - 1);
}
return $description;
}