如果您可以在PHP端处理这个问题,那么您可能可以h2
使用内容中的标记regular expression.
PHP VERSION
测试内容
$content = "<h2>This is H2 Content</h2><p>This is p content</p>Random Content<h2>This is another H2 section</h2><b><h2>This the third H2 section</h2></b>";
抓取H2s中的任何文本
preg_match_all(\'#<h2>(.*?)</h2>#\', $content, $matches);
// Merge the first 2 matches
$potential_title = implode ( \' - \', array_slice($matches[0], 0, 2));
如果我们有什么。。。
if( ! empty ( $potential_title ) ) {
从剩余内容中删除任何HTML
$cleaner_title = strip_tags( $potential_title );
准备帖子标题更新
$my_post_updates = array(
\'ID\' => $post_id,
\'post_title\' => $cleaner_title,
);
// Update the post into the database
wp_update_post( $my_post_updates );
}
JS VERSION
Javascript版本与此类似,您只需截取表单提交,获取内容,解析结果,然后传递数据。
这只是提醒解析测试内容的结果
// test content
var content = "<h2>This is H2 Content</h2><p>This is PPP content</p>Random Content<h2>This is another H2 section</h2><b><h2>This the third H2 section</h2></b>";
// variables to store regEx results.
var match, regex = /<h2>(.*?)<\\/h2>/ig, matches = [];
// get all the matches and put in array
while (match = regex.exec(content)) { matches.push( match[1] ); }
// if we have any...
if( matches.length ) {
// pull the first 2 and join them together
var title = matches.slice ( 0, 2 ).join ( \' - \' );
// send this along with your form submission
alert(title);
}