【问题标题】:WordPress Foreach showing only one resultWordPress Foreach 只显示一个结果
【发布时间】:2012-12-04 21:34:56
【问题描述】:

我有以下 WP 功能和我写的简码

function selected_inventory_products( $atts ) {
    extract( shortcode_atts( array(
        'brand' => '',
        'product' => '',
    ), $atts ) );

        global $wpdb;
        $inventory_products = $wpdb->get_results("SELECT * FROM wp_inventory, wp_inventory_images WHERE wp_inventory.inventory_id = wp_inventory_images.inventory_id and sort_order = 0 and inventory_manufacturer = '".$brand."'");

        foreach($inventory_products as $i_products){
        $return_string = '<div style="background: #F4F4F4; width: 100%;">';
        $return_string .= '<a href="http://www.devicemondo.com/products/item/'.$i_products->inventory_slug.'/">'.$i_products->inventory_name.'</a>';
        $return_string .= '</div>';
        }

        return $return_string;

}
add_shortcode( 'post_sidebar_products', 'selected_inventory_products' );

问题是当我在我的 WP 页面中使用简码 [post_sidebar_products] 时,即使有超过 15 个结果,我也只能得到 1 个结果?知道我在哪里犯了错误吗?

感谢您的帮助

【问题讨论】:

  • 您是否在数据库中运行 SQL 查询来检查 SQL 是否返回 1 个或 15 个结果?
  • 是的,这是我做的第一件事,我得到了所有 15 个结果

标签: wordpress loops foreach shortcode


【解决方案1】:

啊!刚看到。您每次都在循环内重新定义 $return_string !因为第一行,等号前没有点。所以替换这个:

    $return_string = '<div style="background: #F4F4F4; width: 100%;">';

有了这个:

    $return_string .= '<div style="background: #F4F4F4; width: 100%;">';

【讨论】:

  • 天哪,非常感谢!诸如小事造成了很多问题,哈哈。甚至没有注意到它。
猜你喜欢
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多