【问题标题】:wordpress widget and post content issuewordpress 小部件和发布内容问题
【发布时间】:2012-11-29 12:12:23
【问题描述】:

我有点卡住了。我正在使用的模板有一个名为 products 的自定义帖子类型。当我在这里添加帖子/页面时,我可以使用页面中的小部件代码显示它们。

小部件显示输入到自定义字段中的内容,但我一生都无法弄清楚如何修改代码以显示来自 custompost 类型内容部分的内容。有什么见解吗?

这是生成小部件的 shortcodes.php 文件中的代码。

   $count_posts = wp_count_posts('product');
    $published_posts = $count_posts->publish;
    $out ='';
    $out .=' <div class="pricing-table">';
        $out .= '<ul>';
        $counter = 0; 
        while ( have_posts() ) : the_post();
        $counter++;
        $product_price = get_post_meta($post->ID,'_product_price',true);
          $out .= '<li class="heading-column '.$class_heading.' color'.$counter.'">';
            $out .= '<h3>'.get_the_title().'</h3>';
            $out .= '<h5>';
            $out .= $currency ? $currency : "&#36;";
            $out .= $product_price;
            if ($billing_cycle == "none") {
              $out .= ""; 
            } else {
              $out .= ' per '.$billing_cycle; 
            }
            $out .= '</h5>';
          $out .= '</li>';
        endwhile;
        $out .= '</ul>';  

        $out .= '<div class="clear"></div>';
        $out .= '<ul>';
        while ( have_posts() ) : the_post();
        $product_url = get_post_meta($post->ID,'_product_url',true);
        $product_feature = get_post_meta($post->ID,'_product_feature',true);
        $features_list = explode(",",$product_feature);
        $counter++;
          $out .= '<li class="pric-column '.$class_column;
          if ($counter%$columns==0) $out .= '-last';
          $out .= '">';
            foreach ($features_list as $flist) {
            $out .= '<ul class="feature-list">';
              $out .= '<li>'.$flist.'</li>';
            }
            $out .= '<li class="last">';
            $out .= '<a class="button" href="'.$product_url.'"><span>'.$product_button_text.'</span></a>';
            $out .= '</li>';
            $out .= '</ul>';  
      endwhile;wp_reset_query();
      $out .= '</ul>';  
  $out .= '</div>';

  return '[raw]'.$out.'[/raw]';
}

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    您的代码不起作用,因为 the loop 没有被填写。
    在那里它将执行页面的默认循环。 (如果它还没有运行,那就是)

    你也做一些与$published_posts类似的事情。

    您应该使用WP_Query 创建一个自定义循环。

    帮助您入门:

    $query_args = array(
        'post_type' => 'products', // the post type name
        'post_status' => 'publish',
    );
    $products = new WP_Query ($query_args)
    
    // before loop stuff like opening tags
    while($products->have_posts()): $products->the_post();
        // do your stuff like display post_meta
    endwhile;
    // after loop stuff like closing tags
    

    这应该可以帮助您入门。
    如果您需要任何帮助,请告诉我!

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多