简体   繁体   English

调用wp-load.php从主站点上的Wordpress Blog加载3个最新帖子,现在无法切换回其他数据库

[英]Calling wp-load.php to load 3 recent posts from Wordpress Blog on Main site, now can't switch back to other database

I have a main site at www.mydomain.com. 我在www.mydomain.com有一个主要站点。 I have a wordpress blog set up at www.mydomain.com/blog. 我在www.mydomain.com/blog上有一个wordpress博客。 I want visitors to my main site to see the 3 recent blog posts. 我希望访问我主站点的访问者可以看到3篇最近的博客文章。 I was able to pull it off easily using the following code: 我可以使用以下代码轻松地将其完成:

<?php    
define('WP_USE_THEMES', false);
require('/home/mydomain/public_html/blog/wp-load.php');
query_posts('showposts=3');
?>

<?php while (have_posts()) : the_post(); ?>
      <div class='category rounded_box'>
        <?php if ( get_post_meta($post->ID, 'image', true) ) { ?>
        <div class="category-thumbnail">
            <a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "image", $single = true); ?>&h=100&w=100&zc=1" width="100" height="100" border="0" /></a>
        </div>
        <?php } ?>
        <div class='category_title'>
            <h2><a href='<?php the_permalink() ?>'><?php the_title(); ?></a></h2>
        </div>
        <div class='duration_home'>
            <?php the_excerpt(); ?>
            <span>Posted on <?php the_time('F jS, Y') ?> - <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></span>
        </div>
      </div>
      <?php endwhile;?>

<?php
wp_reset_query();
?>

Problem now is, anything on the page that requires calling a table for the main site no longer works. 现在的问题是,页面上需要调用主站点的表的所有内容都不再起作用。 I'm getting an error saying it is searching for that table in the Wordpress blogs database. 我收到一条错误消息,说它正在Wordpress博客数据库中搜索该表。

How do I break the connection to the Wordpress database? 如何断开与Wordpress数据库的连接?

The OP wrote in an edit: OP在编辑中写道:

SOLVED! 解决了!

I had to close the mysql connection, and then immediately after connect to my main database table. 我必须关闭mysql连接,然后在连接到主数据库表后立即关闭。 This is what I added to the end of the code: 这是我在代码末尾添加的内容:

 mysql_close(); include "scripts/connect_to_mysql.php"; 

I know you fixed your issue, however, for future reference you should define each connection object when working with multiple connections. 我知道您已解决问题,但是,为以后参考,在使用多个连接时应定义每个连接对象。 If your main database is on the same mysql server then you should be able to do a mysql_select_db($dbname); 如果您的主数据库在同一台mysql服务器上,那么您应该能够执行mysql_select_db($ dbname);。 after you run the wordpress code; 在您运行wordpress代码之后; instead of closing and reopening the connection. 而不是关闭并重新打开连接。 For multiple connections follow the information from this answer: How do you connect to multiple MySQL databases on a single webpage? 对于多个连接,请遵循以下答案中的信息: 如何在单个网页上连接到多个MySQL数据库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM