我试图在单击带有
www.website/custom_post/category/post_name
的链接时获取网址
我现在拥有的是
www.website/category/post_name
。
我当前的代码
<?php
$post_type = get_post_type( $post->ID );
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'post_status' => 'publish'
);
$the_query = new WP_Query( $args );
$my_categories = array();
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$terms = get_the_terms( get_the_ID(), 'category' );
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
if(!in_array($term->term_id, $my_categories))
$my_categories[] = $term->term_id;
}
endif;
}
wp_reset_postdata();
}
if(sizeof($my_categories)) { ?>
<h6>show only posy cats of post type test</h6>
<?php
foreach ($my_categories as $term_id) {
$category = get_term_by('id', $term_id, 'category');
if($category->slug!="all-articles") {
?>
<li><a href="<?php echo get_category_link( $category->term_id ) ?>">
<?php echo $category->name; ?>
</a></li>
<?php
}
}
}
这与 php 无关,但您也可以通过制作 Router 来使用 php 来实现。你可以在google上搜索一下。
但您也可以使用 .htaccess 文件和路径重定向和重写规则来做到这一点。这是我为您的示例准备的代码,将其写入服务器中的 .htaccess 中。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Rewrite rule for www.website/custom_post/category/post_name
RewriteRule ^custom_post/category/([^/]+)/([^/]+)/?$ index.php?category=$1&post_name=$2 [L,QSA]
# If the request is not a file or directory, pass it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]