Php Wordpress自定义帖子类型帖子和类别链接404
我最近在wordpress网站上创建了几个自定义帖子类型,使用下面的代码,它们以正确的形式生成链接,例如root/category/id/postname,但每个链接都指向完整帖子、分页或404类别 我已经尝试了很多流行的解决方案,在url结构中添加了/%category%/%post\u id%/,重写了函数名,但我没有什么进展 将wordpress permalink结构设置为默认值,例如root/?page\u id=1257,一切正常 任何添加额外参数进行重写(见下文)的努力都会产生“解析错误:语法错误,意外的“;”,即使没有“;”他在场Php Wordpress自定义帖子类型帖子和类别链接404,php,wordpress,Php,Wordpress,我最近在wordpress网站上创建了几个自定义帖子类型,使用下面的代码,它们以正确的形式生成链接,例如root/category/id/postname,但每个链接都指向完整帖子、分页或404类别 我已经尝试了很多流行的解决方案,在url结构中添加了/%category%/%post\u id%/,重写了函数名,但我没有什么进展 将wordpress permalink结构设置为默认值,例如root/?page\u 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']);
}
?>
链接到外部出版物:
我一整天都在与之抗争:)只要进入选项->永久链接并保存选项,它就会在数据库中重新生成重写规则。当您设置了permalinks选项,然后注册了自定义的帖子类型时,就会发生这种情况
如果您使用像W3 Total cache这样的缓存插件,请在保存永久链接选项之前刷新缓存
此外,请确保帖子类型名称中没有冲突,请使用:
'rewrite' => array('slug' => '<some-unique-prefix>')
'rewrite'=>数组('slug'=>“”)
如果这没有帮助,每当这种情况发生在我身上时,我都会查看.htaccess,并很快意识到它不在那里,或者wordpress permalink.htaccess代码没有自己编写,需要复制/粘贴
仔细检查 事实证明,在涉及重写时,出现在中的顺序分类法和post类型非常重要 看
真是令人沮丧,不是吗!我会试试你的解决办法,然后再打给你。奇怪的是,这会导致一个意外的“;”错误,即使没有;在上面的帖子中,我修改了一些函数名,每当我插入代码时,它都会抛出错误。纯代码的答案对读者的教育作用不大。您的答案可能是正确的,也可能不是正确的,但在被标记为“低质量”后,您的答案在审核队列中。请花点时间解释一下,以改进您的答案。
<?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
)
);
?>
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;
}
}