Wordpress自定义帖子类型帖子和类别链接404

时间:2011-11-18 15:44:06

标签: php wordpress

我最近在wordpress网站上创建了几个自定义帖子类型,使用下面的代码,他们以正确的形式生成链接,例如root / category / id / postname但是每个链接指向完整的帖子,分页或类别404。

我尝试了一些流行的解决方案,将/%category%/%post_id%/追加到url结构,重写函数名称,但我无法快速完成。

将wordpress永久链接结构设置为默认值,例如root /?page_id = 1257一切正常。

任何为重写添加额外参数的努力(见下文)都会产生“Parse error:syntax error,unexpected';'”尽管没有';'在场。

'rewrite' => array(
'slug' => 'issue')

任何帮助表示赞赏 - 非常困惑,非常沮丧!

<?php

//      CUSTOM POST TYPE 1
add_action('init', 'mjwpress_register');

function mjwpress_register() {
    $args = array(
        'label' => __('Press'),
        'singular_label' => __('Press'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'new_item' => __('New Press Item'),
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'comments', 'revisions', 'page-attributes', 'post-formats')
    );

    register_taxonomy('press-category', array('article'), 
        array(
            'label' => 'Press Story Category', 
            'singular_label' => 'press-story-category',
            'public' => TRUE,
            'show_tagcloud' => TRUE,
            'hierarchical' => TRUE,
            'query_var' => TRUE,
            'menu_position' => 5,
            'rewrite' => TRUE)
    );

    register_post_type( 'mjwpress' , $args );
}


add_action('inthenews_init', 'inthenews_init');
add_action('save_post', 'save_mjwpress_options');

function inthenews_init(){
    add_meta_box('newsmeta', 'Press Options', 'mjwpress_meta_options', 'mjwpress', 'normal', 'low');

}

function mjwpress_meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $linkurl = $custom['linkurl'][0];
    $linktitle = $custom['linktitle'][0];
?>

<div class='form-wrap'>
    <div class='form-field'>
        <label for='linkurl'>Link to External Publication:</label>
        <input name='linkurl' value='<?php echo $linkurl; ?>' />
        <p>E.g. http://www.example.com/article-title.php</p>
    </div>

    <div class='form-field'>
        <label for='linktitle'>Title of External Publication:</label>
        <input name='linktitle' value='<?php echo $linktitle; ?>' />
        <p>E.g. Lib Dem Voice</p>
    </div>
</div>

<?php
}

function save_mjwpress_options(){
    global $post;
    update_post_meta($post->ID, 'linkurl', $_POST['linkurl']);
    update_post_meta($post->ID, 'linktitle', $_POST['linktitle']);
}

?>

4 个答案:

答案 0 :(得分:2)

我整天都在争吵:)只需转到选项 - &gt;永久链接并保存选项,这样它就会重新生成数据库中的重写规则。如果您设置了永久链接选项,并且在此之后注册了自定义帖子类型,则会发生这种情况。

如果使用W3 Total Cache之类的缓存插件,请在保存永久链接选项之前刷新缓存。

另外,请确保帖子类型名称没有冲突,请使用:

'rewrite'   => array('slug' => '<some-unique-prefix>')

如果这没有用,check this out

答案 1 :(得分:0)

每当发生这种情况时,我会看一下.htaccess,并很快发现它不存在或者wordpress永久链接.htaccess代码没有自己编写,需要复制/粘贴。

仔细检查!

答案 2 :(得分:0)

事实证明,在重写方面,订单分类和发布类型很重要。

请参阅http://mondaybynoon.com/2011/05/20/revisiting-custom-post-types-taxonomies-permalinks-slugs/

<?php

// Links post type...

// Taxonomy

$labels = array(
    'name'                          => 'Brands',
    'singular_name'                 => 'Brand',
    'search_items'                  => 'Search Brands',
    'popular_items'                 => 'Popular Brands',
    'all_items'                     => 'All Brands',
    'parent_item'                   => 'Parent Brand',
    'edit_item'                     => 'Edit Brand',
    'update_item'                   => 'Update Brand',
    'add_new_item'                  => 'Add New Brand',
    'new_item_name'                 => 'New Brand',
    'separate_items_with_commas'    => 'Separate Brands with commas',
    'add_or_remove_items'           => 'Add or remove Brands',
    'choose_from_most_used'         => 'Choose from most used Brands'
    );

$args = array(
    'label'                         => 'Brands',
    'labels'                        => $labels,
    'public'                        => true,
    'hierarchical'                  => true,
    'show_ui'                       => true,
    'show_in_nav_menus'             => true,
    'args'                          => array( 'orderby' => 'term_order' ),
    'rewrite'                       => array( 'slug' => 'cameras/brands', 'with_front' => false ),
    'query_var'                     => true
);

register_taxonomy( 'brands', 'cameras', $args );

//Post type, must come after the taxonomy...

register_post_type( 'cameras',
    array(
        'labels'                => array(
            'name'              => __( 'Cameras' ),
            'singular_name'     => __( 'Camera' )
            ),
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'supports'              => array( 'title', 'editor', 'thumbnail' ),
        'rewrite'               => array( 'slug' => 'cameras', 'with_front' => false ),
        'has_archive'           => true
    )
);


?>

答案 3 :(得分:0)

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
   if(is_category()) {
      $post_type = get_query_var('post_type');
      if($post_type)
         $post_type = $post_type;
      else
         $post_type = array('nav_menu_item', 'post', 'your_custom_post_type');
      $query->set('post_type', $post_type);
      return $query;
   }
 }