Wordpress获得了另一个db的最新文章

时间:2022-02-06 13:38:17

I am trying to put the most recent post from another Wordpress DB in my footer.php file, but obviously am not understanding the concept. I'm new to WP and "the loop", so any help would be appreciated!

我想把另一个Wordpress DB的最新文章放到我的页脚上。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 ?>

2 个解决方案

#1


2  

The following code will gives the last post record.

下面的代码将给出最后一个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;
?>

#2


1  

If I understand what you're trying to do ... first, you shouldn't need a foreach loop since you're only planning to use one result row. Second, why not directly access the values of $newestPost as an associative array? See where I've added "$newestPost['theTitle']" to replace "the_title()", and I've done similarly for the the author and content as well.

如果我明白你想做什么……首先,您不需要foreach循环,因为您只计划使用一个结果行。其次,为什么不直接访问$newestPost作为关联数组的值呢?看看我在哪里添加了$newestPost['theTitle']来替换"the_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 ?>

You would need to replace 'theTitle' etc with whatever is in your db schema. Hope that helps, I'm not big into WP so I might be missing something important but this seems universally applicable.

您需要将'theTitle'等替换为db模式中的任何内容。希望这能帮助我,我不太喜欢WP,所以我可能会错过一些重要的东西,但这似乎是普遍适用的。

EDIT: It looks like using mysql_fetch_array is no longer recommended.

编辑:似乎不再推荐使用mysql_fetch_array。

#1


2  

The following code will gives the last post record.

下面的代码将给出最后一个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;
?>

#2


1  

If I understand what you're trying to do ... first, you shouldn't need a foreach loop since you're only planning to use one result row. Second, why not directly access the values of $newestPost as an associative array? See where I've added "$newestPost['theTitle']" to replace "the_title()", and I've done similarly for the the author and content as well.

如果我明白你想做什么……首先,您不需要foreach循环,因为您只计划使用一个结果行。其次,为什么不直接访问$newestPost作为关联数组的值呢?看看我在哪里添加了$newestPost['theTitle']来替换"the_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 ?>

You would need to replace 'theTitle' etc with whatever is in your db schema. Hope that helps, I'm not big into WP so I might be missing something important but this seems universally applicable.

您需要将'theTitle'等替换为db模式中的任何内容。希望这能帮助我,我不太喜欢WP,所以我可能会错过一些重要的东西,但这似乎是普遍适用的。

EDIT: It looks like using mysql_fetch_array is no longer recommended.

编辑:似乎不再推荐使用mysql_fetch_array。