如何在新的WordPress页面模板中显示分类下的自定义帖子类型?

时间:2021-08-03 作者:Ashiquzzaman Kiron

我创建了这个插件,其中测验作为CPT,主题作为分类法。我需要以列表类型显示分类法下的CPT。但是,由于目前的原因,它没有显示以下代码-

将测验CPT和科目注册为分类法

/**
 * Register a custom post type called "quiz".
 *
 * @see get_post_type_labels() for label keys.
 */
function wpqb_init() {
    $label = array(
        \'name\'                  => _x( \'WP Quiz Bank\', \'qb\' ),
        \'singular_name\'         => _x( \'Quiz Bank\', \'qb\' ),
        \'menu_name\'             => _x( \'WP Quiz Bank\', \'qb\' ),
        \'name_admin_bar\'        => _x( \'WP Quiz\', \'qb\' ),
        \'add_new\'               => __( \'Add Quiz\', \'qb\' ),
        \'add_new_item\'          => __( \'Add New Quiz\', \'qb\' ),
        \'new_item\'              => __( \'New Test\', \'qb\' ),
        \'edit_item\'             => __( \'Edit Quiz\', \'qb\' ),
        \'view_item\'             => __( \'View Quiz\', \'qb\' ),
        \'all_items\'             => __( \'All Quizes\', \'qb\' ),
        \'search_items\'          => __( \'Search Quiz\', \'qb\' ),
        \'parent_item_colon\'     => __( \'Parent Quiz:\', \'qb\' ),
        \'not_found\'             => __( \'No Quiz found.\', \'qb\' ),
        \'not_found_in_trash\'    => __( \'No quiz found in Trash.\', \'qb\' ),
        \'featured_image\'        => _x( \'Quiz Cover Image\', \'qb\' ),
        \'set_featured_image\'    => _x( \'Set Quiz cover image\', \'qb\' ),
        \'remove_featured_image\' => _x( \'Remove Quiz cover image\', \'qb\' ),
        \'use_featured_image\'    => _x( \'Use as Quiz cover image\', \'qb\' ),
        \'archives\'              => _x( \'Quiz archives\', \'qb\' ),
        \'show_ui\' => true,
    );     

    $args = array(
        \'labels\'             => $label,
        \'description\'        => \'Quiz custom post type\',
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'supports\'           => array( \'title\', \'editor\', \'author\', \'thumbnail\'),
        \'show_in_rest\'       => false,
        \'rewrite\'            => array( \'slug\' => \'Quiz\' ),
        \'menu_position\'      => 20,
        \'menu_icon\'          => \'dashicons-welcome-learn-more\',
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,

        //\'taxonomies\'         => array( \'category\', \'post_tag\' ),
       
    );
      
    register_post_type( \'Quiz\', $args );


    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        \'name\'                       => _x( \'Subjects\', \'qb\' ),
        \'singular_name\'              => _x( \'Subject\', \'qb\' ),
        \'search_items\'               => __( \'Search Subjects\', \'qb\' ),
        \'popular_items\'              => __( \'Popular Subjects\', \'qb\' ),
        \'all_items\'                  => __( \'All Subjects\', \'qb\' ),
        \'parent_item\'                => null,
        \'parent_item_colon\'          => null,
        \'edit_item\'                  => __( \'Edit Subject\', \'qb\' ),
        \'update_item\'                => __( \'Update Subject\', \'qb\' ),
        \'add_new_item\'               => __( \'Add New Subject\', \'qb\' ),
        \'new_item_name\'              => __( \'New Subject Name\', \'qb\' ),
        \'separate_items_with_commas\' => __( \'Separate Subjects with commas\', \'qb\' ),
        \'add_or_remove_items\'        => __( \'Add or remove Subjects\', \'qb\' ),
        \'choose_from_most_used\'      => __( \'Choose from the most used Subjects\', \'qb\' ),
        \'not_found\'                  => __( \'No Subjects found.\', \'qb\' ),
        \'menu_name\'                  => __( \'Subjects\', \'qb\' ),
    );
 
    $args = array(
        \'hierarchical\'          => false,
        \'labels\'                => $labels,

        \'rewrite\'               => array( \'slug\' => \'subjects\' ),
    );
 
    register_taxonomy( \'subjects\', \'quiz\', $args );


}
add_action( \'init\', \'wpqb_init\' );
和页面模板代码

<!-- show cpt with taxonomy -->
    <?php
        $loop = new WP_Query( array( \'post_type\' => \'subjects\', \'posts_per_page\' => \'3\' ) );

            if ( $loop->have_posts() ) :

                while ( $loop->have_posts() ) : $loop->the_post();

                    // get all of the terms for this post, with the taxonomy of categories-projets.
                    $terms = get_the_terms( $post->ID, \'quiz\' );
                    the_title();

                    // create the span element, and write out the date this post was created.
                    echo "<div>" ;

                    foreach ( $terms as $term ) {
                        echo $term->name;
                    }

                    echo "</div>";


                endwhile;
                wp_reset_query();

            endif;
    ?>
任何帮助都将被告知

1 个回复
SO网友:rudtek

我看到一些可能导致问题的事情。

要回答您的问题,请执行以下命令:

$loop = new WP_Query( array( \'post_type\' => \'subjects\', \'posts_per_page\' => \'3\' ) );

正在对名为“的帖子类型”执行查询;受试者;。

我想你想做测验。所以这条线应该是:

$loop = new WP_Query( array( \'post_type\' => \'quiz\', \'posts_per_page\' => \'3\' ) );

我还要检查一下大写字母。当你注册你的帖子类型时,你称之为;测验“;,但后来称之为;测验;。它应该是小写的。您在更改CPT slug时也做到了这一点。

相关推荐