【问题标题】:Wordpress custom post type post and category links 404Wordpress 自定义帖子类型帖子和类别链接 404
【发布时间】:2012-01-01 08:49:20
【问题描述】:

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

我尝试了许多流行的解决方案,将 /%category%/%post_id%/ 附加到 url 结构,重写函数名称,但我没有快速实现。

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

任何添加额外参数以重写(见下文)的努力都会产生“解析错误:语法错误,意外';'”,即使没有';'存在。

'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']);
}

?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我整天都在与这个作斗争 :) 只需转到选项->永久链接并保存选项,这样它就会在数据库中重新生成重写规则。当您设置了永久链接选项,然后注册了自定义帖子类型时,就会发生这种情况。

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

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

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

    如果这没有帮助,check this out

    【讨论】:

    • 真的很令人沮丧,不是吗!我会试试你的解决方法并回复你。
    • 奇怪的是这会导致意外的';'错误,即使没有;存在于那条线上。
    • 贴在上面,我修改了一些函数名,每当我插入你的代码时它就会吐出错误。
    【解决方案2】:

    每当我遇到这种情况时,我都会查看 .htaccess,并很快意识到它不存在,或者 wordpress 永久链接 .htaccess 代码没有自行编写,需要复制/粘贴。

    仔细检查!

    【讨论】:

      【解决方案3】:

      事实证明,在涉及重写时,出现的顺序分类法和帖子类型很重要。

      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
          )
      );
      
      
      ?>
      

      【讨论】:

        【解决方案4】:
        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;
           }
         }
        

        【讨论】:

        • 纯代码答案对教育 SO 读者的作用很小。您的答案可能是也可能不是正确的答案,但它在被标记为“低质量”后处于审核队列中。请花点时间通过解释改进您的答案。
        猜你喜欢
        • 2011-06-24
        • 2011-07-24
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        相关资源
        最近更新 更多