好的,现在我正在编辑旧问题,因为它已经完成了,但现在我有了新问题,我了解mysql查询,但不了解wordpress查询,我正在使用mysql查询,它工作得很好,但服务器速度非常慢,我知道我每页面加载大约运行30个查询,所以有没有办法在wordpress查询或其他帮助下轻松完成???
<?php $rss = fetch_feed(\'http://example.com/feed\');
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(10);
$rss_items = $rss->get_items(0, $maxitems);
endif;
if ($maxitems == 0) ;
else
foreach ( $rss_items as $item ) :
$posttitle=$item->get_title();
$postcontent=$item->get_description();
$pt=mysql_real_escape_string($posttitle);
$pc=mysql_real_escape_string($postcontent);
$result=mysql_query("SELECT post_title FROM wp_posts WHERE post_title=\'".$pt."\'");
if(mysql_num_rows>0)
{
echo "hello";
mysql_query("UPDATE wp_posts SET post_title=\'".$pt."\',post_content=\'".$pc."\' WHERE post_title=\'".$pt."\'");
}
else
{
echo "hi";
mysql_query("INSERT INTO wp_posts (post_title,post_content) VALUES (\'".$pt."\',\'".$pc."\')");
}
endforeach; ?>
最合适的回答,由SO网友:Alex Sancho 整理而成
您可以在后台使用一些ajax进行页面加载,下面是一个未经测试的示例来说明这一想法。
jQuery(function() {
jQuery.get(\'http://domain.tld/remote\');
});
<?php
function is_ajax()
{
return (isset($_SERVER[\'HTTP_X_REQUESTED_WITH\']) and strtolower($_SERVER[\'HTTP_X_REQUESTED_WITH\']) == \'xmlhttprequest\');
}
function add_rewrite_rules($wp_rewrite)
{
$new_rules = array
(
\'remote\' => \'index.php?remote=true\'
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function add_query_vars($vars)
{
array_push($vars, \'remote\');
return $vars;
}
function remote_feed()
{
if (($var = get_query_var(\'remote\')) and is_ajax())
{
// get remote feed and insert/update posts
}
}
add_action(\'generate_rewrite_rules\', \'add_rewrite_rules\');
add_filter(\'query_vars\',\'add_query_vars\');
add_action(\'template_redirect\', \'remote_feed\');