用于定制POST类型归档的插件

时间:2012-05-14 作者:urok93

我创建了一个新的自定义帖子类型“Projects”,并希望在mysite上提供所有此类帖子的存档。com/项目。目前,所有项目的单发帖子都显示了一个slug,如下mysite所示。com/projects/project title,但当我访问mysite时。我拿到了404。

下面是我如何构建自定义帖子类型的:

   /* Create the Project Custom Post Type ------------------------------------------*/
 function create_post_type_project() 
 {
$labels = array(
    \'name\' => __( \'Projects\' ),
    \'singular_name\' => __( \'Project\' ),
    \'add_new\' => __(\'Add New\'),
    \'add_new_item\' => __(\'Add New Project\'),
    \'edit_item\' => __(\'Edit Project\'),
    \'new_item\' => __(\'New Project\'),
    \'view_item\' => __(\'View Project\'),
    \'search_items\' => __(\'Search Project\'),
    \'not_found\' =>  __(\'No project found\'),
    \'not_found_in_trash\' => __(\'No project found in Trash\'), 
    \'parent_item_colon\' => \'\'
  );

  $args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'exclude_from_search\' => true,
    \'publicly_queryable\' => true,
    \'show_ui\' => true, 
    \'query_var\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'menu_position\' => null,
    // Uncomment the following line to change the slug; 
    // You must also save your permalink structure to prevent 404 errors
    \'rewrite\' => array( \'slug\' => \'projects\' ),
    \'has_archive\' => true,
    \'supports\' => array(\'title\',\'editor\',\'thumbnail\'),

  ); 

  register_post_type(__( \'project\' ),$args);
    }

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

似乎没有什么不正确的地方-(我假设您已经保存了permalink结构,以便按照注释所示刷新重写规则?:))。

我建议使用此插件来确定url重定向问题:http://wordpress.org/extend/plugins/monkeyman-rewrite-analyzer/ - 用你的发现更新你的问题,也许有人能提供解决方案

但是(但这可能不是您问题的原因)。,您不应翻译帖子类型的名称,必须:

register_post_type(\'project\',$args);
而不是

register_post_type(__( \'project\' ),$args);
翻译是为了用户的利益,因此应该只在标签上显示,WordPress的内部名称不应该依赖于翻译。

SO网友:Terry Barriff

您粘贴的代码并不一定是错误的,但是您所要求的也可以使用\'rewrite\' => true

http://codex.wordpress.org 国家:

has_archive (布尔值或字符串)(可选)启用post类型存档。默认情况下,将使用$post\\U type作为归档段塞。

默认值:false

Note: Will generate the proper rewrite rules if rewrite is enabled. Also use rewrite to > change the slug used.

您的问题的解决方案可能是自定义帖子类型的模板不起作用或尚未创建,您可以尝试向函数中添加以下代码。php或插件功能:

function _post_type_template_smart(){

    global $post;

    $single_template_name = \'single-projects.php\';
    $archive_template_name = \'archive-projects.php\';

    if ( is_single() && \'projects\' == get_post_type() ){

        $template = locate_template(array($single_template_name), true);

        if(empty($template)) {

          include(PLUGIN_DIR . \'template/\' . $single_template_name);

          exit();
        }

    }else if( is_archive() && \'projects\' == get_post_type() ){

        $template = locate_template(array($archive_template_name), true);

        if(empty($template)) {

          include(PLUGIN_DIR . \'template/\' . $archive_template_name);

          exit();
        }

    }

}
add_filter(\'template_redirect\', \'_post_type_template_smart\');
在你的“单个项目”中。php“/”存档项目。php的页面,创建循环/查询/get\\u页面(以您喜欢的为准),以引入和显示内容:

    $args = array(
        //\'child_of\' => 0,
        \'sort_order\' => \'ASC\',
        \'sort_column\' => \'post_modified\',
        \'hierarchical\' => 1,
        \'parent\' => 0,
        \'post_type\' => \'projects\',
        \'post_status\' => \'publish\'
    );
    $pages = get_pages( $args );
    foreach ( $pages as $project ){
        $project_id = $project->ID;
        $project_link = get_page_link($project->ID);
        $project_title = $project->post_title;
        $content = $project->post_content;
        $author = $project->post_author;
        $posted_on = $project->post_date;

        if(empty($content)){
            $content = \'There is no description for this package\';
        }
        echo \'<div class="content">\';
        echo $content;
        echo \'</div>\';      

    }
希望这有帮助!

结束

相关推荐

Custom post type permalinks

我有一个名为“数字”的自定义帖子类型,我想实现URL的“帖子”如下http://website.com/post-name 而不是http://website.com/number/post-name.我试图将register\\u post\\u type中的“rewrite”参数设置为:\'rewrite\' => array( \'slug\' => \'\', \'with_front\' => false ), 但它没有起作用。即使我保存了永久链接设置以刷新它。(我将永久链接设