1

So I have a Custom Post Type named network and Custom Taxonomy name company_category. Everything is all good until I can't render http://localhost/digitalhxstaging/company. Both Company Category and Company are all good except for archive-network.php. I tried removing the dynamic slug and its working but now my company category and company post is not showing its correct link. Please help me. I hope this makes sense. Below is the code with post link function.

public function company_post_type() {
        $labels = array(
            'name'                  => _x( 'Companies', 'companies', 'dhx-portal' ),
            'singular_name'         => _x( 'Company', 'company', 'dhx-portal' ),
            'menu_name'             => _x( 'Companies', 'companies', 'dhx-portal' ),
            'name_admin_bar'        => _x( 'Company', 'company', 'dhx-portal' ),
            'add_new'               => __( 'Add New', 'dhx-portal' ),
            'add_new_item'          => __( 'Add New Company', 'dhx-portal' ),
            'new_item'              => __( 'New Company', 'dhx-portal' ),
            'edit_item'             => __( 'Edit Company', 'dhx-portal' ),
            'view_item'             => __( 'View Company', 'dhx-portal' ),
            'all_items'             => __( 'All Companies', 'dhx-portal' ),
            'search_items'          => __( 'Search Companys', 'dhx-portal' ),
            'parent_item_colon'     => __( 'Parent Companys:', 'dhx-portal' ),
            'not_found'             => __( 'No companies found.', 'dhx-portal' ),
            'not_found_in_trash'    => __( 'No companies found in Trash.', 'dhx-portal' ),
            'featured_image'        => _x( 'Company Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'dhx-portal' ),
            'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'dhx-portal' ),
            'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'dhx-portal' ),
            'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'dhx-portal' ),
            'archives'              => _x( 'Company archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'dhx-portal' ),
            'insert_into_item'      => _x( 'Insert into company', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'dhx-portal' ),
            'uploaded_to_this_item' => _x( 'Uploaded to this company', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'dhx-portal' ),
            'filter_items_list'     => _x( 'Filter companies list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'dhx-portal' ),
            'items_list_navigation' => _x( 'Companies list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'dhx-portal' ),
            'items_list'            => _x( 'Companies list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
        );

        $args = array(
            'labels'             => $labels,
            'taxonomies'         => array('company_category'),
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'menu_position'      => 5,
            'menu_icon'          => 'dashicons-building',
            'rewrite'            => array( 'slug' => 'company/%company_category%', 'with_front' => false ),
            'query_var'          => true,
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => true,
            'supports'           => array( 'title', 'author', 'custom-fields' )
        );

        register_post_type( 'network', $args );
        flush_rewrite_rules();
    }

    /**
     * Company custom taxonomy
     * @since    0.1.0
     * @access   public
     */
    public function company_taxonomy() {

        $labels = array(
             'name' => __('Company Categories', 'dhx-portal'),
             'singular_name' => __('Category', 'dhx-portal'),
             'search_items' => __('Search Categories', 'dhx-portal'),
             'all_items' => __('All Categories', 'dhx-portal'),
             'parent_item' => __('Parent', 'dhx-portal'),
             'parent_item_colon' => __('Parent:', 'dhx-portal'),
             'edit_item' => __('Edit Category', 'dhx-portal'),
             'update_item' => __('Update Category', 'dhx-portal'),
             'add_new_item' => __('Add New Category', 'dhx-portal'),
             'new_item_name' => __('New Category', 'dhx-portal'),
             'menu_name' => __('Categories', 'dhx-portal'),
        );

        $args = array(
             'hierarchical' => true,
             'labels' => $labels,
             'show_ui' => true,
             'show_admin_column' => true,
             'query_var' => true,
             'rewrite' => array( 'slug' => 'company', 'with_front' => false ),
        );

        register_taxonomy( 'company_category', array( 'company_category' ), $args );
        flush_rewrite_rules();
    }

    /**
     * Company custom permalink
     * @since    0.1.0
     * @access   public
     */
    public function company_post_link( $post_link, $id = 0 ){
        $post = get_post($id);  
        if ( is_object( $post ) ){
            $terms = wp_get_object_terms( $post->ID, 'company_category' );
            if( $terms ){
                return str_replace( '%company_category%' , $terms[0]->slug , $post_link );
            }
        }
        return $post_link;  
    }

Here is the correct URL which is working

enter Here is the correct URL which is working

Let me know what you think. I just need http://localhost/digitalhxstaging/company to work

1
  • So you can get your company and categories working the way you want, right? But then archive-network.php doesn't render. Can you show the network custom post type?
    – Faye
    Commented May 27, 2019 at 20:20

2 Answers 2

0

Reset your Permalinks (Dashboard > Settings > Permalinks). Just make a change, save and it should pick up your rewrite change. I've made this mistake a million times, you adjust a thing and forget this is needed.

2
  • Already done that nothing happened Commented May 25, 2019 at 6:34
  • Hmm. Okay, will ask some clarifying questions up on the question so others will see it.
    – Faye
    Commented May 27, 2019 at 20:19
-1

Okay so the solution was just adding this code to the $args

'has_archive' => 'companies',
1
  • Thanks! @nmr for sharing Commented Jun 12, 2019 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.