首页 文章

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

提问于
浏览
2

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

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

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

多谢你们

2 回答

  • 0

    它可以比使用 WordPress database layer 尝试的更容易 . 见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 "
    "; ?> <?php the_title(); ?> <?php the_excerpt(); ?> <?php endforeach; ?>

    或者, there is always RSS 从WordPress中获取一个项目, Headers 和摘录 .

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

    就摘录而言,您不是在查询中使用它 . 将查询的开头更改为:

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

    至于帖子的链接,我不是100%确定你可以在不通过WordPress机器的情况下获得"pretty-printable"链接 . wp_posts 表中的 guid 列下提供了一个短链接样式的URL,但WP文档声称如果禁用了非常永久链接,它可能不会显示 . 由于你知道帖子的ID(它是你查询的一部分),你可以使用WordPress函数来获取链接 . 有关更多信息,请参见get_permalink文档页面 .

    另外,请查看PDOMysqli作为 mysql_* 函数的替代 . 你不应该再使用后者了 .

相关问题