Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Wordpress博客外部查询Wordpress数据库以返回最新的博客链接,摘录_Php_Html_Mysql_Sql_Wordpress - Fatal编程技术网

Php 在Wordpress博客外部查询Wordpress数据库以返回最新的博客链接,摘录

Php 在Wordpress博客外部查询Wordpress数据库以返回最新的博客链接,摘录,php,html,mysql,sql,wordpress,Php,Html,Mysql,Sql,Wordpress,我正在编写一些php来查询我的wordpress博客数据库,并在wordpress环境之外的我的主页上显示最新的文章 我不是很精通php,但我已经能够显示最新的博客标题,以及文章内容。我想做的是有缩略图是一个点击链接到文章。我如何获得该帖子的链接?我还想只显示摘录,而不是整个文章,但是使用post_摘录的方式与使用post_标题、post_内容的方式相同,似乎不起作用 // ...Connect to WP database $dbc = mysql_connect(XXX,XXX,XXX);

我正在编写一些php来查询我的wordpress博客数据库,并在wordpress环境之外的我的主页上显示最新的文章

我不是很精通php,但我已经能够显示最新的博客标题,以及文章内容。我想做的是有缩略图是一个点击链接到文章。我如何获得该帖子的链接?我还想只显示摘录,而不是整个文章,但是使用post_摘录的方式与使用post_标题、post_内容的方式相同,似乎不起作用

// ...Connect to WP database
$dbc = mysql_connect(XXX,XXX,XXX);
if ( !$dbc ) {
    die( 'Not Connected: ' . mysql_error());
}
// Select the database
$db = mysql_select_db('wrd_2ikhd5ho53');
if (!$db) {
    echo "There is no database: " . $db;
}


  // ...Formulate the query
$query = "
        SELECT post_title,post_content,UNIX_TIMESTAMP(post_date) AS post_date_unix, ID
        FROM `wp_posts`
        WHERE `post_status` = 'publish'
        AND `post_password` = ' '
        AND `post_type` = 'post'
        ORDER BY `wp_posts`.`post_date` DESC
        ";

// ...Perform the query
$result = mysql_query( $query );

// ...Check results of the query and terminate the script if invalid results
if ( !$result ) {
    $message = '<p>Invalid query.</p>' . "\n";
    $message .= '<p>Whole query: ' . $query ."</p> \n";
    die ( $message );
}

// Init a variable for the number of rows of results
$num_rows = mysql_num_rows( $result );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );

// Init var for DATE of the post
$post_date = date( "l, F jS, Y ", $row['post_date_unix'] );  

// Init var for TITLE of the post
$post_title = $row['post_title'];

// Init var for CONTENT of the post
$post_content = $row['post_content'];
$post_excerpt = $row['post_excerpt'];

// Init var for Excerpt of the post

// Print the number of posts
echo "$post_title";
echo "$post_date";



// Free the resources associated with the result set
if ( $result ) {
    mysql_free_result( $result );
    mysql_close();
}


?>
/…连接到WP数据库
$dbc=mysql\u connect(XXX,XXX,XXX);
如果(!$dbc){
死('notconnected:'.mysql_error());
}
//选择数据库
$db=mysql_select_db('wrd_2ikhd5ho53');
如果(!$db){
echo“没有数据库:”.$db;
}
//…制定查询
$query=”
选择post_标题、post_内容、UNIX_时间戳(post_日期)作为post_日期、ID
来自“wp_”的帖子`
其中“发布状态”=“发布”
和'post_password'='
和'post_type`='post'
由'wp_posts'订购。'post_date'DESC
";
//…执行查询
$result=mysql\u query($query);
//…检查查询结果,如果结果无效,则终止脚本
如果(!$result){
$message='无效查询。

'.“\n”; $message.='整个查询:'.$query.“

\n”; 死亡($信息); } //Init一个用于结果行数的变量 $num\u rows=mysql\u num\u rows($result); $row=mysql\u fetch\u数组($result,mysql\u ASSOC); //发布日期的初始变量 $post_date=日期(“l,F jS,Y”,$row['post_date_unix']); //文章标题的Init变量 $post_title=$row['post_title']; //帖子内容的Init变量 $post_content=$row['post_content']; $post_extract=$row['post_extract']; //文章摘录的Init变量 //打印帖子的数量 回显“$post_title”; 回显“$post_date”; //释放与结果集关联的资源 如果($结果){ mysql_free_result($result); mysql_close(); } ?>
参考的网站是


