如何使用ARCHIVE-{post_type}.php主题模板?

时间:2015-07-15 作者:Luca Reghellin

我有一个staff_member 自定义帖子类型,带有\'has_archive\' => true. 我有一个archive-staff_member.php 模板,带/* Template Name: Staff */

在backoffice中,为了将其添加到nav菜单,我创建了一个页面并分配了模板人员,然后将其添加到主菜单。

直到这里,一切都很好。问题是,当我导航到staff页面时,即使是archive-staff\\u成员。php加载正确,wp\\U查询错误。换句话说,is_post_type_archive()is_post_type_archive(\'staff_member\') 返回false。

那么这里有什么问题?

目前,我只使用一个自定义查询,但正如官方文档所述,自动化归档会更好。

顺便说一句:如果有办法从菜单页面添加归档文件,而不是创建一个“愚蠢的”员工页面,那会更好。

4 个回复
最合适的回答,由SO网友:Luca Reghellin 整理而成

Ok, an answer to all. First, thank you for you help.

Anyway, none of your solutions really address my issue. I don\'t like custom links on nav menus, at all. And that\'s because they\'re not dynamic, you cannot build them dynamically. So when you move the site and change url (for example, from staging to production) you\'ll have to remember to manually change the links. Also, the client, who often is not a programmer, nor too used to computer/web/urls things, won\'t be able to properly manage it by himself.

EDIT: I\'ve found a solution to the above issue, so now Marek\'s answer can be considered reliable, at least to me: we can filter the nav_menu-generated urls so that they\'ll always use real site url:

function normalize_url($atts){
    if( !parse_url($atts[\'href\'], PHP_URL_SCHEME) && $atts[\'href\'] != \'#\'){
        $atts[\'href\'] = site_url() . $atts[\'href\'];
    }
    return $atts;
}

add_filter(\'nav_menu_link_attributes\', \'normalize_url\');


//Marek\'s way will cause wp_nav_menu not to correctly 
//output parent/child  classes, so here\'s an example of 
//how to solve this issue too:
function fix_nav_classes($classes, $item){

    //say I\'ve got some custom links: the \'Projects\' parent and a child that 
    //points to \'/selected_project/\' and I want always these classes for both 
    //parents and child items
    if(is_post_type_archive(\'project\') || get_post_type() == \'project\'){
        if($item->post_title == \'Projects\') $classes[] = \'current-menu-parent\';
        if($item->url == \'/selected_project/\') $classes[] = \'current-menu-item\';
    }

  return $classes;
}

add_filter( \'nav_menu_css_class\', \'fix_nav_classes\', 10, 2 );  

OLD ANSWER > good as alternative:

The problem here, is that wordpress consider the \'page\' nature first: if you assign a custom post type template (according to rules) to a page, that will not work. I think it\'s probably a permalink thing -> page has different permalink than the archive. I think I could fix just modifying the page\'s permalink OR the rewrite rule on register_post_type so that the 2 are the same, but this is not a good practice too.

So for now, just dumb page and a \'classic\' custom query. For those who need, just this:

global $wp_query;

$query_array = array(
    \'posts_per_page\' => 15,
    \'post_type\' => \'staff_member\',
    \'post_status\' => \'publish\',
    \'orderby\' => \'menu_order\',
    \'order\' => \'ASC\'
);

$member = $wp_query->query($query_array);

I generally use this kind of code.. If one really needs all the wp sugar, he can also substitute the main query:

global $wp_query;
$original = $wp_query; // save for restore
$wp_query = new WP_Query();

$wp_query->query(array(
        \'posts_per_page\' => 15,
        \'post_type\' => \'staff_member\',
        \'post_status\' => \'publish\',
        \'orderby\' => \'menu_order\',
        \'order\' => \'ASC\'
    );

// here goes processing code and sugars
// if(have_posts()): while(have_posts()): the_post(); etc etc.. the_content() etc..

// restore the original
$wp_query = $original;

Have a nice day!

SO网友:Marek

自定义帖子类型存档页不需要“哑”页。事实上,可能有罪魁祸首。您应该尝试以下操作:

删除/* Template Name: Staff */ 来自archive-staff\\u成员。php(不需要)

  • 删除“哑”页面(如果已创建)。可能会有一些永久性的冲突
  • 转到管理>设置>永久链接并重新保存(它将重新生成重写规则)
  • 现在只需键入URL。。。您的站点。com/staff\\u成员/(取决于rewrite 在您的register_post_type(...) )
  • 您应该只看到“工作人员”帖子。如果是,则wp\\U查询正确,并且is_post_type_archive() 将返回true。如果它也使用了正确的存档模板,那么就完成了:)如果没有(可能是404错误?),然后是一些配置错误。

    当你有yoursite.com/staff_member/ URL,您可以将其作为“自定义链接”添加到导航菜单。无需创建“哑”页面。

    SO网友:Trang

    是的,功能是\\u post\\u type\\u archive only works in archive。phpTo在nav菜单上有一个指向staff\\u成员的链接项,您将添加一个自定义链接菜单项,而不是以文件tempate为模板的页面。当您vardump此项时,将获得该项的URL:

    get_post_type_archive_link(\'staff_member\')
    
    要自定义staff\\u成员的性能,请在文件存档中使用is\\U post\\U type\\U存档(“staff\\u成员”)。主题的php。

    SO网友:shashikant

    只需创建archive-{post_type}.php 创建自定义帖子类型并包含此功能时:flush_rewrite_rules();

    创建自定义帖子类型的页面。这叫做归档页。

    结束

    相关推荐

    Editing a theme's templates

    我已成功安装模板。我知道如何使用模块、文章和菜单创建。我不知道如何使用代码部分。如何更改css上的样式如何修改模板如何修改主页我已多次尝试在安装目录中查找文件夹,但未能找到。如何查找并更改它。

    如何使用ARCHIVE-{post_type}.php主题模板? - 小码农CODE - 行之有效找到问题解决它

    如何使用ARCHIVE-{post_type}.php主题模板?

    时间:2015-07-15 作者:Luca Reghellin

    我有一个staff_member 自定义帖子类型,带有\'has_archive\' => true. 我有一个archive-staff_member.php 模板,带/* Template Name: Staff */

    在backoffice中,为了将其添加到nav菜单,我创建了一个页面并分配了模板人员,然后将其添加到主菜单。

    直到这里,一切都很好。问题是,当我导航到staff页面时,即使是archive-staff\\u成员。php加载正确,wp\\U查询错误。换句话说,is_post_type_archive()is_post_type_archive(\'staff_member\') 返回false。

    那么这里有什么问题?

    目前,我只使用一个自定义查询,但正如官方文档所述,自动化归档会更好。

    顺便说一句:如果有办法从菜单页面添加归档文件,而不是创建一个“愚蠢的”员工页面,那会更好。

    4 个回复
    最合适的回答,由SO网友:Luca Reghellin 整理而成

    Ok, an answer to all. First, thank you for you help.

    Anyway, none of your solutions really address my issue. I don\'t like custom links on nav menus, at all. And that\'s because they\'re not dynamic, you cannot build them dynamically. So when you move the site and change url (for example, from staging to production) you\'ll have to remember to manually change the links. Also, the client, who often is not a programmer, nor too used to computer/web/urls things, won\'t be able to properly manage it by himself.

    EDIT: I\'ve found a solution to the above issue, so now Marek\'s answer can be considered reliable, at least to me: we can filter the nav_menu-generated urls so that they\'ll always use real site url:

    function normalize_url($atts){
        if( !parse_url($atts[\'href\'], PHP_URL_SCHEME) && $atts[\'href\'] != \'#\'){
            $atts[\'href\'] = site_url() . $atts[\'href\'];
        }
        return $atts;
    }
    
    add_filter(\'nav_menu_link_attributes\', \'normalize_url\');
    
    
    //Marek\'s way will cause wp_nav_menu not to correctly 
    //output parent/child  classes, so here\'s an example of 
    //how to solve this issue too:
    function fix_nav_classes($classes, $item){
    
        //say I\'ve got some custom links: the \'Projects\' parent and a child that 
        //points to \'/selected_project/\' and I want always these classes for both 
        //parents and child items
        if(is_post_type_archive(\'project\') || get_post_type() == \'project\'){
            if($item->post_title == \'Projects\') $classes[] = \'current-menu-parent\';
            if($item->url == \'/selected_project/\') $classes[] = \'current-menu-item\';
        }
    
      return $classes;
    }
    
    add_filter( \'nav_menu_css_class\', \'fix_nav_classes\', 10, 2 );  
    

    OLD ANSWER > good as alternative:

    The problem here, is that wordpress consider the \'page\' nature first: if you assign a custom post type template (according to rules) to a page, that will not work. I think it\'s probably a permalink thing -> page has different permalink than the archive. I think I could fix just modifying the page\'s permalink OR the rewrite rule on register_post_type so that the 2 are the same, but this is not a good practice too.

    So for now, just dumb page and a \'classic\' custom query. For those who need, just this:

    global $wp_query;
    
    $query_array = array(
        \'posts_per_page\' => 15,
        \'post_type\' => \'staff_member\',
        \'post_status\' => \'publish\',
        \'orderby\' => \'menu_order\',
        \'order\' => \'ASC\'
    );
    
    $member = $wp_query->query($query_array);
    

    I generally use this kind of code.. If one really needs all the wp sugar, he can also substitute the main query:

    global $wp_query;
    $original = $wp_query; // save for restore
    $wp_query = new WP_Query();
    
    $wp_query->query(array(
            \'posts_per_page\' => 15,
            \'post_type\' => \'staff_member\',
            \'post_status\' => \'publish\',
            \'orderby\' => \'menu_order\',
            \'order\' => \'ASC\'
        );
    
    // here goes processing code and sugars
    // if(have_posts()): while(have_posts()): the_post(); etc etc.. the_content() etc..
    
    // restore the original
    $wp_query = $original;
    

    Have a nice day!

    SO网友:Marek

    自定义帖子类型存档页不需要“哑”页。事实上,可能有罪魁祸首。您应该尝试以下操作:

    删除/* Template Name: Staff */ 来自archive-staff\\u成员。php(不需要)

  • 删除“哑”页面(如果已创建)。可能会有一些永久性的冲突
  • 转到管理>设置>永久链接并重新保存(它将重新生成重写规则)
  • 现在只需键入URL。。。您的站点。com/staff\\u成员/(取决于rewrite 在您的register_post_type(...) )
  • 您应该只看到“工作人员”帖子。如果是,则wp\\U查询正确,并且is_post_type_archive() 将返回true。如果它也使用了正确的存档模板,那么就完成了:)如果没有(可能是404错误?),然后是一些配置错误。

    当你有yoursite.com/staff_member/ URL,您可以将其作为“自定义链接”添加到导航菜单。无需创建“哑”页面。

    SO网友:Trang

    是的,功能是\\u post\\u type\\u archive only works in archive。phpTo在nav菜单上有一个指向staff\\u成员的链接项,您将添加一个自定义链接菜单项,而不是以文件tempate为模板的页面。当您vardump此项时,将获得该项的URL:

    get_post_type_archive_link(\'staff_member\')
    
    要自定义staff\\u成员的性能,请在文件存档中使用is\\U post\\U type\\U存档(“staff\\u成员”)。主题的php。

    SO网友:shashikant

    只需创建archive-{post_type}.php 创建自定义帖子类型并包含此功能时:flush_rewrite_rules();

    创建自定义帖子类型的页面。这叫做归档页。