如何仅为video格式文章的永久链接添加前缀?
Hey there! Let's break down how to fix this step by step—you're on the right track, just need to tweak the logic for targeting video post formats and set up proper rewrite rules to avoid duplicate content issues.
1. Fix the Video Post Format Targeting for $post_link
The most reliable way to customize permalinks for specific post formats is using the post_link filter. The mistake you might have made earlier is either incorrect format checking or not properly passing the post object. Here's a tested working snippet:
add_filter( 'post_link', 'custom_video_post_permalink', 10, 3 ); function custom_video_post_permalink( $permalink, $post, $leavename ) { // Ensure post format functions are available (for backward compatibility) if ( function_exists( 'has_post_format' ) ) { // Check if this post uses the video format if ( has_post_format( 'video', $post ) ) { // Modify the permalink to your desired structure here // Example: Add a "/video/" prefix before the post slug $permalink = home_url( '/video/' . $post->post_name . '/' ); } } return $permalink; }
This works better than manual if-statement tweaks because has_post_format() is WordPress' official function for checking post formats, and it properly validates against the post object passed to the filter—no guesswork involved.
2. Set Up Rewrite Rules & Avoid Duplicate Content
The original post URL (like yoursite.com/your-post-slug) will still be accessible after customizing the permalink, which creates duplicate content for SEO. We need two key steps to fix this:
Add a Rewrite Rule for the New URL Structure
This tells WordPress how to interpret your custom video post URLs:
add_action( 'init', 'register_video_rewrite_rule' ); function register_video_rewrite_rule() { add_rewrite_rule( '^video/([^/]+)/?$', // Match URLs starting with /video/[post-slug]/ 'index.php?post_type=post&name=$matches[1]&post_format=video', // Map to WordPress' internal query 'top' // Prioritize this rule over default ones ); }
Add 301 Redirects for Old URLs
This sends traffic from the original post URL to the new video-specific URL (permanent 301 redirects are best for SEO):
add_action( 'template_redirect', 'redirect_old_video_posts' ); function redirect_old_video_posts() { global $post; // Only run on single posts that use the video format if ( is_single() && $post && function_exists( 'has_post_format' ) ) { if ( has_post_format( 'video', $post ) && ! strpos( $_SERVER['REQUEST_URI'], '/video/' ) ) { wp_redirect( get_permalink( $post->ID ), 301 ); exit; } } }
Critical Final Step
After adding this code (to your theme's functions.php or a custom plugin), go to your WordPress Dashboard → Settings → Permalinks and click "Save Changes" (no need to modify any settings). This flushes the rewrite rules so WordPress recognizes your new URL structure.
Why Your Previous Attempts Might Have Failed
- You weren't properly validating the post format (e.g., using
get_post_format()without checking if the function exists, or comparing to the wrong value) - You didn't flush rewrite rules after making changes, so WordPress didn't pick up your custom URL structure
- Your custom permalink logic was conflicting with default WordPress rewrite rules
内容的提问来源于stack exchange,提问作者Tasa

