【问题标题】:Wordpress get most recent post from another dbWordpress 从另一个数据库获取最新帖子
【发布时间】:2012-09-18 11:33:53
【问题描述】:

我正在尝试将另一个 Wordpress 数据库中的最新帖子放在我的 footer.php 文件中,但显然我不理解这个概念。我是 WP 和“循环”的新手,所以任何帮助都将不胜感激!

<!-- .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 ?>

【问题讨论】:

    标签: php mysql database wordpress content-management-system


    【解决方案1】:

    下面的代码会给出最后的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】:

      如果我了解您要做什么...首先,您不需要 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 ?>
      

      您需要将“theTitle”等替换为您的数据库架构中的任何内容。希望有所帮助,我对 WP 并不感兴趣,所以我可能会遗漏一些重要的东西,但这似乎普遍适用。

      编辑:看起来使用 mysql_fetch_array 是no longer recommended

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2018-08-08
        • 1970-01-01
        • 1970-01-01
        • 2013-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多