嵌套的CPT网址+帖子2帖子

时间:2015-01-13 03:12:58

标签: php wordpress

我正在使用具有两种不同自定义帖子类型的客户端网站:artistportfolio。我使用posts-to-posts插件创建了每个艺术家与其投资组合之间的关系。

艺术家网址设置为{siteUrl}/artist,而投资组合网址设置为接收艺术家姓名:{siteUrl}/artist/%artistname%

我写了以下内容以检查portfolio是否与artist相关联,如果是,请将portfolio的网址更改为{siteUrl}/artist/artist-name/portfolio-name。如果没有关联艺术家,则会将网址更改为{siteUrl}/portfolio/portfolio-name

function filter_portfolio_link( $post_link, $post ) {
  if ( $post->post_type === 'portfolio' ) {
    $connected = new WP_query( array(
      'post_type' => 'artist',
      'connected_type' => 'portfolios_to_artists',
      'connected_items' => $post,
      'nopaging' => true,
    ) );
    if ($connected->have_posts() ) {
      foreach ( $connected as $connectedPost ) {
        setup_postdata($connectedPost);
        if ($connectedPost->post_type === 'artist') {
          $artistName = $connectedPost->post_name;
          $first = false;
        }
      }
      $post_link = str_replace( '%artist_name%', $artistName, $post_link );
    }
    else {
      $post_link = str_replace( 'artist/%artist_name%', 'portfolio', $post_link);
    }
  }
  return $post_link;
}
add_filter( 'post_type_link', 'filter_portfolio_link', 10, 2);

这会提取正确的数据并将其正确插入到URL中,但会有大量的PHP通知。我此刻并不那么担心。

因此,虽然这会正确地更改slug,但它并没有改变永久链接,而且我在前端获得了404错误。我想这需要一个重写功能与它配对?不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

因此,经过大量的时间和大量尝试不同的事情,我终于想出了正确的方法来做到这一点。我使用MonkeyMan Rewrite Analyzer和WP调试栏来帮助弄清楚如何构造重写。

首先,我的自定义帖子类型网址会被重写:

// Artist Rewrite Args
$rewrite = array(
  'slug'                => 'artist',
  'with_front'          => true,
  'pages'               => true,
  'feeds'               => true,
);

// Portfolio Rewrite Args
$rewrite = array(
  'slug'                => 'portfolio',
  'with_front'          => false,
  'pages'               => true,
  'feeds'               => true,
);

然后,我为艺术家名称注册了重写标记%artistname%,我们将通过WP_Query函数获取该重写标记,以及重写规则以填充要由艺术家的名字,将出现在展示组合的slu ..之前。

// Portfolio Rewrite Rule
add_action( 'init', 'portfolio_rewrite_tag' );
function portfolio_rewrite_tag() {

    add_rewrite_tag('%artistname%','[^/]+');
    // defines the rewrite structure for 'portfolios'
    // says that if the portfolio URL matches this rule, then it should display the 'artists' post whose post name matches the last slug set
    add_rewrite_rule( '^artist/[^/]+/([^/]+)/?$','index.php?portfolio=$matches[1]','top' );   
}

接下来,我改进了URL过滤器以检查它是否是一个投资组合帖子,如果是,请拉出第一个连接的artist的slug,然后将该slug拖到post link字符串中。

// Grab connected artist name and swap it into the URL
function filter_portfolio_link( $post_link, $post ) {
  if ( $post->post_type === 'portfolio' ) {
    $connected = new WP_query( array(
      'post_type' => 'artist',
      'connected_type' => 'portfolios_to_artists',
      'connected_items' => $post,
      'nopaging' => true,
    ) );
    if ($connected->have_posts() ) {
      $first = true;
      foreach ( $connected as $connectedPost ) {
          setup_postdata($connectedPost);
          if ($connectedPost->post_type === 'artist') {
            if ($first) {
              $artistName = $connectedPost->post_name;
              $first = false;
            }
          }
      }
      $post_link = str_replace( 'portfolio', 'artist/' . $artistName, $post_link );
    }
  }
  return $post_link;
}
add_filter( 'post_type_link', 'filter_portfolio_link', 10, 2);

就是这样!虽然我没有使用线程Nested custom post types with permalinks中讨论的相同插件,但整个帖子对于得出这个结论非常有帮助。