【问题标题】:Wordpress get_template_part not working - returns 404 page instead of loop-xxx.phpWordpress get_template_part 不起作用 - 返回 404 页面而不是 loop-xxx.php
【发布时间】:2013-03-12 20:23:01
【问题描述】:

通常我会尽力通过在线搜索或无休止的实验来找到答案。这一次我真的绝望了。我已经阅读了许多相关问题,但没有一个可以解决我的问题。

我在许多其他主题中使用“get_template_part”并且效果很好。这次不知道是什么原因造成的。

在我的主题中有一个注册的自定义帖子类型事件。事件中帖子的 url 看起来像 example.com/event/wine-tasting

问题是:example.com/event/ 返回一个 404 页面而不是排列所有事件帖子 (loop-event.php)

这是我的archive.php:

<?php get_header(); ?>
<?php if ( have_posts() ) the_post();?>
<?php rewind_posts();?>
<?php $post_type = get_post_type();
    get_template_part( 'loop', $post_type );?>
<?php get_footer(); ?>

这是我的循环事件.php:

<?php get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <div class="container">
        <?php the_title();?>
        <?php the_content();?>
    </div>
<?php endwhile; ?>

这就是我注册自定义帖子类型“事件”的方式:

function yd_create_post_type_event() 
{
$labels = array(
    'name' => __( 'Events','yd'),
    'singular_name' => __( 'Event','yd' ),
    'add_new' => __('Add New','yd'),
    'add_new_item' => __('Add New Event','yd'),
    'edit_item' => __('Edit Event','yd'),
    'new_item' => __('New Event','yd'),
    'view_item' => __('View Event','yd'),
    'search_items' => __('Search Event','yd'),
    'not_found' =>  __('No event found','yd'),
    'not_found_in_trash' => __('No event found in Trash','yd'), 
    'parent_item_colon' => ''
  );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true,
    'show_in_admin_bar' => true,
    'query_var' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'menu_icon' => 0,
    'supports' => array('title','editor','excerpt')

  ); 

 register_post_type('event',$args);
}

register_taxonomy(
    "event-category", 
    array("event"), 
    array(
        "hierarchical" => true, 
        "label" => __( "Event Categories",'yd' ), 
        "singular_label" => __( "Event Category",'yd' ), 
        "rewrite" => array('slug' => 'event')
    )
); 

非常感谢您的帮助。我已经尝试了一切,我希望是我犯了一些愚蠢的错误。但到目前为止我无法解决。提前谢谢!

【问题讨论】:

  • 这个问题已经解决了......基本上我认为 example.com/event/ 会返回 loop-event.php 但实际上它应该是像 example.com/event-category/all-events/ 等等……大脑短路了。

标签: php wordpress-theming custom-post-type wordpress


【解决方案1】:

当您在 WordPress 中添加新的自定义帖子类型时,您需要刷新/刷新永久链接。请转至:Settings &gt; Permalinks 并单击 保存更改 按钮(执行此操作两次)。这应该可以解决您的问题。

【讨论】:

  • 谢谢桑尼!我实际上只是尝试过它并且它有效,但只有在我创建了一个loop.php(没有-event)之后。因为我有其他帖子类型,所以我想使用 loop-event.php 和 loop-lecture.php 等。让我尝试将重写添加到自定义帖子类型寄存器...稍后再返回...
  • 我很高兴严,很高兴我能帮上忙。
【解决方案2】:

您需要刷新 WordPress 生成的重写规则。

创建如下函数:

function rewrite_flush() {
    flush_rewrite_rules();
}

如果自定义帖子类型在您的模板中,请使用:

add_action( 'after_switch_theme', 'rewrite_flush' );

如果自定义帖子类型在您的插件中,请使用:

register_activation_hook( __FILE__, 'rewrite_flush' );

@Sunny Johal 的回答恰恰相反。

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 2015-04-20
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多