【问题标题】:wordpress custom post type with custom slug and get variables带有自定义 slug 的 wordpress 自定义帖子类型并获取变量
【发布时间】:2014-02-03 15:58:36
【问题描述】:

我用这种方式在 wordpress 中定义了一个自定义帖子类型:

add_action('init', 'register_cima_fellowship');

function register_cima_fellowship() {
    $args = array(
        'labels' => $labels,
        'public' => true,
        'menu_position'=>20,
        'show_ui'=>true,
        'show_in_menu'=>true,
        'show_in_nav_menus' => true,
        'rewrite' => array('slug' => 'fellow'),    
        'supports' => array('title', 'editor','thumbnail'),
        'has_archive'=>true
    );
    register_post_type('cima_fellowship', $args);
}

我在一个名为 archive-cima_fellowship.php 的文件中创建了一个自定义存档,它可以正常工作,显示我所有的 cpt。

这个cpt有一个像status这样的元属性,用来定义其中一些是当前的,而另一些是过去的。我想要两个不同的存档页面,一个只显示他当前的,另一个只显示过去的。

现在我以这种方式使用 get 变量来解决这个问题mysite.com/cima_fellowship/?type=past

但我想要更简洁的网址,例如mysite.com/fellows/past

为此我在cpt的注册中定义了'rewrite' => array('slug' => 'fellow'),,但是我无法通过`mysite.com/fellows访问存档页面,我仍然要访问'mysite.com/cima_fellowship/',我不能了解如何设置 URL 的“样式”。

有什么建议吗?教程?指导?

提前致谢!

【问题讨论】:

    标签: wordpress url archive custom-post-type slug


    【解决方案1】:

    现在我不能 100% 确定这是否可行,但请尝试添加:

    'has_archive' => 'fellow'
    

    所以你会:

     function register_cima_fellowship() {
    $args = array(
        'labels' => $labels,
        'public' => true,
        'menu_position'=>20,
        'show_ui'=>true,
        'show_in_menu'=>true,
        'show_in_nav_menus' => true,
        'has_archive'=>true,
        'rewrite' => array('slug' => 'fellow', 'with_front' => false),    
        'supports' => array('title', 'editor','thumbnail'),
    
    );
    register_post_type('cima_fellowship', $args);
    }    
    

    大约 10 个月前我遇到了类似的问题,我添加了这个,似乎解决了我的问题。

    【讨论】:

    • 感谢您的回答。您是否认为通过添加该行我会访问像 mysite.com/fellow/ 这样的存档页面?不幸的是它不起作用......
    • 您有权访问您的 htaccess 文件吗?那里可能有一些偏远的东西会导致你的问题。据我所知,您的原始代码无需解决即可工作。
    • 再试一次上面的代码,我已经把它改回原来的代码,在你的重写数组中添加了“'with_front' => false”。再说一次,我不能说它会解决你的问题,但值得一试,wp 论坛上的其他用户已经成功了,有些人没有
    【解决方案2】:

    这是 WordPress 的一个问题,只要你有 'slug' => 'my-slug' 参数,它就可以工作,但你必须先改变你的永久链接结构。通过 WorkPress>Settings>Permalinks 将您的永久链接更改为随机的不同结构,然后返回到以前的结构。

    这应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2017-01-31
      • 2014-02-08
      • 2016-10-28
      • 2019-09-06
      • 1970-01-01
      • 2021-10-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      相关资源
      最近更新 更多