自定义POST类型和固定链接问题

时间:2017-07-28 作者:viperguynaz

我有一个自定义的帖子类型“目标”设置。为每个目标分配常规职位,导致像http://example.com/credit/fix-my-credit/learn-how (格式为%category%/%goal%/%postname%) 其中:

类别(slug)=“信用”(credit)目标(slug)=“修复我的信用”(fix my credit)

  • 贴子(slug)=“了解如何”(learn how)
    • permalinks在大部分情况下工作。然而,在管理端,例如编辑帖子时,在编辑页面顶部的永久链接是正确的,但顶部管理栏中的“查看帖子”链接如下所示http://example.com/credit/%goal%/learn-how. 我在管理仪表板上最近的评论下的链接中看到了同样的情况。设置:

      设置>>永久链接>>自定义结构:/%category%/%goal%/%postname%/

      自定义帖子:

          $labels = array(
          \'name\'                  => _x( \'Goals\', \'Post Type General Name\', \'bonestheme\' ),
          \'singular_name\'         => _x( \'Goals\', \'Post Type Singular Name\', \'bonestheme\' ),
          \'menu_name\'             => __( \'Goals\', \'bonestheme\' ),
          \'name_admin_bar\'        => __( \'Goals\', \'bonestheme\' ),
          \'archives\'              => __( \'Goal Archives\', \'bonestheme\' ),
          \'parent_item_colon\'     => __( \'Parent Goal:\', \'bonestheme\' ),
          \'all_items\'             => __( \'All Goals\', \'bonestheme\' ),
          \'add_new_item\'          => __( \'Add New Goal\', \'bonestheme\' ),
          \'add_new\'               => __( \'Add New Goal\', \'bonestheme\' ),
          \'new_item\'              => __( \'New Goal\', \'bonestheme\' ),
          \'edit_item\'             => __( \'Edit Goal\', \'bonestheme\' ),
          \'update_item\'           => __( \'Update Goal\', \'bonestheme\' ),
          \'view_item\'             => __( \'View Goal\', \'bonestheme\' ),
          \'search_items\'          => __( \'Search Goals\', \'bonestheme\' ),
          \'not_found\'             => __( \'Not found\', \'bonestheme\' ),
          \'not_found_in_trash\'    => __( \'Not found in Trash\', \'bonestheme\' ),
          \'featured_image\'        => __( \'Featured Image\', \'bonestheme\' ),
          \'set_featured_image\'    => __( \'Set featured image\', \'bonestheme\' ),
          \'remove_featured_image\' => __( \'Remove featured image\', \'bonestheme\' ),
          \'use_featured_image\'    => __( \'Use as featured image\', \'bonestheme\' ),
          \'insert_into_item\'      => __( \'Insert into item\', \'bonestheme\' ),
          \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'bonestheme\' ),
          \'items_list\'            => __( \'Items list\', \'bonestheme\' ),
          \'items_list_navigation\' => __( \'Items list navigation\', \'bonestheme\' ),
          \'filter_items_list\'     => __( \'Filter items list\', \'bonestheme\' ),
      );
      $args = array(
          \'label\'                 => __( \'Goals\', \'bonestheme\' ),
          \'description\'           => __( \'Post Type Description\', \'bonestheme\' ),
          \'labels\'                => $labels,
          \'supports\'              => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\', \'comments\' ),
          \'taxonomies\'            => array( \'category\', \'tag\' ),
          \'hierarchical\'          => true,
          \'public\'                => true,
          \'show_ui\'               => true,
          \'show_in_menu\'          => true,
          \'menu_position\'         => 5,
          \'show_in_admin_bar\'     => true,
          \'show_in_nav_menus\'     => true,
          \'can_export\'            => true,
          \'has_archive\'           => true,
          \'exclude_from_search\'   => false,
          \'publicly_queryable\'    => true,
          \'capability_type\'       => \'page\',
          \'query_var\'             => true,
          \'rewrite\'               => array( \'slug\' => \'goal\', \'with_front\' => false ),
      );
      
      重写:

      function custom_rewrite_basic() {
      add_rewrite_rule(
              \'^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?\', 
              \'index.php?category_name=$matches[1]&goal=$matches[2]&goal_article=$matches[3]\', 
              \'top\'
          );
      }
      add_action(\'init\', \'custom_rewrite_basic\');
      
      Permalink过滤器:

      function update_article_permalink( $permalink, $post_id ) {
      global $post;
          if ( get_post_type($post_id) === \'post\' ) {
              $the_goal = get_article_goal($post_id);
              $post = get_post($post_id);
              $post->post_name = sanitize_title($post->post_title);
              $post_link = $permalink;
      
              if (!empty($the_goal)) {
                  $goal_slug = $the_goal[\'slug\'];
                  $post_link = str_replace( \'%goal%\', $goal_slug, $permalink );
              } else {
                  $slug = $post->post_name;
                  $post_link = str_replace( \'%goal%/\', \'\', $permalink );
              }
              $permalink = $post_link;
          }
      
          if (  get_post_type($post_id) === \'goal\' ) {
              $categories = get_the_category($post_id);
              if (count($categories) > 0){
                  $first_cat = $categories[0]->slug;
                  $permalink = preg_replace( \'(\\/goal\\/)\', \'/\'.$first_cat.\'/\', $permalink );
              }
          }
      
          return $permalink;
      }
      add_filter( \'get_sample_permalink\', \'update_article_permalink\', 10, 2 );
      
      我错过了什么?我怎样才能%goal% 是否在所有permalinks中替换?

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

    的筛选器post permalinks是post_link.

    钩住你的update_article_permalink 功能到post_link 筛选以在任何时候应用您的函数get_permalink 被调用。

    结束

    相关推荐

    Change Taxonomy Permalinks

    我有自定义帖子,我创建了一个显示所有自定义帖子的页面。示例:www.example.com/archive-page我想知道是否可以更改与此自定义帖子相关的类别和标签的永久链接。现在我有:www.example.com/my-custom-post-type-cats/my-category-1www.example.com/my-custom-post-type-tags/my-tag-1</我想要这样的东西:www.example.com/archive-page?category=1www.e