2

wordpress ブログ データベースにクエリを実行し、wordpress 環境外のホームページに最新の投稿を表示するための php を作成中です。

私は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();
}


?>

参照する Web サイトはhttp://www.uniconutrition.comです。

みんなありがとう

4

2 に答える 2

6

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

または、WordPress から 1 つの項目、タイトル、および抜粋を取得するためのRSS が常に存在します。

開発者ガイド - Google AJAX フィード 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>
于 2013-02-08T19:07:59.673 に答える
0

抜粋に関する限り、SELECTクエリでそれを使用していません。クエリの先頭を次のように変更します。

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

投稿へのリンクについては、WordPress の仕組みを経由せずに「きれいに印刷できる」リンクを取得できるとは 100% 確信が持てません。短縮リンク スタイルの URL は表のguid列の下にありますwp_postsが、WP のドキュメントによると、プリティ パーマリンクが無効になっていると表示されない可能性があります。投稿の ID はわかっているので (これはクエリの一部です)、WordPress 関数を使用してリンクを取得できます。詳細については、get_permalinkのドキュメント ページを参照してください。

余談ですが、関数の代わりとしてPDOまたはMysqlimysql_*を見てください。後者は使用しないでください。

于 2013-02-08T18:14:34.767 に答える