在我的搜索中得到了这个答案;“重写”;这反过来又让我找到了一个使用php domdocument函数的不同解决方案,我将在下面发布它可能提供的任何帮助。
/**
* Add tweet link to blockquotes
*/
function my_blockquote_tweets( $the_content ) {
global $wp;
// Parse content
$document = new DOMDocument();
$document->loadHTML( $the_content );
$blockquotes = $document->getElementsByTagName(\'blockquote\');
$svg = file_get_contents( get_stylesheet_directory() . \'/resources/images/svg/social-logos/twitter.svg\' );
foreach ($blockquotes as $blockquote) {
// Twitter Icon
$twitter_svg = $document->createDocumentFragment();
$twitter_svg->appendXML( $svg );
// Icon wrapper
$twitter_logo_wrapper = $document->createElement(\'span\');
$twitter_logo_wrapper->setAttribute(\'class\', \'tweet-logo\');
$twitter_logo_wrapper->appendChild( $twitter_svg );
$tweet_text = substr($blockquote->nodeValue, 0, 250);
if( strlen( $tweet_text ) > 250 ) {
$tweet_text .= \' …\';
}
$tweet = $document->createElement(\'a\');
$tweet->setAttribute( \'class\', \'tweet-this group\' );
$tweet->setAttribute( \'title\', __(\'Tweet this quote\', \'pwa\') );
$tweet->setAttribute( \'href\', \'https://twitter.com/intent/tweet?text=\' . urlencode( $tweet_text ) . \'&url=%0A\' . urlencode(home_url( $wp->request )));
$tweet->setAttribute( \'target\', \'_blank\' );
$tweet->appendChild( $twitter_logo_wrapper );
$tweet_text = $document->createTextNode( __( \'Tweet this\' , \'pwa\' ) );
$tweet->appendChild( $tweet_text );
$blockquote->appendChild( $tweet );
}
$the_content = $document->saveHtml();
return $the_content;
}
add_filter(\'the_content\', \'my_blockquote_tweets\', 10, 1);
希望它能帮助别人。