I'm trying to figure out how to create a common newspaper device called a 'pullquote' in a Wordpress post. (But this isn't strictly a Wordpress question; it's more a generic Regex question.) I have a tag to surround the text in the post. I want to copy the text between the tags (which I know how to do) and the insert it between the 3rd and 4th instances of the p tags in the post.
The function below, finds the text and strips out the tags, but simply prepends the matched text to the beginning. I need help with targeting the 3rd/4th paragraph (as one can do with jQuery nth-child).
function jchwebdev_pullquote( $content ) {
$newcontent = $content;
$replacement = '$1';
$matches = array();
$pattern = "~\[callout\](.*?)\[/callout\]~s";
// strip out 'shortcode'
$newcontent = preg_replace($pattern, $replacement, $content);
if( preg_match($pattern, $content, $matches)) {
// now have formatted pullquote
$pullquote = '<blockquote class="pullquote">' .$matches[1] . '</blockquote>';
// now how do I target and insert $pullquote
// between 3rd and 4th paragraph?
preg_replace($3rd_4th_pattern, $3rd_4th_replacement,
$newcontent);
return $newcontent;
}
return $content;
}
add_filter( 'the_content' , 'jchwebdev_pullquote');
Aucun commentaire:
Enregistrer un commentaire