简体   繁体   English

如何在WordPress自定义帖子类型中按类别显示帖子

[英]how to show posts by category in WordPress custom post type

Hello Friends I am stuck in a code.你好朋友我被困在一个代码中。 where I have created a custom post type and called its categories also in the page template.我在其中创建了一个自定义帖子类型,并在页面模板中调用了它的类别。 I have used the below code for showing categories of post type.我使用以下代码来显示帖子类型的类别。

<?php
                $args = array(
                    'type'                     => 'post',
                    'child_of'                 => 0,
                    'parent'                   => '',
                    'orderby'                  => 'name',
                    'order'                    => 'ASC',
                    'hide_empty'               => 1,
                    'hierarchical'             => 1,
                    'exclude'                  => '',
                    'include'                  => '',
                    'number'                   => '',
                    'taxonomy'                 => 'readings_post-category',
                    'pad_counts'               => false );
                $categories = get_categories($args);

                    echo '<ul>';

                        foreach ($categories as $category) {
                            $url = get_term_link($category);?>
                            <li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
                        <?php
                        }

                    echo '</ul>';
            ?>

and now when I am clicking on a category its showing me 404 error page and not showing my posts.现在,当我点击一个类别时,它会显示 404 错误页面而不显示我的帖子。 Please help me in this.请帮助我。 I have created a custom post type with below code.我使用以下代码创建了一个自定义帖子类型。

/* readings Post Type */
if ( !function_exists('readings_category_register') ) {
        function readings_category_register() {
            $readings_permalinks = get_option( 'readings_permalinks' );
            $args = array(
                "label"                         => __('Topics'),
                "singular_label"                => __('Topic'),
                'public'                        => true,
                'hierarchical'                  => true,
                'show_ui'                       => true,
                'show_in_nav_menus'             => false,
                'args'                          => array( 'orderby' => 'term_order' ),
                'rewrite'           => array(
                    'slug'       => empty( $readings_permalinks['category_base'] ) ? __( 'readings-category' ) : __( $readings_permalinks['category_base']   ),
                    'with_front' => false
                ),
                'query_var'         => true
            );
            register_taxonomy( 'readings-category', 'readings', $args );
        }
        add_action( 'init', 'readings_category_register' );
    }
    /* readings POST TYPE
    ================================================== */
    if ( !function_exists('readings_register') ) {
        function readings_register() {
            $readings_permalinks = get_option( 'readings_permalinks' );
            $readings_permalink  = empty( $readings_permalinks['readings_base'] ) ? __( 'readings' ) : __( $readings_permalinks['readings_base']  );
            $labels = array(
                'name' => __('readings'),
                'singular_name' => __('readings'),
                'add_new' => __('Add New readings'),
                'add_new_item' => __('Add New readings'),
                'edit_item' => __('Edit readings'),
                'new_item' => __('New readings'),
                'view_item' => __('View readingss'),
                'search_items' => __('Search readings'),
                'not_found' =>  __('No readingss have been added yet'),
                'not_found_in_trash' => __('Nothing found in Trash'),
                'parent_item_colon' => ''
            );
            $args = array(
                'labels'            => $labels,
                'public'            => true,
                'show_ui'           => true,
                'show_in_menu'      => true,
                'show_in_nav_menus' => true,
                'menu_icon'=> 'dashicons-groups',
                'rewrite'           => $readings_permalink != "readings" ? array(
                    'slug'       => untrailingslashit( $readings_permalink ),
                    'with_front' => false,
                    'feeds'      => true
                )
                    : false,
               // 'supports' => array('title', 'editor'),
               'show_in_rest' => true,
               'supports' => array('title','editor', 'author', 'thumbnail', 'excerpt', 'comments','categories'),
                //"taxonomies" => [ "readings-category" ],
                'has_archive' => true,
                'taxonomies' => array('readings-category')
            );
            register_post_type( 'readings', $args );
        }
        add_action( 'init', 'readings_register' );
    }
    /* readings POST TYPE COLUMNS
    ================================================== */
    if ( !function_exists('readings_edit_columns') ) {
        function readings_edit_columns($columns){
            $columns = array(
                "cb" => "<input type=\"checkbox\" />",
                "title" => __("readings Name"),
                "description" => __("readings Description"),
                "readings-category" => __("readings Category")
            );
            return $columns;
        }
        add_filter("manage_edit-readings_columns", "readings_edit_columns");
    }

Try this尝试这个

$args = array(
'posts_per_page' => -1,
'post_type' => 'here write post name',
'orderby' => 'date',
'order' => 'DESC',
'category_name' => 'here write category name'
);
$loop = new WP_Query( $args );

<?php while ( $loop->have_posts() ) : $loop->the_post();?>
 ...
<?php endwhile; ?>

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

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