自定义帖子类型中的固定链接

时间:2011-03-01 作者:Ashfame

自定义帖子类型是否可以将永久链接作为domain.com/custom-slug/ 而不是domain.com/custom/custom-slug/?

我似乎无法实现它。rewrite 参数注册时默认为后一个参数,或使用\'rewrite\' => array( \'slug\' => \'blah-blah\' )

3 个回复
SO网友:MikeSchinkel

看看my answer 对于以下问题。您可以修改我的parse_request() 要获取所需内容:

  • "Remove taxonomy slug from a custom hierarchical taxonomy permalink".

    根据评论,我决定继续并将其作为插件的下一个版本来实现。它实际上只针对这个问题的用例和the prior referenced question. 随着时间的推移,我计划在所有潜在的用例中实施它,因为出现了问题或客户机来驱动需求。

    下面是您将放置在主题函数中的代码。php文件,用于设置post\\u类型\'custom\':

    add_action(\'init\',\'init_url_routes\');
    function init_url_routes() {
      $post_type = \'custom\'; // Change this to your actual post_type name
      register_url_route(array(\'post_type\'=>$post_type));
    }
    
    这是一个插件,你可以把它放进去/wp-content/mu-plugins/:

    <?php
    /*
    Filename: wp-extended.php
    Plugin Name: WP Extended for Taxonomy URL Routes
    Author: Mike Schinkel
    Version: 0.2
    */
    function register_url_route($args=array()) {
      WP_Extended::register_url_route($args);
    }
    class WP_Extended extends WP {
      static $root = array();
      static function on_load() {
        add_action(\'setup_theme\',array(__CLASS__,\'setup_theme\'));
      }
      static function register_url_route($args) {
        if (isset($args[\'taxonomy\']))
          self::$root[\'taxonomy\'][$args[\'taxonomy\']] = get_taxonomy($args[\'taxonomy\']);
        if (isset($args[\'post_type\']))
          self::$root[\'posts\'][$args[\'post_type\']] = get_post_type_object($args[\'post_type\']);
      }
      static function setup_theme() { // Setup theme is 1st code run after WP is created.
        global $wp;
        $wp = new WP_Extended();  // Replace the global $wp
      }
      function parse_request($extra_query_vars = \'\') {
        $path = $_SERVER[\'REQUEST_URI\'];
        $domain = str_replace(\'.\',\'\\.\',$_SERVER[\'SERVER_NAME\']);
        $root_path = preg_replace("#^https?://{$domain}(/.*)$#",\'$1\',WP_SITEURL);
        if (substr($path,0,strlen($root_path))==$root_path)
          $path = substr($path,strlen($root_path));
        list($path) = explode(\'?\',$path);
        $path_segments = explode(\'/\',trim($path,\'/\'));
        // This does not handle ordering priority of type to match yet
        $matched = $this->parse_post_type_request($path_segments);
        if (!$matched)
          $matched = $this->parse_taxonomy_request($path_segments);
        if ($matched) {
          // This is hamfisted but necessary in some cases.
          // TODO: Look into ways to have more finesse with this.
          remove_action(\'template_redirect\',\'redirect_canonical\');
        } else {
          parent::parse_request($extra_query_vars); // Delegate to WP class
        }
      }
      function parse_post_type_request($path_segments) {
        // This does not handle heirarchical pages yet
        $post_id = false;
        global $wpdb;
        $sql =<<<SQL
    SELECT
      ID
    FROM
      {$wpdb->posts}
    WHERE 1=1
      AND post_status=\'publish\'
      AND post_type=\'%s\'
      AND post_name=\'%s\'
    SQL;
        if (is_array(self::$root[\'posts\'])) {
          foreach(self::$root[\'posts\'] as $post_type => $post_type_object) {
            $sql = $wpdb->prepare($sql,$post_type,$path_segments[0]);
            $post_id = $wpdb->get_var($sql);
            if ($post_id) {
              $this->query_vars[($post_type==\'page\' ? \'page_id\' : \'p\')] = $post_id;
              unset($path_segments[0]);  // Remove from future consideration
              break;
            }
          }
        }
        return ($post_id);
      }
      function parse_taxonomy_request($path_segments) {
        $taxonomy_term = array();
        $parent_id = 0;
        if (is_array(self::$root[\'taxonomy\'])) {
          foreach(self::$root[\'taxonomy\'] as $taxonomy_slug => $taxonomy) {
            $terms = get_terms($taxonomy_slug);
            foreach($path_segments as $segment_index => $path_segment) {
              foreach($terms as $term_index => $term) {
                if ($term->slug==$path_segment) {
                  if ($term->parent!=$parent_id) { // Make sure we test parents
                    $taxonomy_term = array();
                  } else {
                    $parent_id = $term->term_id; // Capture parent ID for verification
                    $taxonomy_term[] = $term->slug; // Collect slug as path segment
                    unset($terms[$term_index]); // No need to scan it again
                  }
                  unset($path_segments[$segment_index]);  // Remove from future consideration
                  break;
                }
              }
            }
            if (count($taxonomy_term))
              break;
          }
          if (count($taxonomy_term)) {
            $path = implode(\'/\',$taxonomy_term);
            switch ($taxonomy_slug) {
              case \'category\':
                $this->query_vars[\'category_name\'] = $path;
                break;
              case \'post_tag\':
                $this->query_vars[\'tag\'] = $path;
                break;
              default:
                $this->query_vars[\'taxonomy\'] = $taxonomy_slug;
                $this->query_vars[\'term\'] = $path;
                break;
            }
          }
        }
        return count($taxonomy_term);
      }
    }
    WP_Extended::on_load();
    

SO网友:Bainternet

要完全删除段塞,URL结构如下所示:

http://domain.com/post-slug/

在register\\u post\\u类型集中

\'rewrite\' => array(\'slug\' => false, \'with_front\' => false)
希望这有帮助

SO网友:Max

虽然已经2年没有更新了,但下面的插件对我很有用:http://wordpress.org/plugins/remove-slug-from-custom-post-type/

仅供参考,我正在运行WP3.9.1 使用WP类型1.5.7

结束

相关推荐

curl problem or permalinks

我刚刚配置了我的VPS,我使用的是Centos,一切都很好,但如果我将永久链接设置为自定义结构,然后接受主页,没有帖子出现,它会显示404页,我想这是因为我没有启用curl,但我不知道我的php在哪里。我的centos中的ini文件?好的,我的卷曲被启用了,我检查过了phpinfo(); 这里是URLhttp://74.117.158.182/info.php但如果我在我的wordpress中设置了永久链接,那么接受主页,所有都会给我404页,你可以在这个URL上查看http://mbas.co.in如果