个性化阅读
专注于IT技术分析

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

朋友你好我被困在代码中。我在其中创建了一个自定义帖子类型, 并在页面模板中也将其称为类别。我使用以下代码显示帖子类型的类别。

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

现在, 当我单击某个类别时, 它会显示404错误页面而不显示我的帖子。请帮助我。我用以下代码创建了一个自定义帖子类型。

/* 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");
    }

#1


尝试这个

$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; ?>
赞(0)
未经允许不得转载:srcmini » 如何在WordPress自定义帖子类型中按类别显示帖子

评论 抢沙发

评论前必须登录!