首页 > 解决方案 > 如何在也有 wordpress 博客文章摘要的静态站点上查询 mysql 用户数据库

问题描述

我有一个 html 和 php 的静态网站。在页脚中,我有一个段,它从我托管的与静态站点相关的 wordpress 博客中检索 4 篇最新的博客文章。

我用来获取最新博客文章的代码是

<?php
// Include WordPress
require_once($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');query_posts('showposts=4');?>
      <!-- Footer Content -->
      <div class="col-lg-3 col-md-6 g-mb-40 g-mb-0--lg">
        <div class="u-heading-v2-3--bottom g-brd-white-opacity-0_8 g-mb-20">
          <h2 class="u-heading-v2__title h6 text-uppercase mb-0">From the Wisdom Tooth</h2>
        </div>
    <?php while (have_posts()): the_post(); ?>
        <article>
          <h3 class="h6 g-mb-2">
        <a class="g-color-white-opacity-0_8 g-color-white--hover" href="<?php the_permalink() ?>"><?php $thetitle = $post->post_title; /* or you can use get_the_title() */$getlength = strlen($thetitle);$thelength = 25;echo substr($thetitle, 0, $thelength);if ($getlength > $thelength) echo "...";?></a>
      </h3>
          <div class="small g-color-white-opacity-0_6"><?php echo get_the_date( 'd M Y' ); ?></div>
        </article>

        <hr class="g-brd-white-opacity-0_1 g-my-10">
    <?php endwhile; ?>

我试图在同一页面上做的是获取我的一些用户的列表,这些用户存储在单独的 mysql 数据库中。

我试过这个

<?php 
    require_once($_SERVER['DOCUMENT_ROOT'].'/staff/config.php');
    $sql = "SELECT `stafffname` FROM `accounts` WHERE active = 1 AND id != (SELECT MIN(id) FROM `accounts`) ORDER BY stafffname ASC";
    $result = $mysqli->query($sql);
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "" .$row["stafffname"]. ", " ;
        }
    } else {
        echo "";
    }   
    $mysqli->close();
    flush();
    ob_flush();
?>

获取用户的代码在博文的页脚代码之前运行。

但是当我这样做时,我的页脚没有加载并且出现错误

建立与 wordpress 站点的数据库连接。

我已经尝试过寻找解决方法,但似乎无法让它发挥作用。

我是否遗漏了什么或者我的语法错误或者不能这样做?

感谢您的任何建议。

标签: phpmysqlwordpress

解决方案


我想到了。@Kinglish - 您在评论中启发了我的想法,即 WP 正在与博客保持持久连接。由于这不是一个活动的博客页面,因此动态更新并不重要。

因此我只是做了一个基于非 wp 代码的查找

<?php
   $mysqli = new mysqli(localhost, username, password, tablename);
   $sql2 = "SELECT post_title, post_date, guid FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date DESC LIMIT 4";
   $result2 = $mysqli->query($sql2);
      if($result2->num_rows > 0) 
          {
          while($row2 = $result2->fetch_assoc()) 
            {
            echo '
            <article>
              <h3 class="h6 g-mb-2">
            <a class="g-color-white-opacity-0_8 g-color-white--hover" href="'.$row2['guid'].'">'.$row2['post_title'].'</a>
          </h3>
              <div class="small g-color-white-opacity-0_6">'.date('d M Y', strtotime($row2['post_date'])).'</div>
            </article>

            <hr class="g-brd-white-opacity-0_1 g-my-10">
            
            ';
                    }
                } else 
                {
                echo "No Post Avaliable";
                }
            ?>  

          </div>
          <!-- End Footer Content -->

推荐阅读