在WordPress中,将活动类添加到自定义post类型的日期存档涉及到自定义WordPress查询和模板文件的修改。以下是基础概念和相关步骤:
function create_custom_post_type() {
register_post_type('event',
array(
'labels' => array(
'name' => __('Events'),
'singular_name' => __('Event')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'events'),
'supports' => array('title', 'editor', 'thumbnail')
)
);
}
add_action('init', 'create_custom_post_type');
在主题文件夹中创建或编辑archive-{post-type}.php
文件(例如archive-event.php
)。
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('event'); ?>>
<header class="entry-header">
<h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="entry-meta">
<?php printf(__('Posted on %s', 'textdomain'), get_the_date()); ?>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_excerpt(); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part('template-parts/content', 'none'); ?>
<?php endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
在主题的CSS文件中添加活动类的样式。
.event {
background-color: #f0f0f0;
border-left: 4px solid #0073aa;
padding-left: 10px;
}
问题:日期存档页面没有显示自定义post类型的内容。
原因:可能是自定义post类型的注册代码没有正确执行,或者日期存档模板文件没有被正确调用。
解决方法:
functions.php
文件中,并且使用了正确的钩子(如init
)。archive-{post-type}.php
文件存在于主题文件夹中,并且文件名和路径正确。通过以上步骤,你应该能够成功地将活动类添加到自定义post类型的日期存档页面中。
领取专属 10元无门槛券
手把手带您无忧上云