Я хочу отобразить название категории и все связанные посты в Wordpress - PullRequest
1 голос
/ 12 марта 2020

Я использую плагин 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; ?>

Может ли кто-нибудь мне помочь?

/////// ### EDIT ### ////////

Я больше не использую плагин CPTUI для тех, кому интересно, как выглядит мой пользовательский тип поста здесь go

<?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' );

1 Ответ

0 голосов
/ 12 марта 2020

Вам необходимо l oop просмотреть все термины (категории) и настроить запрос в каждом из них:

<?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 ?>
...