Php 嵌套的CPT URL+;员额2个员额
我在一个客户网站上工作,该网站有两种不同的自定义帖子类型:Php 嵌套的CPT URL+;员额2个员额,php,wordpress,Php,Wordpress,我在一个客户网站上工作,该网站有两种不同的自定义帖子类型:artist和portfolio。我正在使用这个插件创建一个从每个艺术家到他们作品集的关系 艺术家URL设置为{siteUrl}/Artist,而公文包URL设置为接收艺术家名称:{siteUrl}/Artist/%artistname% 我写了以下内容来检查公文包是否连接到了艺术家,如果是,请将公文包的URL更改为{siteUrl}/artist/artist name/portfolio name。如果没有连接艺术家,它会将URL更改
artist
和portfolio
。我正在使用这个插件创建一个从每个艺术家到他们作品集的关系
艺术家URL设置为{siteUrl}/Artist
,而公文包URL设置为接收艺术家名称:{siteUrl}/Artist/%artistname%
我写了以下内容来检查公文包
是否连接到了艺术家
,如果是,请将公文包
的URL更改为{siteUrl}/artist/artist name/portfolio name
。如果没有连接艺术家,它会将URL更改为{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,但它并没有改变permalink,我在前端得到了404个错误。我想这需要一个重写函数来配合它?只是不知道从这里走到哪里。所以在花了很多时间,尝试了很多不同的事情之后,我终于找到了正确的方法。我使用和WP调试栏来帮助了解如何构造重写 因此,首先,我的自定义帖子类型URL被重写如下:
// 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\u Query
函数获取该标记,以及一个重写规则来填充艺术家名称占用的段塞,该段塞将出现在显示的公文包段塞之前
// 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过滤器,以检查它是否是公文包帖子,如果是,则拉第一个连接的艺术家的slug,然后将该slug塞进帖子链接字符串中
// 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);
就这样!虽然我没有使用与线程中讨论的插件相同的插件,但整个线程非常有助于得出这一结论。这个问题似乎更接近我的需要,但不确定如何用我当前的设置实现它…