WordPress.org

Ready to get started?Download WordPress

Forums

Weird little problem with adding custom post type functions (7 posts)

  1. Chris
    Member
    Posted 1 year ago #

    I have managed to add custom post types for "Videos" and "Photos" using the below code (the "Photos" example). For each one I have also added the functions you see under 'supports':

    // Add new post type for Photos
    add_action('init', 'cooking_photos_init');
    function cooking_Photos_init()
    {
    	$photo_labels = array(
    		'name' => _x('Photos', 'post type general name'),
    		'singular_name' => _x('Photo', 'post type singular name'),
    		'all_items' => __('All Photos'),
    		'add_new' => _x('Add new photo', 'photos'),
    		'add_new_item' => __('Add new photo'),
    		'edit_item' => __('Edit photo'),
    		'new_item' => __('New photo'),
    		'view_item' => __('View photo'),
    		'search_items' => __('Search in photos'),
    		'not_found' =>  __('No photos found'),
    		'not_found_in_trash' => __('No photos found in trash'),
    		'parent_item_colon' => ''
    	);
    	$args = array(
    		'labels' => $photo_labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields'),
    		'has_archive' => 'photos'
    	);
    	register_post_type('photos',$args);
    }

    All the "support" functions/ fields appeared ('title','editor','author','thumbnail','excerpt','comments','custom-fields') as expected.

    When I copy pasted to make a third type: "Images", several of the function/ fields did not show up. I went through line by line and it appears the breaking point was the archive line:

    'has_archive' => 'photos'

    when I changed it to:

    'has_archive' => 'images'

    the page went completely blank and a statement said "Invalid post type".

    What is it with the word "images" in that line?

    I am using a twentyeleven child theme. No idea what this could be.... Please help!

  2. bythegram
    Member
    Posted 1 year ago #

    $image_labels = array(
    		'name' => _x('Images', 'post type general name'),
    		'singular_name' => _x('Image', 'post type singular name'),
    		'all_items' => __('All Images'),
    		'add_new' => _x('Add new image', 'images'),
    		'add_new_item' => __('Add new image'),
    		'edit_item' => __('Edit image'),
    		'new_item' => __('New Image'),
    		'view_item' => __('View Image'),
    		'search_items' => __('Search in Images'),
    		'not_found' =>  __('No Images found'),
    		'not_found_in_trash' => __('No Images found in trash'),
    		'parent_item_colon' => ''
    	);
    	$args = array(
    		'labels' => $image_labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','author','thumbnail','excerpt','comments','custom-fields'),
    		'has_archive' => 'images'
    	);
    	register_post_type('images',$args);

    Works fine for me.

  3. Chris
    Member
    Posted 1 year ago #

    Thanks for the reply... There are strange anomalies happening. When I work locally, it works fine. When I work remotely, 'author' and 'comments' do not show up. However, when I use other custom post types for "Videos" "Recipes", it works (remotely).

    Sometimes, working with the above specifically, the whole page breaks and it says "Invalid post type", all centered around

    register_post_type('images',$args);

    There are a couple of weird things going on, it's complicated to explain. Absolutely no idea what the problem is...

  4. Chris
    Member
    Posted 1 year ago #

    Now 'author' and 'comments' do not show up for all custom post types, when they clearly did moments ago...
    This is the kind of mind game that drives me bonkers.

  5. bythegram
    Member
    Posted 1 year ago #

    hm that's odd. my default "go to" when something like this is happening is deactive all plugins and see if the problem is still there. If not, reactivate the plugins one by one until the troublesome plugin shows up.

    I realize how it might seem odd that a plugin could be causing this but everything is connected in one way or another and it might be worth a shot.

  6. Chris
    Member
    Posted 1 year ago #

    I de-activated all plugins and no change. I love wasting four hours on this kind of nonsense.

  7. Chris
    Member
    Posted 1 year ago #

    Ok I suspect it's the theme, since I deactivated the plugins. Who knows. I'm still getting "Invalid post type" - sometimes.

    Oh and then sometimes the 'author' and 'comments' fields show up, sometimes they don't.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.