WooCommerce从" remote"获得产品。小部件中的网站

时间:2014-05-19 17:54:15

标签: php wordpress woocommerce

我尝试在我的主网站上安装WooCommerce。我没有成功,因为我的Wordpress主题没有WooCommerce支持,布局也没有了。所以我决定在其独立网络的子域中安装WooCommerce,并使用它自己的WooCommerce -enabled主题。

现在我想在我的主网站上有一个小部件,它显示我的子域中的Woocommerce中的产品。这可能吗?我该怎么做?感谢。

1 个答案:

答案 0 :(得分:1)

您正尝试从主要位置访问数据库,这是不可能的。您必须使用新凭据覆盖wpdb,执行查询,显示结果,然后恢复为原始状态。

编写自己的代码,

global $wpdb;

$wpdb = new wpdb('subdomain-dbuser', 'subdomain-dbpass', 'subdomain-db', 'subdomain-dbserverver');

<div id="content" class="narrowcolumn">

<?php

$querystr = "
SELECT $wpdb->posts.* 
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
AND $wpdb->postmeta.meta_key = 'tag' 
AND $wpdb->postmeta.meta_value = 'email' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";

$pageposts = $wpdb->get_results($querystr, OBJECT);

?>
<?php if ($pageposts): ?>
<?php global $post; ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>

<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
 <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
  <div class="entry">
     <?php the_content('Read the rest of this entry »'); ?>
  </div>

  <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  
  <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endforeach; ?>

<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>

</div>

工作结束后

回归原始

global $wpdb;

$wpdb = new wpdb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

这段代码是一个想法,你必须在互联网上查看更好的一个,但我相信这应该对你有帮助。

或在主站点上安装woo-commerce并自定义其模板以使用远程数据库。

此致

Suyash