我正在尝试创建一个模板,该模板将显示特定自定义类型的帖子列表。然而,我缺少一些基本的东西。到目前为止,我的情况如下:
功能。php
add_action( \'pre_get_posts\', \'query_filter\', 1);
function query_filter($query)
{
if (!$query->is_admin OR
$query->is_main_query() OR
is_page_template( \'page-events.php\' ))
{
$query->query_vars[\'posts_per_page\'] = 3;
}
return $query;
}
页面事件。php
<?php
/*
Template Name: Events Page
*/
?>
<?php get_header(); ?>
<?php get_template_part(\'includes/breadcrumbs\'); ?>
<?php get_template_part(\'includes/top_info\'); ?>
<div id="content" class="clearfix fullwidth">
<div id="left-area">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div>
<?php the_content(); ?>
<div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h2>
<?php endwhile; endif; ?>
</div>
</div>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
如您所见,我仍然没有尝试筛选自定义帖子类型,因为目前正在发生的是“the\\u content()”被重复了100多次(应该只有大约12篇帖子)。此外,\\u title()不会出现在页面的任何位置。
我错过了什么?
William更新了几条建议后,下面是我的更新代码:
EventManager。php这是创建自定义帖子类型的地方。
add_action(\'init\', \'Initialize\');
static function Initialize()
{
register_post_type(Event::POST_TYPE,
array(
\'labels\' => array(
\'name\' => \'Events\',
\'singular_name\' => \'Event\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Event\',
\'edit_item\' => \'Edit Event\',
\'new_item\' => \'New Event\',
\'view_item\' => \'View Event\',
\'search_items\' => \'Search Event\',
\'not_found\' => \'No Events Found\',
\'not_found_in_trash\' => \'No events found in trash\',
\'menu_name\' => \'Events\',
),
\'public\' => true,
\'publicly_queryable\' => true,
\'query_var\' => true,
\'rewrite\' => false,
\'show_ui\' => true,
\'hierarchical\' => false,
\'has_archive\' => true,
\'menu_icon\' => JUPITER_PLUGIN . \'images/small-greyscale.png\',
\'supports\' => array(
\'title\',
\'editor\',
\'author\',
\'thumbnail\',
\'excerpt\',
\'comments\',
\'revisions\'
),
)
);
global $wp_rewrite;
$gallery_structure = \'/\' . Event::SLUG . \'/%year%/id=%event_id%/%event%\';
$wp_rewrite->add_rewrite_tag("%event%", \'([^/]+)\', "event=");
$wp_rewrite->add_rewrite_tag("%event_id%", \'([0-9]+)\', "event_id=");
$wp_rewrite->add_permastruct(\'event\', $gallery_structure, false);
add_filter(\'post_type_link\', \'gallery_permalink\', 10, 3);
function gallery_permalink($permalink, $post_id, $leavename)
{
$post = get_post($post_id);
if ($post->post_type == Event::POST_TYPE)
{
$rewritecode = array(
\'%year%\',
\'%event_id%\'
);
$unixtime = strtotime($post->post_date);
$date = explode(" ",date(\'Y m d H i s\', $unixtime));
$rewritereplace = array(
$date[0],
$post->ID
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
return $permalink;
}
return $permalink;
}
存档事件。php
<?php
/*
* Template Name: Events Archive
*/
?>
<?php get_header(); ?>
<?php get_template_part(\'includes/breadcrumbs\'); ?>
<?php get_template_part(\'includes/top_info\'); ?>
<div>
<div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div>
<?php echo the_title(); ?>
</div>
<?php endwhile; endif; ?>
</div>
</div>
<?php get_footer(); ?>
这应该是我导航到域时自动加载的页面。com/事件。然而,该地址目前表示“未找到结果”。如果我在创建自定义帖子类型时没有包含自定义重写,那么这将按预期工作。我是否必须包含某种额外的重写才能将存档重定向到此页面?