这是我用来玩和操纵搜索的一个函数。也许能帮上忙。
/*** Suggested Keywords for search ***/
function get_suggested_keywords(){
/* Suggested keywords + tags */
if(isset($_GET[\'s\']) && !empty($_GET[\'s\']))
{
$search=preg_replace("[^a-zA-Z0-9\\ ]","",urldecode($_GET[\'s\']));
if(!empty($search))
{
$words=array_map("trim",explode(" ",$search));
if(isset($words[1]) && !empty($words[1]))
{
$tags=array();
foreach($words as $tag)
{
$tags[]= \'<a href="\' . get_site_url() . \'/?s=\' . $tag . \'" title="\' . $tag . \'" rel="search">\' .$tag. \'</a>\';
}
echo \'<h3 class="general-title">
<span>\'.__(\'Suggested keywords\', \'creativform\').\'</span>
</h3>\'."\\r\\n";
echo \'<div class="tagcloud">\'.join($tags, " ").\'</div>\';
}
}
}
}
我对此有很多变体,但您可以使用这些关键字调用查询并处理这些数据。
UPDATE:
若您需要重新组织内容并向帖子中添加额外内容,首先需要循环数据库中的所有帖子,保存在数组中,在数组中添加额外内容,并在新循环中以HTML列出所有数据。以下是示例:
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) $random[$key] = $list[$key];
return $random;
}
$query=array();
if(have_posts()){
#### Get posts and save for manipulation ####
while (have_posts()){
the_post();
// get the ID
$ID = get_the_ID();
// get thumbnail
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), \'large\' );
if(isset($thumb[0]) && !empty($thumb[0]))
$thumbnail=$thumb[0];
else
$thumbnail=false;
// save in object array
$query[]=(object)array(
\'thumbnail\' => $thumbnail,
\'post_permalink\' => post_permalink($ID),
\'the_title_attribute\' => the_title_attribute(array(\'echo\' => 0)),
\'the_title\' => get_the_title($ID),
\'the_content\' => get_the_content($ID),
\'the_date\' => get_the_date($ID),
);
}
#### Add new content inside posts ####
$query[]=(object)array(
\'thumbnail\' => "SOMETHING 1",
\'post_permalink\' => "SOMETHING 1",
\'the_title_attribute\' => "SOMETHING 1",
\'the_title\' => "SOMETHING 1",
\'the_content\' => "SOMETHING 1",
\'the_date\' => "SOMETHING 1",
);
$query[]=(object)array(
\'thumbnail\' => "SOMETHING 2",
\'post_permalink\' => "SOMETHING 2",
\'the_title_attribute\' => "SOMETHING 2",
\'the_title\' => "SOMETHING 2",
\'the_content\' => "SOMETHING 2",
\'the_date\' => "SOMETHING 2",
);
#### Randomize content (OPTIONAL - delete if you don\'t need or just comment) ####
$query=shuffle_assoc($query);
#### Loop and display in your HTML ####
foreach($query as $fetch){
echo $fetch->thumbnail.\'<br>\';
echo $fetch->post_permalink.\'<br>\';
echo $fetch->the_title_attribute.\'<br>\';
echo $fetch->the_title.\'<br>\';
echo $fetch->the_content.\'<br>\';
echo $fetch->the_date.\'<br>\';
echo \'<hr>\';
}
}
在您添加新内容的区域中,您可以手动添加或使用一些关键字检查循环,以类似的方式获取其他内容并存储在
$query
. 您可以在帖子列表、页面列表、搜索等中使用此代码,只需在WP\\u query()中稍加修改即可;