• Resolved LinusOnTheLine

    (@linusontheline)


    Trying to get some understanding regarding the post types.
    In this case I’m working on my own theme and in index.php i have this code:

    global $post;
    dx($post);
    dx(get_post_type());

    (the dx function is just a small function that makes this:
    function dx($a) { echo '<pre>'; var_dump($a); echo '</pre>';})

    Now, to the problem. In a plugin i register e new post type, let’s say ‘cars’. something like this:

    add_action( 'init', 'create_cars_post_type' );
    function create_cars_post_type() {
      register_post_type( 'cars',array(
          		'labels' => array(
            	'name' => __( 'Cars' ),
            	'singular_name' => __( 'Car' ),
            	'slug' => __('cars'),
           		'view_item' => __('show cars')
          	),
        	'public' => true,
          	'has_archive' => true,
          	'hierarchical' => false,
          	'rewrite' => array('slug'=>'cars')
        )
      );
    }

    The question regards the rewrite part. Lets say I create a nice car, let’s call it “volvo”. Now the url is nice and is [mydomain]/cars/volvo.
    However, my code from index.php gives me this:

    null
    boolean false

    Ok, it’s no post and the post type can’t be found.
    If i set the rewrite to false my index.php outputs the post object and my post type -> but the url is now [mydomain]?cars=volvo and that sucks. 🙂

    What am i missing? How can I have nice URL:s and make WP accept it as a post?

Viewing 1 replies (of 1 total)
  • Thread Starter LinusOnTheLine

    (@linusontheline)

    aaaah. sorry. forgot to RTFM.
    https://codex.wordpress.org/Function_Reference/register_post_type
    says:

    Note: If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.

Viewing 1 replies (of 1 total)

The topic ‘Not really getting the post types’ is closed to new replies.