如何拥有动态插件并呈现归档文件-{post_type}.php?

时间:2019-05-24 作者:Frank Mendez

因此,我有一个名为network的自定义帖子类型和一个名为company\\u category的自定义分类法。在我无法渲染之前,一切都很好http://localhost/digitalhxstaging/company. 除了档案网络,公司类别和公司都很好。php。我尝试删除动态slug及其工作,但现在我的公司类别和公司帖子没有显示其正确链接。请帮帮我。我希望这是有意义的。下面是带有后期链接功能的代码。

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;  
    }
以下是正确的URL

enter Here is the correct URL which is working

让我知道你的想法。我只是需要http://localhost/digitalhxstaging/company 工作

2 个回复
SO网友:Faye

重置永久链接(仪表板>设置>永久链接)。只要做一个更改,保存,它就会恢复您的重写更改。我犯了无数次这个错误,你调整了一件事,却忘记了这是必要的。

SO网友:Frank Mendez

好的,所以解决方案就是将此代码添加到$args

\'has_archive\' => \'companies\',

相关推荐

Pagination in Archives

你好,我是wordpress的新手,我很烂。我试图在wordpress中为我的博客页面添加编号分页。我下载了插件“page navi”,进入编辑器并更改了索引中的一个文件。php收件人: <?php if ( $wp_query->max_num_pages > 1 ) : ?> <div class=\"post-nav archive-nav\"> <?php wp_pagenavi();