我目前正在整理这篇文章的节选,但开头有两个词,我不想放在这个地方,因为已经有了标题。
我以前使用过wp\\U trim,但这只会去掉结尾的单词,有没有办法对前两个单词做到这一点。如果有帮助的话,这些词总是一样的?我不确定是否已将摘录获取为字符串,然后将其替换为无内容,或者wp\\U trim是否可以做到这一点。
<?php $tagname = get_the_title (); ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array(\'posts_per_page\'=>1,
\'orderby\' => \'rand\',
\'tag\' => sluggify( $tagname));
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
echo \'<h2 class="entry-title">\';
echo \'CASE STUDY\';
echo \'</h2>\';
echo \'<span>\';
the_post_thumbnail();
echo \'</span>\';
echo \'<strong>\';
the_title();
echo \'</strong>\';
echo \'<p>\';
the_excerpt();
echo \'</p>\';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();?>
来自@RRikesh建议答案的修订代码:
<?php $tagname = get_the_title (); ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args=array(\'posts_per_page\'=>1,
\'orderby\' => \'rand\',
\'tag\' => sluggify( $tagname));
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
while (have_posts()) : the_post();
$str = get_the_excerpt();
echo \'<h2 class="entry-title">\';
echo \'CASE STUDY\';
echo \'</h2>\';
echo \'<span>\';
the_post_thumbnail();
echo \'</span>\';
echo \'<strong>\';
the_title();
echo \'</strong>\';
echo \'<p>\';
echo ltrim($str, "INSTRUCTION SYNOPSIS"); // Output: This is another Hello World.
echo \'</p>\';
endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();?>
SO网友:Pieter Goosen
更可靠的方法是过滤摘录并将字符串分解为一个数组,从数组中删除前两个键/值对,然后返回字符串
add_filter( \'wp_trim_excerpt\', function ( $text )
{
// Make sure we have a text
if ( !$text )
return $text;
$text = ltrim( $text );
$text_as_array = explode( \' \', $text );
// Make sure we have at least X amount of words as an array
if ( 10 > count( $text_as_array ) )
return $text;
$text_array_to_keep = array_slice( $text_as_array, 2 );
$text_as_string = implode( \' \', $text_array_to_keep );
$text = $text_as_string;
return $text;
}):
SO网友:jgraup
preg_replace 一次呼叫救援。/\\w+/
将匹配单词,而preg_replace()
将指定匹配数。既然您想删除它们,那么我们只需传递一个空字符串作为替换。
$str = \'These are some words. But the first two will not remain.\';
// pattern, replacement, string, limit
echo preg_replace( \'/\\w+/\', \'\', $str, 2 );
// output: some words. But the first 2 will not remain.
另一种选择是使用
substr 具有
strpos.
// reduce the extra whitespace
$str = trim( " This is some text and stuff. " );
// find the second space and pull everything after
echo trim( substr( $str, strpos( $str, \' \', strpos( $str, \' \' ) + 1 ) ) );
// output: some text and stuff.