无法获取自定义分类存档页面以显示帖子

Posted

技术标签:

【中文标题】无法获取自定义分类存档页面以显示帖子【英文标题】:Trouble getting custom taxonomy archive page to show posts 【发布时间】:2019-02-09 04:00:22 【问题描述】:

我创建了一个自定义帖子类型和与之关联的自定义分类。单个帖子以及自定义帖子类型存档页面可以正常工作。然而,无论我尝试什么,分类档案页面都会返回“发现注意”。任何帮助将不胜感激。

这是我在functions.php中的CPT和分类代码:

// Register Custom Post Type
function press_post_type() 

    $labels = array(
        'name'                  => _x( 'Press' ),
        'singular_name'         => _x( 'Press' ),
        'menu_name'             => __( 'Press' ),
        'name_admin_bar'        => __( 'Press' ),
        'archives'              => __( 'Press Archives' ),
        'attributes'            => __( 'Press Attributes' ),
        'parent_item_colon'     => __( 'Press:' ),
        'all_items'             => __( 'All Press' ),
        'add_new_item'          => __( 'Add New Press' ),
        'add_new'               => __( 'Add New' ),
        'new_item'              => __( 'New Press' ),
        'edit_item'             => __( 'Edit Press' ),
        'update_item'           => __( 'Update Press' ),
        'view_item'             => __( 'View Press' ),
        'view_items'            => __( 'View Press' ),
        'search_items'          => __( 'Search Press' ),
        'not_found'             => __( 'Not found' ),
        'not_found_in_trash'    => __( 'Not found in Trash' ),
        'featured_image'        => __( 'Featured Image' ),
        'set_featured_image'    => __( 'Set featured image' ),
        'remove_featured_image' => __( 'Remove featured image' ),
        'use_featured_image'    => __( 'Use as featured image' ),
        'insert_into_item'      => __( 'Insert into item' ),
        'uploaded_to_this_item' => __( 'Uploaded to this press' ),
        'items_list'            => __( 'Press list' ),
        'items_list_navigation' => __( 'Press list navigation' ),
        'filter_items_list'     => __( 'Filter press list' ),
    );
    $args = array(
        'label'                 => __( 'Press' ),
        'description'           => __( 'Press' ),
        'labels'                => $labels,
        'supports'            => array( 'title', 'editor', 'thumbnail', 'revisions', 'custom-fields'),
        'public'              => true,
        'hierarchical'        => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => true,
        'rewrite' => array(
            'slug' => 'press'
        ),
        'can_export'          => true,
        'exclude_from_search' => false,
      'yarpp_support'       => true,
        'taxonomies'  => array(),
        'publicly_queryable'  => true,
        'capability_type'     => 'post'
    );
    register_post_type( 'press', $args );


add_action( 'init', 'press_post_type', 0 );

// Taxonomy for Custom Post Type
add_action( 'init', 'create_press_custom_taxonomy', 0 );

function create_press_custom_taxonomy() 

  $labels = array(
    'name' => _x( 'Topics', 'taxonomy general name' ),
    'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Topics' ),
    'all_items' => __( 'All Topics' ),
    'parent_item' => __( 'Parent Topic' ),
    'parent_item_colon' => __( 'Parent Topic:' ),
    'edit_item' => __( 'Edit Topic' ), 
    'update_item' => __( 'Update Topic' ),
    'add_new_item' => __( 'Add New Topic' ),
    'new_item_name' => __( 'New Topic Name' ),
    'menu_name' => __( 'Topics' ),
  );    

  register_taxonomy('topics',array('press'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
        'show_admin_column' => true,
        'public' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'topics' ),
  ));

下面是 taxonony-topics.php 中的内容:

<?php
/**
 * The template for displaying archive pages.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package understrap
 */

get_header();
?>

<?php
$container   = get_theme_mod( 'understrap_container_type' );
?>

<div class="wrapper" id="archive-wrapper">

    <div class="<?php echo esc_attr( $container ); ?>" id="content" tabindex="-1">

        <div class="row">

            <!-- Do the left sidebar check -->
            <?php get_template_part( 'global-templates/left-sidebar-check' ); ?>

            <main class="site-main" id="main">

                <?php if ( have_posts() ) : ?>

                    <header class="page-header">
                        <?php
                        the_archive_title( '<h1 class="page-title">', '</h1>' );
                        the_archive_description( '<div class="taxonomy-description">', '</div>' );
                        ?>
                    </header><!-- .page-header -->

                    <?php /* Start the Loop */ ?>
                    <?php while ( have_posts() ) : the_post(); ?>

                        <article <?php post_class(); ?> id="post-<?php the_ID(); ?>">

                            <header class="entry-header">

                                <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ),
                                '</a></h2>' ); ?>

                                <?php if ( 'post' == get_post_type() ) : ?>

                                    <div class="entry-meta">
                                        <?php understrap_posted_on(); ?>
                                    </div><!-- .entry-meta -->

                                <?php endif; ?>

                                <?php
                                    $terms= get_terms(array('taxonomy'=>'topics'));
                                    foreach($terms as $term)
                                            echo '<a href="' . get_term_link($term) . '"><span>' . $term->name . '</span></a>';
                                    
                                ?>

                            </header><!-- .entry-header -->

                            <?php echo get_the_post_thumbnail( $post->ID, 'large' ); ?>

                            <div class="entry-content">

                                <?php
                                the_excerpt();
                                ?>

                                <?php
                                wp_link_pages( array(
                                    'before' => '<div class="page-links">' . __( 'Pages:', 'understrap' ),
                                    'after'  => '</div>',
                                ) );
                                ?>

                            </div><!-- .entry-content -->

                            <footer class="entry-footer">

                                <?php understrap_entry_footer(); ?>

                            </footer><!-- .entry-footer -->

                            </article><!-- #post-## -->

                    <?php endwhile; ?>

                <?php else : ?>

                    <?php get_template_part( 'loop-templates/content', 'none' ); ?>

                <?php endif; ?>

            </main><!-- #main -->

            <!-- The pagination component -->
            <?php understrap_pagination(); ?>

        <!-- Do the right sidebar check -->
        <?php get_template_part( 'global-templates/right-sidebar-check' ); ?>

    </div> <!-- .row -->

</div><!-- Container end -->

</div><!-- Wrapper end -->

<?php get_footer(); ?>

【问题讨论】:

【参考方案1】:

结帐WordPress template 文件文档

您可以简单地创建 archive-your-post-type.php 文件,为每种帖子类型设置不同的存档模板。

分类:

taxonomy-taxonomy-term.php
taxonomy-taxonomy.php
tag-slug.php
tag-id.php
category-slug.php
category-ID.php

【讨论】:

谢谢。我已经用 taxonomy-topics.php 完成了这个,但我的问题是循环没有返回任何帖子。

以上是关于无法获取自定义分类存档页面以显示帖子的主要内容,如果未能解决你的问题,请参考以下文章

如何为自定义帖子和分类法制作 Wordpress 存档页面?

如何为自定义帖子类型存档页面创建导航

Wordpress - 自定义帖子类型存档页面

Wordpress 在自定义帖子类型存档页面上按日期排序

自定义帖子类型中帖子格式的模板层次结构?

php 在主博客和存档页面上显示WordPress自定义帖子类型