Php Wordpress从另一个db获取最新帖子

Php Wordpress从另一个db获取最新帖子,php,mysql,database,wordpress,content-management-system,Php,Mysql,Database,Wordpress,Content Management System,我试图将另一个Wordpress DB的最新帖子放在我的footer.php文件中,但显然我不理解这个概念。我是WP和“the loop”的新手,因此任何帮助都将不胜感激 <!-- .entry-content --> <?php $originaldb = new wpdb('db_name', 'db_password', 'db_user', 'localhost'); //This has been replaced obviously. $newestPost = $

我试图将另一个Wordpress DB的最新帖子放在我的footer.php文件中,但显然我不理解这个概念。我是WP和“the loop”的新手,因此任何帮助都将不胜感激

<!-- .entry-content -->
<?php
$originaldb = new wpdb('db_name', 'db_password', 'db_user', 'localhost'); //This has been replaced obviously.
$newestPost = $originaldb->query("SELECT * FROM wp_posts WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT 0,1;");
$newestPost = mysql_fetch_array($newestPost);
        if ($newestPost) {
        foreach ($newestPost as $newPost) {
        ?>
            <header class="entry-header">
                <div class="entry-meta">
                    <?php echo '<span class="entry-day">'.get_the_date('j').'</span><br><span class="entry-month">'.get_the_date('M').'</span>'; ?>
                </div>
                <div class="title-box">
                    <h2 class="blog-title"><a href="<?php //the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <?php echo '<a href="'.get_author_posts_url( get_the_author_meta( 'ID' ) ).'">'.get_the_author().'</a>'; ?>
                </div>
                <div class="clear"></div>
            </header>
            <div class="entry-content">
                <?php the_excerpt(); ?>
            </div>
        <?php } //end foreach ?>
    <?php } //endif ?>


如果我明白你想做什么。。。首先,您不应该需要foreach循环,因为您只计划使用一个结果行。其次,为什么不直接访问$newestPost的值作为关联数组?看看我在哪里添加了“$newestPost['theTitle']”来替换“theu title()”,我在作者和内容方面也做了类似的操作

if ($newestPost)  {
       ?>
        <header class="entry-header">
            <div class="entry-meta">
                <?php echo '<span class="entry-day">'.get_the_date('j').'</span><br><span class="entry-month">'.get_the_date('M').'</span>'; ?>
            </div>
            <div class="title-box">
                <h2 class="blog-title"><a href="<?php //the_permalink(); ?>"><?php $newestPost['theTitle']; ?></a></h2>
                <?php echo '<a href="'.get_author_posts_url( get_the_author_meta( 'ID' ) ).'">'.$newestPost['theAuthor']).'</a>'; ?>
            </div>
            <div class="clear"></div>
        </header>
        <div class="entry-content">
            <?php echo $newestPost['theContent']; ?>
        </div>

    <?php
    } //endif ?>
if($newestPost){
?>
你需要用数据库模式中的任何内容替换“theTitle”等。希望这能有所帮助,我对WP不太感兴趣,所以我可能遗漏了一些重要的东西,但这似乎是普遍适用的


编辑:看起来使用mysql\u fetch\u数组是。

下面的代码将给出最后一条post记录

<?php
$mydb = new wpdb('root','pass','dbname','localhost');
$lastpost = $mydb->get_results("SELECT wp_posts.* FROM wp_posts 
WHERE 1=1 
AND wp_posts.post_type = 'post' 
AND (wp_posts.post_status = 'publish')
ORDER BY wp_posts.post_date DESC LIMIT 0,1");

echo $lastpost[0]->post_title;
?>