WordPress.org

Ready to get started?Download WordPress

Forums

New Post Type is not functioning properly in Menu (3 posts)

  1. Shaun McMillan
    Member
    Posted 1 year ago #

    I've added new post-type functionality and it appears to be working in the backend, but the menus don't work properly on the front end. If I go to the archive it brings up a 404 page.

    The post-type is "assignment." The taxonomy/categories is called "lesson"
    You can see what it looks like in the backend

    http://drawalot.com/learn/lesson/compositing should go to my new assignment, but instead it navigates over to a whole different post http://drawalot.com/learn/compositing-putting-your-face-on-an-animals-body/

    Clicking on the other empty "lessons" taxonomy menu items takes you to the 404 page.

    I have created single-assignment.php as well as archive-assignment.php but no go.

    When I dropped this code into one of my templates it does retrieve the assignment post-type

    <?php
        $args = array( 'post_type' => 'assignment', 'posts_per_page' => 10 );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
            the_title();
            echo '<div class="entry-content">';
            the_excerpt();
            echo '</div>';
        endwhile;
    	?>

    Here's the code I added to functions.php

    // Custom PostType / Label & Taxonomy "Add new assignment"
    
    // The register_post_type() function is not to be used before the 'init'.
    add_action( 'init', 'my_custom_init' );
    
    /* Here's how to create your customized labels */
    function my_custom_init() {
    	$labels = array(
    		'name' => _x( 'Assignments', 'post type general name' ), // Tip: _x('') is used for localization
    		'singular_name' => _x( 'Assignment', 'post type singular name' ),
    		'add_new' => _x( 'Add New', 'Assignment' ),
    		'add_new_item' => __( 'Add New Assignment' ),
    		'edit_item' => __( 'Edit Assignment' ),
    		'new_item' => __( 'New Assignment' ),
    		'view_item' => __( 'View Assignment' ),
    		'search_items' => __( 'Search Assignments' ),
    		'not_found' =>  __( 'No Assignments found' ),
    		'not_found_in_trash' => __( 'No Assignments found in Trash' ),
    		'parent_item_colon' => ''
    	);
    
    	// Create an array for the $args
    	$args = array( 'labels' => $labels, /* NOTICE: the $labels variable is used here... */
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		'has_archive' => true,
    		'supports' => array( 'title', 'editor', 'author', 'thumbnail' , 'comments' )
    	); 
    
    	register_post_type( 'assignment', $args ); /* Register it and move on */
    }
    
    // hook into the init action and call create_assignment_taxonomies() when it fires
    add_action( 'init', 'create_assignment_taxonomies', 0 );
    
    // create two taxonomies, lessons and writers for the post type "assignment"
    function create_assignment_taxonomies() {
    
    	// Add new taxonomy, make it hierarchical (like categories)
    	$labels = array(
    		'name' => _x( 'Lessons', 'taxonomy general name' ),
    		'singular_name' => _x( 'Lesson', 'taxonomy singular name' ),
    		'search_items' =>  __( 'Search Lessons' ),
    		'all_items' => __( 'All Lessons' ),
    		'parent_item' => __( 'Parent Lesson' ),
    		'parent_item_colon' => __( 'Parent Lesson:' ),
    		'edit_item' => __( 'Edit Lesson' ),
    		'update_item' => __( 'Update Lesson' ),
    		'add_new_item' => __( 'Add New Lesson' ),
    		'new_item_name' => __( 'New Lesson Name' ),
    	); 	
    
    	register_taxonomy( 'lesson', array( 'assignment' ), array(
    		'hierarchical' => true,
    		'labels' => $labels, /* NOTICE: Here is where the $labels variable is used */
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => array( 'slug' => 'lesson' ),
    	));
    }
  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try re-saving your permasink structure in wp-admin > Settings > Permalinks.

  3. Shaun McMillan
    Member
    Posted 1 year ago #

    Thank you kindly, worked like a charm

Topic Closed

This topic has been closed to new replies.

About this Topic