未找到自定义分类中的自定义POST类型的固定链接

时间:2012-03-15 作者:nigedo

我试着将死亡医生的建议应用于this post 要实现以下URL结构:

实例com/activities=WP页
示例。com/activities/type term=自定义存档示例。com/活动/类型术语/活动帖子=自定义帖子

所有URL的工作,直到返回Wordpress“not found”错误的最低URL为止。

我不知道是否需要过滤重写规则才能使其正常工作。我在上面链接的帖子表明,我已经完成了所需的一切。

任何帮助都将不胜感激,谢谢。这是我的代码:

/*
 * Register custom content objects on initiation
 */
add_action(\'init\', \'register_type\'); // Type taxonomy (activities)
add_action(\'init\', \'register_activity\'); // Activity post-type
add_filter(\'post_type_link\', \'filter_activity_link\', 10, 2); // Filter permalinks for activity post-type

/*
 * Register Type custom taxonomy for activities
 */
function register_type() {

    $labels = array(
        \'name\'                  => (\'Types\'),
        \'singular_name\'         => (\'Type\'),
        \'add_new\'               => (\'Add New Type\'),
        \'add_new_item\'          => (\'Add New Type\'),
        \'edit_item\'             => (\'Edit Type\'),
        \'new_item\'              => (\'New Type\'),
        \'view_item\'             => (\'View Type\'),
        \'search_items\'          => (\'Search Type\'),
    );

    $args = array(
        \'labels\'                => $labels,
        \'public\'                => true,
        \'query_var\'             => true,
        \'rewrite\'               => array( \'slug\' => \'activities\', \'with_front\' => false ),

    );
    register_taxonomy( \'type\', \'activity\', $args );
}

/*
 * Register Activity custom post-type
 */
function register_activity() {

    $labels = array(
        \'name\'                  => (\'Activities\'),
        \'singular_name\'         => (\'Activity\'),
        \'all_items\'             => (\'All Activities\'),
        \'add_new_item\'          => (\'Add New Activity\'),
        \'edit_item\'             => (\'Edit Activity\'),
        \'new_item\'              => (\'New Activity\'),
        \'view_item\'             => (\'View Activity\'),
        \'search_items\'          => (\'Search Activities\')
    );

    $args = array(
        \'labels\'                => $labels,
        \'public\'                => true,
        \'show_in_nav_menus\'     => false, // Do not appear directly in navigation
        \'menu_position\'         => 5,
        \'hierarchical\'          => false,
        \'query_var\'             => true,
        \'supports\'              => array( \'title\', \'editor\', \'type\', \'revisions\', \'comments\' ),
        \'rewrite\'               => array( \'slug\' => \'activities/%tax_type%\', \'with_front\' => false )
    );

    register_post_type( \'activity\', $args );
}

/*
 * Update Type rewrite slug with applicable terms
 */
function filter_activity_link($link, $post) {
    if ($post->post_type != \'activity\')
    return $link;

if ($cats = get_the_terms($post->ID, \'type\'))
    $link = str_replace(\'%tax_type%\', array_pop($cats)->slug, $link);
return $link;
}
更新好的,您总是需要添加重写规则才能完成此过程。URL可能显示正确,但数据库无法理解。This post 极大地帮助了我this reference 有关regex的信息。

我只需要添加代码

add_rewrite_rule(\'^activities/([^/]*)/([^/]*)/?$\' ,\'index.php?activity=$matches[2]\',\'top\';
希望这对别人有帮助。

1 个回复
最合适的回答,由SO网友:sanchothefat 整理而成

wordpress中没有可自动替换的内容%tax_type% 在重写段塞中。您必须添加一些自定义代码才能像此答案中那样处理它https://stackoverflow.com/questions/7723457/wordpress-custom-type-permalink-containing-taxonomy-slug

结束

相关推荐

All in One Seo and Permalinks

我正在使用多功能搜索引擎优化,对于帖子标题格式选项,我将其作为:%post\\u标题%kewyord1关键字2等关键字1,关键字2是我为我的网站选择的关键字此外,在Permalinks中,我将其命名为/%postname%/,以便帖子名称显示在短链接/url中因此,使用这些配置,如果我在标题中使用“Books”进行发布,它将如下所示:Books关键字1关键字2等但该帖子的短链接或实际url如下所示:www.mydomain。com/书籍它不包含我在搜索引擎优化中指定的所有关键字我试着将Permalink更