我有700篇帖子,大部分都超过1000字。我正在将此设置为会员网站。我正在使用wishlist成员,并希望保护前350个单词之后的所有内容。这需要在第350个单词后放置一个短标签,并在文章的最后放置一个短标签。
在末尾添加短代码似乎非常简单。然而,我不知道如何将短代码放到post\\u内容的中间。。
我考虑了几种不同的方法,包括:
a、 尝试在dreamweaver中使用xml平面文件B找到并替换正则表达式。尝试在mysql中使用查询来查询帖子内容字段,计算该字段中的单词数,然后插入shortcode标记therec。运行一些php脚本以获得结果(此处类似,但不完全相同http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-create-a-variable-length-excerpt-in-wordpress-without-a-plugin/ )d、 或者是以上的一些组合,或者是更尖锐更简单的答案
由于我希望对数据进行永久性更改(而不是每次加载一篇文章时都对主题进行动态更改),mysql选项似乎是最合适的方法。
在查找特定字符串并替换它的简单上下文中,这不是一个查找和替换,而是计算到post\\u内容中的某个位置,然后
添加一个短码[示例\\u短码]或将post\\u内容的第一部分复制到并包括第350个单词,并将其与[示例\\u短码]连接起来,再加上第350个单词后的所有剩余文本,再加上结束符Some of my research leads so far:因此,比example of search and replace query
我怀疑它可能需要substring函数more on substrings in mysql 在本例中,子字符串用于分页pagination link
SELECT SUBSTRING(post_content,1000,1000) FROM posts WHERE id=x
Visual example of my goal
如何在说出帖子的前350个单词后插入第一个标签,在帖子末尾插入最后一个标签,并在所有700多篇帖子中重复?因此Lorem ipsum dolor sit amet,concetetur adipiscing elit。整数nec odio。自由女神。直径。塞德·尼西。nibh elementum imperdiet的Nulla quis sem。同侧矢状肌。Praesent mauris。Fusce nec tellus sed augue semper porta。毛里斯·马萨。前庭短弧。等级aptent taciti sociosqu ad litora torquent per
跳到->**word 350->前庭[示例\\u快捷码]**faucibus orci luctus et ultrices posuere cubilia Curae中的前同侧基本信息;Morbi lacinia molestie dui。普拉森特·布兰迪特
跳到Morbi lacinia molestie dui后的结尾。普雷森特·布兰迪特·多洛(Praesent blandit dolor)。Sed非quam。在我的内心深处,有一种元素。伊普苏姆的莫比。[/example\\u短代码]
由于各种原因,简单地编写一个函数来动态添加标记,而不是更新数据库的路径proven unworkable. 主要原因是由于愿望列表成员api中存在缺陷(缺少选项)。
bottom line 它不支持使用php版本的短标记。(请注意,如果我再做一次,我可能会使用不同的成员插件解决方案,而不是像wishlist那样关闭)。
这就是说,目前唯一可行的选择是简单地通过将短标签插入到具有适当类别的帖子中来更新db。(这就是我在原始问题中提出的问题,迄今为止还没有提出任何解决方案,尽管备选方案似乎可行,但在这种情况下它们不可行)。
现在,我已经回到了第一步的一半。如何对几乎所有wordpress帖子进行大规模更新。特别是不属于两个类别的所有员额。
我发现有3个相关的建议可以在wordpress中进行大规模更新,尽管它们主要在wp\\U帖子中的字段上执行更新,而不是在post\\U内容字符串中执行更新。
参见https://stackoverflow.com/questions/7917725/update-all-wordpress-posts
/*插件名称:示例描述:这不仅仅是一个插件,它是代码。。作者:*/add\\u action(\'init\',\'example\\u hide\');
函数示例\\u hide(){
$my\\u posts=get\\u posts(数组(\'post\\u type\'=>\'post\',\'numberposts\'=>10));
foreach($my\\u posts as$my\\u post):
$my\\u post[\'post\\u content\']=“这是更新的内容。”;
wp\\u update\\u post($my\\u post);
endforeach;
}然后看看这个只更新slug的示例(顺便说一句,在mysql中做这件事要容易得多https://stackoverflow.com/questions/12707469/update-all-the-posts-at-once-nothing-want-to-add-or-delete-only-update-so-that-p
//get all posts$posts=get\\u posts(数组(\'numberposts\'=>-1));
foreach($posts as$post){//检查slug并在必要时运行更新$new\\u slug=sanitize\\u title($post->post\\u title);wp\\u update\\u post(数组(\'ID=>$post->ID,\'post\\u name=>$new\\u slug));
}
然后是3,我认为这是最有希望的http://www.michaelbrentecklund.com/wordpress-mass-update-posts-mass-update-pages-custom-post-types/07-18-2012/
/**
* Mass Update Posts
*
* Mass update portions of posts, pages or custom post types.
* Get all posts from a specified post type, determine the
* status of the post, choose a portion of the post structure,
* search for a string value associated with the specified key,
* specify a replacement string, and choose whether you want to
* search for a partial or exact match.
*
* @param string $post_type Post type to fetch posts from.
* @param string $post_status Status of the post (eg. publish, private).
* @param string $key Portion of the post (eg. post_status, post_type).
* @param string $find Value of the portion of post (eg. publish, page).
* @param string $replace String to replace value with. (eg. private, post).
* @param string $match_type Accepts \'partial\' or \'exact\' match.
**/
function mass_update_posts($post_type, $post_status, $key, $find, $replace, $match_type){
$args = array(
\'numberposts\' => -1,
\'post_type\' => $post_type,
\'post_status\' => $post_status
);
$posts = get_posts($args);
$return = \'\';
for($i = 0; $i < count($posts); $i++){
// Check if the portion of the post exists.
if(array_key_exists($key, $posts[$i])){
if($match_type == \'exact\'){
// Check if the portion of the post matches exact search.
if($posts[$i]->$key == $find){
$action = \'update\';
} else{
$action = \'return\';
}
} elseif($match_type == \'partial\'){
// Check if the portion of the post matches partial search.
$search = strpos($posts[$i]->$key, $find);
if($search !== false){
$action = \'update\';
} else{
$action = \'return\';
}
} else{
$return .= \'<p><strong>Error!</strong> Specify either "partial" or "exact" match.</p>\';
}
// Found the search query. Carry on.
if($action == \'update\'){
// Set the portion of the post to be updated.
$update[\'ID\'] = $posts[$i]->ID;
$update[$key] = $replace;
// Update the portion of the post.
$updated = wp_update_post($update);
// Check if update passes/fails.
if($updated){
//array_push($replaced, array($posts[$i]->$key => str_replace($find, $replace, $posts[$i]->$key)));
$replaced[][$posts[$i]->$key] = str_replace($find, $replace, $posts[$i]->$key);
} else{
$return .= \'<p><strong>Error!</strong> Key: \'.$key.\' was found, however, failed to update the post: <strong>#\'.$posts[$i]->ID.\'</strong> <em>\'.$posts[$i]->post_title.\'</em>.</p>\';
}
} elseif($action == \'return\'){
$return .= \'<p><strong>Error!</strong> Could not find "\'.$find.\'" in key: <strong>\'.$key.\'</strong>.</p>\';
}
} else{
$return .= \'<p><strong>Error!</strong> Invalid key specified.</p>\';
}
}
// Posts were updated. Output the result.
if(!empty($replaced)){
$return .= \'<p><strong>Success!</strong> Updated \'.count($replaced).\' posts.</p>\';
foreach($replaced as $key => $value){
foreach($value as $old => $new){
$return .= \'<p><strong>Old:</strong> <em>\'.$old.\'</em> <span style="padding: 0px 24px;">=></span><br /> <strong>New:</strong> \'.$new.\'</p>\';
}
}
}
echo $return;
}
我认为,最后一个函数与不断发展的内容限制函数的组合,最初设想如下@userabuser 并由我自己适度修改,可能最终证明是解决方案。。。//* Limited Content
add_filter(\'the_content\', \'limited_content\', 11);
function post_is_in_descendant_category( $cats, $_post = null ) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, \'category\' );
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
add_filter(\'the_content\', \'do_shortcode\', 11); // From shortcodes.php
function limited_content($content) {
$limit = 350; //word limit for non-member/non-subscriber
$words = str_word_count($content);
$user_id = get_current_user_id();
//if it is not in either category we have to test membership
if ( in_category( array(\'2497\',\'2811\')) /*|| post_is_in_descendant_category(array(\'2497\',\'2811\')) */ ) {
return $content;
//check if the user is part of the "member" role if so, return the full content
} elseif ( user_can($user_id, \'member\') ) {
return $content;
//otherwise for non members return 350 of the content
} elseif ( $words >= $limit) {
$contentteaser = explode(\' \', $content, $limit);
array_pop($contentteaser);
$contentremainder = explode(\' \', $content);
$contentremainder = array_slice($contentremainder, 349);
$contentremainder = implode(" ", $contentremainder);
$contentteaser = implode(" ", $contentteaser);
echo do_shortcode($contentteaser.\' \'.\'[private_Silver level]\'.$contentremainder.\'[/private_Silver level]\'. \'... <a href="#">sign up to read more</a>\');
//if there\'s less than 350 words, just return whatever exists
} else {
return $content;
}
} // end function Limited Content