• Resolved Tim W

    (@twalker123)


    I love this theme, but I have two custom post types and I would like to use custom page templates for their archive pages (mostly just to add a nice title and some intro text).

    What files do I need to create for a page template that runs the loop for a custom post type? Are there files included I can use as a guide?

    Thank you!

Viewing 6 replies - 31 through 36 (of 36 total)
  • Thread Starter Tim W

    (@twalker123)

    Yep. Thanks again!

    Thanks.
    You’re welcome!

    Thanks all for this!
    One more thing Tim,
    you mentioned:

    The custom meta box is dealt with in a separate part of the plugin. It’s not really created when the CPT is, so I kept is separate:

    add_meta_box(
            'ct_Website_text_550e', // $id
            'Website', // $title
            'show_custom_meta_box', // $callback
            'site', // $page
            'normal', // $context
            'high' // $priority
    		);

    Can you please give me more details about the title meta-box? Did u write your own plugin? where did u put that code?
    I’m asking it because I’m trying to achieve the same result but for regular posts list

    Thread Starter Tim W

    (@twalker123)

    Hi Giorgio – yes, I created a plugin to add the CPT. I looked at some of the plugins that do it for you, but they’re really a lot more code than you need.

    Here is the code that creates the CPT:

    function site_post_init() {
        $nw_post_type = array(
              'labels' => array (
                'name' => 'Sites',
                'singular_name' => 'Site',
                'add_new_item' => 'Add New Site',
                'edit_item' => 'Edit Site',
                'new_item' => 'New Site',
                'view_item' => 'View Site',
                'search_items' => 'Search Sites',
                'not_found' => 'No sites found',
                'not_found_in_trash' => 'No sites found in trash',
              ),
              'taxonomies' => array('category'),
              'supports' =>
              array (
                'title' => 'title',
                'editor' => 'editor',
                'thumbnail' => 'thumbnail',
                'excerpt' => 'excerpt',
                'trackbacks' => 'trackbacks',
                'custom_fields' => 'custom-fields',
                'comments' => 'comments',
                'revisions' => 'revisions',
                'page_attributes' => 'page-attributes',
                'post_formats' => 'post-formats',
              ),
              'supports_reg_tax' =>
              array (
                'category' => '1',
                'post_tag' => '1',
              ),
              'capability_type' => 'post',
              'map_meta_cap' => true,
              'description' => 'NuggetWeb Websites',
              'menu_position' => 20,
              'public' => true,
              'hierarchical' => false,
              'has_archive' => true,
              'rewrite' =>
              array (
                'with_front' => true,
                'feeds' => false,
                'pages' => true,
                'ep_mask' => 0,
              ),
              'query_var' => true,
              'can_export' => true,
              'cf_columns' => NULL,
              );
        register_post_type( 'site', $nw_post_type );
    }
    add_action( 'init', 'site_post_init' );

    Here is the code for the meta boxes. It adds a field in the editor for the post type for the website URL:

    <?php
    
    /**
     * Adds a box to the main column on the Post and Page edit screens.
     */
    function nw_add_custom_meta_box() {
    
    	$screens = array( 'site' ); // add items to add to multiple post types
    
    	foreach ( $screens as $screen ) {
    		add_meta_box(
            'ct_Website_text_550e', // $id
            'Website', // $title
            'show_custom_meta_box', // $callback
            $screen, // $page
            'normal', // $context
            'high' // $priority
    		);
    	}
    }
    add_action( 'add_meta_boxes', 'nw_add_custom_meta_box' );
    
    /**
     * Prints the box content.
     *
     * @param WP_Post $post The object for the current post/page.
     */
    function show_custom_meta_box( $post ) {
    
    	// Add an nonce field so we can check for it later.
    	wp_nonce_field( 'ct_Website_text_550e', 'website_nonce' );
    
    	/*
    	 * Use get_post_meta() to retrieve an existing value
    	 * from the database and use the value for the form.
    	 */
    	$value = get_post_meta( $post->ID, 'ct_Website_text_550e', true );
    
    	echo '<label for="ct_Website_text_550e">';
    	_e( 'Website Address', 'myplugin_textdomain' );
    	echo '</label> ';
    	echo '<input type="text" id="ct_Website_text_550e" name="ct_Website_text_550e" value="' . esc_attr( $value ) . '" size="25" />';
    }
    
    /**
     * When the post is saved, saves our custom data.
     *
     * @param int $post_id The ID of the post being saved.
     */
    function nuggetweb_save_meta_box_data( $post_id ) {
    
    	/*
    	 * We need to verify this came from our screen and with proper authorization,
    	 * because the save_post action can be triggered at other times.
    	 */
    
    	// Check if our nonce is set.
    	if ( ! isset( $_POST['website_nonce'] ) ) {
    		return;
    	}
    
    	// Verify that the nonce is valid.
    	if ( ! wp_verify_nonce( $_POST['website_nonce'], 'ct_Website_text_550e' ) ) {
    		return;
    	}
    
    	// If this is an autosave, our form has not been submitted, so we don't want to do anything.
    	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    		return;
    	}
    
    	// Check the user's permissions.
    	if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
    
    		if ( ! current_user_can( 'edit_page', $post_id ) ) {
    			return;
    		}
    
    	} else {
    
    		if ( ! current_user_can( 'edit_post', $post_id ) ) {
    			return;
    		}
    	}
    
    	/* OK, it's safe for us to save the data now. */
    
    	// Make sure that it is set.
    	if ( ! isset( $_POST['ct_Website_text_550e'] ) ) {
    		return;
    	}
    
    	// Sanitize user input.
    	$my_data = sanitize_text_field( $_POST['ct_Website_text_550e'] );
    
    	// Update the meta field in the database.
    	update_post_meta( $post_id, 'ct_Website_text_550e', $my_data );
    }
    add_action( 'save_post', 'nuggetweb_save_meta_box_data' );
    
    ?>

    This is the code for the CPT taxonomy:

    <?php
    
    // Add product types taxonomy
    function nw_custom_taxonomy() {
        // Taxonomies Export code for CustomPress
    
        $labels = array(
    		'name'              => 'Product Types',
    		'singular_name'     => 'Product Type',
    		'search_items'      => 'Search Product Types',
    		'all_items'         => 'All Product Types',
    		'parent_item'       => 'Parent Product Type',
    		'parent_item_colon' => 'Parent Product Type:',
    		'edit_item'         => 'Edit Product Type',
    		'update_item'       => 'Update Product Type',
    		'add_new_item'      => 'Add New Product Type',
    		'new_item_name'     => 'New Product Type',
    		'menu_name'         => 'Product Types',
            'popular_items'     => 'Popular Product Types',
            'add_or_remove_items'        => 'Add or remove Product Types',
            'separate_items_with_commas' => 'Separate product types with commas',
            'choose_from_most_used'      => 'All Product Types',
    	);
    
    	$args = array(
    		'hierarchical'      => false,
    		'labels'            => $labels,
    		'show_ui'           => true,
    		'show_admin_column' => true,
    		'query_var'         => true,
    		'rewrite'           => array( 'slug' => 'product-types' ),
    	);
        register_taxonomy( 'product-types', array( 'site' ), $args );
    }
    add_action( 'init', 'nw_custom_taxonomy', 0 );
    
    ?>

    I hope that helps!

    Tim

    Cool! Picture clear now ??

    I’ll keep you posted as soon as I come up with a solution for the regular posts!

    Many thanks Tim
    G.

    Hey all!

    I figured that buying the unlimited license for Customizr-pro would have been the best solution!

    I’m now a proud pro-customer

    I will still work on a flat solution for Customizr-free

Viewing 6 replies - 31 through 36 (of 36 total)
  • The topic ‘Custom Post Type Archive Template’ is closed to new replies.