【问题标题】:Wordpress - Custom taxonomy filter in theme?Wordpress - 主题中的自定义分类过滤器?
【发布时间】:2017-01-29 13:31:09
【问题描述】:

我想在 Easy Digital Downloads 插件中编辑结帐、收据和其他页面。
作为他们的documentation,我已将该文件复制到我当前主题的文件夹/edd_templates/
实际上,我有 2 个不同的类别(研讨会、软件),我正在寻找一种方法来显示 不同的表格 用于 每个单独的结帐页面强>这些类别。
所以,我试着用这个:

<?php
$args = array(
    'post_per_page' => '-1',
    'post_type' => 'download',
    'tax_query' => array(
        array(
            'taxonomy' => 'download_category',
            'field'    => 'slug',
            'terms' => 'workshop'
        )
)
);
$my_posts = get_posts( $args );
if ( $my_posts ) :
?>
// custom html table for first category goes here... //
<?php endif; ?>
<?php
$args1 = array(
'post_per_page' => '-1',
'post_type' => 'download',
'tax_query' => array(
    array(
        'taxonomy' => 'download_category',
        'field'    => 'slug',
        'terms' => 'software'
    )
)
);
$myy_posts = get_posts( $args1 );
if ( $myy_posts ) :
?>
// custom html table for second category goes here... //
<?php endif; ?>

但无论您属于哪个类别,它都会在结帐页面中显示两个表格...
我认为问题在于我已经告诉 wordpress 显示这些 html 表格,只要这些类别中有东西。不要过滤它们...
我该如何解决这个问题?

【问题讨论】:

    标签: php wordpress wordpress-theming taxonomy


    【解决方案1】:

    您可以通过多种方式解决此问题,我在下面向您展示其中一种, 首先为您想要的页面创建模板页面,例如 Checkout

    <?php
    /**
     * Template Name: Checkout
     *
     */
    

    然后将您的代码放入自定义模板文件中。 接下来进入管理仪表板,然后选择您的自定义模板 Checkout。

    该页面将显示您想要的输出。

    【讨论】:

      【解决方案2】:

      你的最后一段是对的,你没有放置任何过滤器。您需要先获取购物车中产品的类别。

      <?php $cart_items = edd_get_cart_contents(); //get the contents in the cart
      foreach ( $cart_items as $key => $item ): ?>
      
      <?php 
      $terms = wp_get_post_terms( $item->ID, 'download_category' ); //get the categories of the items in the cart
      foreach ( $terms as $term ) $categories[] = $term->slug; //form an array with those categories
      

      然后您需要检查该类别数组是否有您的术语

      if ( in_array( 'workshop', $categories ) ) {
      // custom html table for first category goes here... //
      }elseif ( in_array( 'software', $categories ) ){
      // custom html table for second category goes here... //
      else {
      // custom html table for regular downloads goes here... //
      }
      

      【讨论】:

        猜你喜欢
        • 2014-11-19
        • 2014-09-30
        • 2020-11-17
        • 2013-08-20
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-15
        相关资源
        最近更新 更多