在Wordpress中获取数组中的特定帖子

时间:2016-12-26 18:40:17

标签: php wordpress

对于单个页面,我需要更改代码以仅获取特定类别的项目列表。所以喜欢:选择食品类别为15的项目。

我尝试了不同的东西,但我在php方面不太好。那么请你帮我改变代码。这是列表发布的部分。

<div class="col-md-6">
    <div class="box_style_2" id="main_menu">
        <h2 class="inner">Menu</h2>
        <?php
        $terms = get_terms( 'foodcat', array(
            'include' => $category_food,
        ));
        ?>
        <?php
        $i = 1;
        foreach($terms as $term){ ?>
        <h3 <?php if($i=1){?>class="nomargin_top"<?php }else{}?> id="<?php echo esc_attr($term->slug);?>"><?php echo esc_attr($term->slug);?></h3>
        <p><?php echo esc_attr($term->description);?></p>
        <table class="table table-striped cart-list">
            <thead>
            <tr>
                <th>
                    <?php echo esc_html__( 'Item', 'quickfood' );?>
                </th>
                <th>
                    <?php echo esc_html__( 'Price', 'quickfood' );?>
                </th>
                <th>
                    <?php echo esc_html__( 'Order', 'quickfood' );?>
                </th>
            </tr>
            </thead>
            <tbody>
            <?php
            $food_arr = new WP_Query(
                array(
                    'post_type' => 'food',
                    'posts_per_page' => -1,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'foodcat',
                            'field' => 'id',
                            'terms' => $term->term_id,
                        )
                    )
                )

            );
            $ii = 1;
            if($food_arr->have_posts()) : while($food_arr->have_posts()) : $food_arr->the_post();
                $textmoney = get_post_meta(get_the_id(), '_cmb2_textmoney', true);
                ?>
                <tr id="detail-<?php the_id();?>">
                    <input type="hidden" value="<?php the_id();?>">
                    <td>
                        <h5><?php echo esc_attr($ii);?>.<span class="name-<?php the_id();?> item_name"><?php the_title();?></span>
                            <input type="hidden" id="name-<?php the_id();?>" value="<?php the_title();?>">
                        </h5>
                        <p>
                            <?php the_content();?>
                        </p>
                    </td>
                    <td>
                        <strong><?php if(isset($currency_menu) && !empty($currency_menu)){ echo esc_attr($currency_menu);}else{}?><span class="price-<?php the_id();?> price"> <?php echo esc_attr( $textmoney );?></span></strong>
                        <input type="hidden" id="price-<?php the_id();?>" value="<?php echo esc_attr( $textmoney );?>">
                    </td>
                    <td class="options">
                        <a href="#0"><i class="icon_plus_alt2 add-to-cart-button" id="<?php the_id();?>"></i></a>
                    </td>
                </tr>
                <?php $ii++; endwhile;endif;?>
            </tbody>
        </table>
        <hr>
        <?php } ?>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您只需要在此代码中输入类别编号:

sslOptions

确切地说 $.ajax({ type: "GET", cache: false, data: { result: JSON.stringify(info) }, url: "/best/Pages/DailyShiftReport/PageSection/IrrecoverableFilmScrap.php" }).done(function(response) { var my_dialog = $('#test_dialog_org').html(response); my_dialog.dialog(); }); 的位置,添加如下数字:

$food_arr = new WP_Query(
    array(
        'post_type' => 'food',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'foodcat',
                'field' => 'id',
                'terms' => $term->term_id,
            )
        )
    )

);