繁体   English   中英

我想在 Wordpress 中显示类别名称和所有相关帖子

[英]I want to display the category name and all the related posts in Wordpress

我正在使用插件 CPT UI。 我想显示类别名称和属于该类别的帖子。 我希望它看起来像这样。 在此处输入图片说明

我想对每个类别重复此操作,以便每当添加新类别时,它都会在页面上显示类别名称和相关帖子。 到目前为止我已经尝试过这个

<div id="row-portfolio" class="row">
    <div class="col-md-12">
        <h2>Achtertuin</h2>
    </div>
</div>

<?php
$loop = new WP_Query( array(
    'post_type' => 'projecten',
    'cat' => '27', // Whatever the category ID is for your aerial category
    'posts_per_page' =>  10,
    'orderby' => 'date', // Purely optional - just for some ordering
    'order' => 'DESC' // Ditto
) );

while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!--single block-->

<a href="<?php the_permalink();?>" rel="bookmark">
    <div class="col-md-6 col-sm-6 col-xs-12 float-left">
        <div id="card" class="card">
            <?= get_the_post_thumbnail();?>
            <div id="card-body" class=" card-body ">
                <h5 class="card-title "><?php the_title(); ?></h5>
                <hr class="hrline">
                <p class="card-text ">
                    <?php the_field('secundaire_titel'); ?>
                </p>
            </div>
        </div>
    <div>
</a>
<!--single block-->
<?php endwhile; ?>

有人能帮我吗?

///////### 编辑 ###////////

对于想知道我的自定义帖子类型是什么样的人,我不再使用插件 CPTUI

<?php

function cptui_register_my_cpts_projecten() {

    /**
     * Post Type: Projecten.
     */

    $labels = [
        "name" => __( "Projecten", "custom-post-type-ui" ),
        "singular_name" => __( "Project", "custom-post-type-ui" ),
        "menu_name" => __( "Projecten", "custom-post-type-ui" ),
        "all_items" => __( "Alle projecten", "custom-post-type-ui" ),
        "add_new" => __( "Nieuw project", "custom-post-type-ui" ),
        "add_new_item" => __( "Voeg nieuw project toe", "custom-post-type-ui" ),
        "edit_item" => __( "Bewerk project", "custom-post-type-ui" ),
        "new_item" => __( "Nieuw project", "custom-post-type-ui" ),
        "view_item" => __( "Project bekijken", "custom-post-type-ui" ),
        "archives" => __( "projecten", "custom-post-type-ui" ),
    ];

    $args = [
        "label" => __( "Projecten", "custom-post-type-ui" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        'menu_position' => 10,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => "projecten",
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => [ "slug" => "projecten", "with_front" => true ],
        "query_var" => true,
        "menu_icon" => "dashicons-admin-home",
        "supports" => [ "title", "thumbnail", "excerpt" ],
    ];

    register_post_type( "projecten", $args );
}

add_action( 'init', 'cptui_register_my_cpts_projecten' );

function cptui_register_my_taxes_project_categorieen() {

    /**
     * Taxonomy: Project Categorieën.
     */

    $labels = [
        "name" => __( "Project Categorieën", "custom-post-type-ui" ),
        "singular_name" => __( "Project Categorie", "custom-post-type-ui" ),
    ];

    $args = [
        "label" => __( "Project Categorieën", "custom-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'project_categorieen', 'with_front' => true,  'hierarchical' => true, ],
        "show_admin_column" => false,
        "show_in_rest" => true,
        "rest_base" => "project_categorieen",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
    ];
    register_taxonomy( "project_categorieen", [ "projecten" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_project_categorieen' );

您需要遍历所有术语(类别)并在每个术语(类别)中设置查询:

<?php
// Get the array of all the term objects in your taxonomy.
$cats = get_terms( 'project_categorieen' );
// Loop through all the terms.
foreach ( $cats as $cat ) :
    ?>

    <div id="row-portfolio" class="row">
        <div class="col-md-12">
            <h2><?php echo $cat->name; // This gets your category name ?></h2>
        </div>
    </div>

    <?php
    $cat_loop = new WP_Query( [
        'post_type'      => 'projecten',
        // Need to use tax_query since it is a custom taxonomy. It's an array of arrays.
        'tax_query'      => [
            [
                'taxonomy' => 'project_categorieen',
                // Get the term id from term object.
                'terms'    => $cat->term_id,
            ],
        ],
        // Get all posts for each term/category
        'posts_per_page' => - 1,
    ] );

    while ( $cat_loop->have_posts() ) : $cat_loop->the_post(); ?>
        <!--single block-->

        <a href="<?php the_permalink(); ?>" rel="bookmark">
            <div class="col-md-6 col-sm-6 col-xs-12 float-left">
                <div id="card" class="card">
                    <?php get_the_post_thumbnail(); ?>
                    <div id="card-body" class=" card-body ">
                        <h5 class="card-title "><?php the_title(); ?></h5>
                        <hr class="hrline">
                        <p class="card-text ">
                            <?php the_field( 'secundaire_titel' ); ?>
                        </p>
                    </div>
                </div>
            </div>
        </a>
        <!--single block-->
    <?php endwhile; ?>
    <?php wp_reset_query(); // reset the query to start over. 
    ?>
<?php endforeach; // End the looping of the terms/categories ?>

暂无
暂无

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

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