永久链接/自定义帖子类型问题 - 无法解决

时间:2013-10-28 11:33:25

标签: wordpress .htaccess permalinks

我在网站上的一个自定义帖子类型现在不再有效:数据库条目仍然存在,但我无法访问帖子。我认为这是已知的固定链接问题,但是经历了很多帖子并尝试了所提供的解决方案,我仍然坚持这个问题。

我有一个'single,php'文件,其中包含一些排序代码,然后重定向到三个不同的单个文件('single-author.php','single-book.php','single-event.php')。

我尝试过的事情:

  • 切换永久链接结构并切换回来
  • 刷新重写规则
  • 检查.htaccess中的重写规则

如果我在此自定义帖子类型中创建一个新条目,我可以在“预览”中看到它,但是一旦我发布它就会得到一个404.问题似乎不会影响我运行的其他自定义帖子类型在网站上。除了最近的WP更新,我没有对网站进行任何更改。

我附上下面的相关代码 - 任何指针或帮助都会非常感激,因为我不确定还有什么可以尝试解决这个问题。

非常感谢!

add_action('init', 'authors_register');

function authors_register() {

$labels = array(
    'name' => _x('Authors', 'post type general name'),
    'singular_name' => _x('Author', 'post type singular name'),
    'add_new' => _x('Add New', 'portfolio item'),
    'add_new_item' => __('Add New Author'),
    'edit_item' => __('Edit Author'),
    'new_item' => __('New Author'),
    'view_item' => __('View Author'),
    'search_items' => __('Search Authors'),
    'not_found' =>  __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
);

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'supports' => array('title','thumbnail','revisions','excerpt'),
    'taxonomies' => array('category', 'post_tag')
  ); 

register_post_type( 'author' , $args ); 

}

在single.php中对代码进行排序/切换:

<?php if ('post_type=book') { include (TEMPLATEPATH . '/single-book.php'); } 

elseif ('post_type=event') { include (TEMPLATEPATH . '/single-event.php'); } 

elseif ('post_type=author') { include (TEMPLATEPATH . '/single-author.php'); } 

else { include (TEMPLATEPATH . '/single-post.php'); } ?>

1 个答案:

答案 0 :(得分:0)

<?php if ('post_type=book') { include (TEMPLATEPATH . '/single-book.php'); } 

elseif ('post_type=event') { include (TEMPLATEPATH . '/single-event.php'); } 

elseif ('post_type=author') { include (TEMPLATEPATH . '/single-author.php'); } 

else { include (TEMPLATEPATH . '/single-post.php'); } ?>

将始终返回single-book.php

'post_type = books'将始终返回true。您必须在变量中包含post_type并检查每个

<?php 
$post_type =  get_post_type();

if ( $post_type == 'book') { include (TEMPLATEPATH . '/single-book.php'); } 
elseif ($post_type == 'event') { include (TEMPLATEPATH . '/single-event.php'); } 
elseif ($post_type == 'author') { include (TEMPLATEPATH . '/single-author.php'); } 
else { include (TEMPLATEPATH . '/single-post.php'); } ?>

此外,作者不是保留的帖子类型。保留的帖子类型是:

post
page
attachment
revision
nav_menu_item