Php 如何在一个静态站点上查询mysql用户数据库,该站点上也有wordpress博客文章摘要

Php 如何在一个静态站点上查询mysql用户数据库,该站点上也有wordpress博客文章摘要,php,mysql,wordpress,Php,Mysql,Wordpress,我有一个html和php的静态网站。在页脚中,我有一个片段,从我主持的与静态站点连接的wordpress博客中检索最近的4篇博客文章 我用来获取最新博客帖子的代码是 <?php // Include WordPress require_once($_SERVER['DOCUMENT_ROOT'] . '/blog/wp-load.php');query_posts('showposts=4');?> <!-- Footer Content -->

我有一个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站点的数据库连接

我试着寻找一种方法来解决这个问题,但似乎无法让它发挥作用

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


谢谢你的建议。

wordpress似乎保持着与数据库的持久连接。在这种情况下尝试建立新的连接是有问题的。可能有一些可用的解决方案,但我建议您首先进行数据库查询,存储数据,然后将其回显到需要的位置

在顶部:

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);
    $staff = array();
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $staff[] = $row["stafffname"];
        }
    }   
    $result->free();
    $mysqli->close();
然后在你需要的地方:

echo implode(",", $staff);

我想出来了@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 -->


您的
'/staff/config.php'
文件起作用吗?这与WP有接口吗?或者它只是一个简单的值数组?staff/config.php是包含数据库连接数据(dbname、username、pw、localhost)的文件。它不应该与WP接口,但我觉得这种连接会产生WP问题,好像我删除了该行WP部分工作正常,但我没有获得我的员工数据。嗨@Kinglish,我尝试了你的建议,但它仍然没有加载WP部分。员工数据加载正确。总是有Ajax…在这种情况下,你会如何使用Ajax?哦,好的,我明白了…我现在正在检查。如果你需要Ajax方面的帮助,请告诉我