skip to Main Content

I have a static website in html and php. In the footer I have a segment that retrieves the 4 most recent blog posts from a wordpress blog that I host in connection to the static site.

The code I use to get the most recent blog posts is

<?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; ?>

What I am trying to do on the same page is get a list of a few of my users that are stored in a separate mysql database.

I have tried this

<?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();
?>

The code to get the users runs before the footer code for the blog post.

But when I do this my page foot does not load and I get an error

Establishing a database connection to the wordpress site.

I have tried looking up a way around this but cannot seem to get it to work.

Am I missing something or is my syntax wrong or can this not be done?

Thanks for any advice.

2

Answers


  1. Chosen as BEST ANSWER

    I figured it out. @Kinglish - you inspired my thinking in your comment that WP is maintaining a persistent connection to the blog. Since this is not an active blog page, it is not critical that it updates dynamically.

    hence I just did a non wp code based lookup

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

  2. It seems like wordpress holds a persistent connection open to the DB. Trying to establish a new connection while that’s the case is problematic. There are probably a few solutions availble, but I would recommend doing your database queries first, storing the data and then echoing it back where you want it.

    At the top:

    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();
    

    Then down where you need it:

    echo implode(",", $staff);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search