WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type + Custom Permalinks = 404 (1 post)

  1. wpAdm
    Member
    Posted 3 years ago #

    WordPress 3.0.1
    Templates: its subsidiary from the "Hybrid", but tried to standard

    Create your own Post Type "playmod".
    I want to change the URL. No amount of trying to experiment, it works only option

    /playmod/%postname%/

    If you try to add anything to the %postname%, 404 page appears, for example:

    /playmod/%year%/%postname%/
    /playmod/%category%/%postname%/

    Most likely this is because of my vinyl hands.
    Help understand. Why does not my type of links?

    add_action( 'init', 'create_my_post_type', 0 );
    
    function create_my_post_type() {
    	register_my_post_type_playmod();
    }
    
    /*
    *	Register post type 'playmod'
    */
    function register_my_post_type_playmod() {
    	$args = array(
    		'labels' => array(
    			'name' => __('Playmod'),
    		),
    		'public' => true,
    		'show_ui' => true,
    		'_builtin' => false,
    		'_edit_link' => 'post.php?post=%d',
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		#'rewrite' => array("slug" => "playmod", 'with_front' => false),
    		'query_var' => true,
    		'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'excerpt', 'comments'),
    		'taxonomies' => array('category'),
    	);
    
    	register_post_type( 'playmod' , $args );
    
    	// reset rules
    	flush_rewrite_rules();
    
    	// change URL
    	add_action('init', 'my_rewrite');
    	function my_rewrite() {
    		global $wp_rewrite;
    			//$wp_rewrite->flush_rules();
    			//$url_structure = '/playmod/%postname%/';
    			//$url_structure = '/playmod/%category%/%postname%/';
    		$url_structure = '/playmod/%year%/%postname%/';
    		$wp_rewrite->add_permastruct('playmod', $url_structure, false);
    	}
    
    	// Add filter to plugin init function
    	add_filter('post_type_link', 'my_permalink', 10, 3);
    	// Adapted from get_permalink function in wp-includes/link-template.php
    	function my_permalink($permalink, $post_id, $leavename) {
    		$post = get_post($post_id);
    		$rewritecode = array(
    			'%year%',
    			'%monthnum%',
    			'%day%',
    			'%hour%',
    			'%minute%',
    			'%second%',
    			$leavename? '' : '%postname%',
    			'%post_id%',
    			'%category%',
    			'%author%',
    			$leavename? '' : '%pagename%',
    		);
    
    		if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
    			$unixtime = strtotime($post->post_date);
    
    			$category = '';
    			if ( strpos($permalink, '%category%') !== false ) {
    				$cats = get_the_category($post->ID);
    				if ( $cats ) {
    					usort($cats, '_usort_terms_by_ID'); // order by ID
    					$category = $cats[0]->slug;
    					if ( $parent = $cats[0]->parent )
    						$category = get_category_parents($parent, false, '/', true) . $category;
    				}
    				// show default category in permalinks, without
    				// having to assign it explicitly
    				if ( empty($category) ) {
    					$default_category = get_category( get_option( 'default_category' ) );
    					$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
    				}
    			}
    
    			$author = '';
    			if ( strpos($permalink, '%author%') !== false ) {
    				$authordata = get_userdata($post->post_author);
    				$author = $authordata->user_nicename;
    			}
    
    			$date = explode(" ",date('Y m d H i s', $unixtime));
    
    			$rewritereplace =
    			array(
    				$date[0],
    				$date[1],
    				$date[2],
    				$date[3],
    				$date[4],
    				$date[5],
    				$post->post_name,
    				$post->ID,
    				$category,
    				$author,
    				$post->post_name,
    			);
    			$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
    		} else { // if they're not using the fancy permalink option
    		}
    		return $permalink;
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Topic