【问题标题】:Query Wordpress Database outside of Wordpress blog to return latest blog link, excerpt在 Wordpress 博客之外查询 Wordpress 数据库以返回最新的博客链接,摘录
【发布时间】:2013-01-24 13:18:54
【问题描述】:

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

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

// ...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();
}


?>

要参考的网站是http://www.uniconutrition.com

谢谢大家

【问题讨论】:

    标签: php html mysql sql wordpress


    【解决方案1】:

    使用 WordPress 数据库层,这比您尝试的要容易得多。见http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    基本上:

    <?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 中获取一个项目、标题和摘录。

    Developer's Guide - Google AJAX Feed API - Google Code

    <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>
    

    【讨论】:

    • 这让我的网站速度太慢了 :( 这就是我自己查询数据库的原因
    • 哇,如果这会减慢您的网站速度,那么您的主机还有其他更严重的问题。 RSS 呢?
    • 至少可以说我不熟悉 RSS - 在上面的代码中,我是否只需输入我的博客网址,其中显示“..fastpshb ...”?是的,它确实减慢了我的页面速度——其他人说这是有道理的,因为 php 加载了整个 wordpress 环境..
    • 阅读有关如何使用 Google RSS API 的文档。至于“其他人说这是有道理的,因为 php 加载了整个 wordpress 环境”,这根本没有意义。你的主机——eigbox.net、pow web,不管它是谁——都是众所周知的糟糕主机,这就是为什么像 WP 这样简单的东西会加载你的帐户。
    • 我使用 ipage- 你能推荐一个更好的主机吗?谢谢!
    【解决方案2】:

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

    SELECT post_title, post_content, post_excerpt,
           UNIX_TIMESTAMP(post_date) AS post_date_unix, ID
    

    至于帖子的链接,我不能 100% 确定您可以在不通过 WordPress 机器的情况下获得“漂亮的可打印”链接。在wp_posts 表中的guid 列下有一个短链接样式的 URL,但 WP 文档声称如果禁用漂亮的永久链接,它可能不会显示。由于您知道帖子的 ID(它是您查询的一部分),您可以使用 WordPress 函数来获取链接。有关更多信息,请参阅 get_permalink 文档页面。

    顺便说一句,看看PDOMysqli 作为mysql_* 函数的替代品。你不应该再使用后者。

    【讨论】:

    • 非常感谢 jonah 的反馈——至于我刚刚在查询中抛出的 ID,我还没有真正将它取出来......我是否像其他人一样声明一个变量?即 $post_id = $row['post_id'];
    • 是的,只需创建另一个变量(如果您愿意,也可以通过$row['ID'] 简单地访问它)。请注意,列名是ID 而不是post_id
    • 好的,我得到它来“回显”id...我如何从 id 中获取永久链接?
    • 我要声明一个永久链接变量吗?比如 $permalink = get_permalink( $id );
    • 应该可以。如有疑问,请尝试一下!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2010-11-15
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多