简体   繁体   English

如何在WordPress中显示类别列表及其所有帖子?

[英]How can I display a list of categories and all their posts in WordPress?

I'm working on a custom WordPress theme. 我正在研究自定义WordPress主题。

I'm trying to show a list of categories (as headings) for a custom post type, and underneath each category heading I would like to list the post titles and a single custom field. 我正在尝试显示自定义帖子类型的类别列表(作为标题),并且在每个类别标题下方,我想列出帖子标题和单个自定义字段。 I'm using the Types plugin. 我正在使用Types插件。 I know a little php and have some WordPress experience, but not quite enough to properly figure this out. 我知道一点点php并且有一些WordPress的经验,但是还不足以正确地解决这个问题。

Example: 例:

Custom Post Type: Menu Item 自定义帖子类型: 菜单项

Custom Field for This Post Type: Item Price 此帖子类型的自定义字段: 商品价格

Category: Sandwich Fillings 类别: 三明治馅料

Category: Soups 类别:

Desired result: 所需结果:

Sandwich Fillings 三明治馅料

Cheese - £#.## 奶酪-£#。##

Ham - £#.## 火腿-£#。##

Tuna - £#.## 金枪鱼-£#。##

... ...

Soups 汤类

Tomato 番茄

Chicken

Vegetable 蔬菜

The idea will be for new categories to be added on the fly (for example, one day they might start selling melts), and for WP to iterate through the categories as new ones are added, saving from having to hard-code in new categories on the page as they are added. 想法是要动态添加新类别(例如,有一天他们可能会开始销售熔胶),WP会在添加新类别时迭代这些类别,从而不必在新类别中进行硬编码在添加它们的页面上。

Here's what I have so far: 这是我到目前为止的内容:

<?php

    $args = array(
        'post_type' => 'menu-item',
        'orderby' => 'name',
        'parent' => 0
    );

    $categories = get_categories( $args );

    foreach ( $categories as $category ) {

        $posts = get_posts($args);
        $item_price = types_render_field( "item-price" );

        echo '<h2>' . $category->name . '</h2>';
        ?>
            <ul><?php
            foreach($posts as $post) {
            ?>
                <li><?php the_title(); ?>, <?php echo $item_price; ?></li>
            <?php 
        }       
    }
?>

What I'm getting is this: 我得到的是:

Sandwiches 三文治

Tomato, 番茄,

Swiss Cheese, 瑞士芝士,

French Brie, 法国布里

French Brie, 法国布里

... ...

Soups 汤类

Tomato, £2.60 番茄£2.60

Swiss Cheese, £2.60 瑞士奶酪,2.60英镑

French Brie, £2.60 法国干酪,2.60英镑

French Brie, £2.60 法国干酪,2.60英镑

Any help would be appreciated! 任何帮助,将不胜感激!

UPDATE 更新

This seems to have helped: 这似乎有所帮助:

<?php

    $args = array(
        'post_type' => 'menu-item'
    );

    $categories = get_categories( $args );
    $posts = get_posts($args);


    foreach ( $categories as $category ) {



        echo '<h2>' . $category->name . '</h2>';
        ?>

        <div class="menu-items-container">
            <?php foreach($posts as $post) { ?>
                <?php $item_price = types_render_field( "item-price" ); ?>
                <?php if (in_category($category)) { ?>
                    <p><?php the_title(); ?>, <?php echo $item_price; ?></p>
                <?php  } ?>
            <?php  } ?>
        </div>
<?php } ?>

And it's giving me the results that I was looking for. 这给了我一直在寻找的结果。 Stumbled upon this fix through experimentation and happy accident, so I'm aware that it may not be best practice - advice or suggestions for improvement would be welcome! 通过实验和意外事故偶然发现了此修复程序,因此我知道这可能不是最佳实践-欢迎提出改进的建议!

Something like this might be better, as it does not loop through all the posts for each category: 这样的事情可能会更好,因为它不会遍历每个类别的所有帖子:

<?php

    $args = array(
        'post_type' => 'menu-item'
    );

    $categories = get_categories( $args );

    foreach ( $categories as $category ) {

        echo '<h2>' . $category->name . '</h2>';

        $args['category'] = $category->term_id;
        $posts = get_posts($args); ?>

        <div class="menu-items-container">
            <?php foreach($posts as $post) { ?>
                <?php $item_price = types_render_field( "item-price" ); ?>
                <p><?php the_title(); ?>, <?php echo $item_price; ?></p>
            <?php  } ?>
        </div>
<?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM