gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:41:48 29 4
gpt4 key购买 nike

我正在编写一些 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

谢谢大家

最佳答案

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

关于php - 在 Wordpress 博客之外查询 Wordpress 数据库以返回最新的博客链接、摘录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14778260/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com