谢谢大家

就摘录而言,您没有在查询中选择它。将查询的开头更改为:

SELECT post_title, post_content, post_excerpt,
       UNIX_TIMESTAMP(post_date) AS post_date_unix, ID
至于这篇文章的链接,我不能100%肯定你不通过WordPress机器就能得到一个“可打印的”链接。在
wp_posts
表中的
guid
列下有一个短链接样式的URL,但是wp文档声称,如果禁用了pretty permalinks,它可能不会显示。因为你知道帖子的ID(这是你查询的一部分),你可以使用WordPress函数来获取链接。有关更多信息,请参阅文档页面


另一方面,请看一看
mysql.*
函数,或将其替换。您不应该再使用后者。

这比使用WordPress数据库层要容易得多。看

基本上:

<?php
require('/the/path/to/your/wp-blog-header.php');
?>

<?php
$posts = get_posts('numberposts=1');
foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>    
<?php the_excerpt(); ?> 
<?php
endforeach;
?>

或者,总是有RSS从WordPress中获取一个项目、标题和摘录

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

    google.load("feeds", "1");

    function initialize() {
      var feed = new google.feeds.Feed("http://fastpshb.appspot.com/feed/1/fastpshb");
      feed.load(function(result) {
        if (!result.error) {
          var container = document.getElementById("feed");
          for (var i = 0; i < result.feed.entries.length; i++) {
            var entry = result.feed.entries[i];
            var div = document.createElement("div");
            div.appendChild(document.createTextNode(entry.title));
            container.appendChild(div);
          }
        }
      });
    }
    google.setOnLoadCallback(initialize);

    </script>
  </head>
  <body>
    <div id="feed"></div>
  </body>
</html>

加载(“提要”,“1”);
函数初始化(){
var feed=新的google.feed.feed(“http://fastpshb.appspot.com/feed/1/fastpshb");
feed.load(函数(结果){
如果(!result.error){
var container=document.getElementById(“提要”);
对于(变量i=0;i
非常感谢jonah的反馈——至于我刚才在查询中抛出的ID,我还没有真正得到它……我是否像其他变量一样声明了一个变量?ie$post_id=$row['post_id'];是的,只需创建另一个变量(或者您也可以通过
$row['ID']
访问它,如果您愿意的话)。请注意,列名是
ID
,而不是
post\u ID
。好的,我将其用于“回显”ID…如何从ID中获取永久链接?我是否声明永久链接变量?比如$permalink=get_permalink($id);应该这样做。如果有疑问,请尝试一下!这会使我的网站速度过慢:(这就是我为什么要自己查询数据库的原因哇,如果这会减慢你的网站速度,那么你与主机之间还有其他更严重的问题。RSS呢?至少可以说,我不熟悉RSS——在上面的代码中,我只需输入我的博客url,上面写着“…fastpshb…”。是的,它真的降低了我的页面速度——其他人都这么说了有意义,因为php加载了整个wordpress环境。阅读有关如何使用Google RSS API的文档。至于“其他人说这是有意义的,因为php加载了整个wordpress环境”,这毫无意义。你的主机——eigbox.net、pow web,不管是谁——众所周知是一个糟糕的主机,这就是为什么像WP这样简单的东西会下载你的帐户。我使用ipage——你能推荐一个更好的主机吗?谢谢!