在注册帖子类型时包含自定义选项是一种良好的做法吗?

时间:2011-04-17 作者:Drew Gourley

与其说是一个问题,不如说是一个开放的讨论,因为我知道向post-type对象添加自定义参数确实有效,但我想知道这在技术上是否有任何错误。

基本上,注册帖子类型时:

$labels = array(
    \'name\' => _x(\'Drews\', \'post type general name\'),
    \'singular_name\' => _x(\'Drew\', \'post type singular name\'),
    \'add_new\' => __(\'Add New\'),
    \'add_new_item\' => __(\'Add New Item\'),
    \'edit_item\' => __(\'Edit Item\'),
    \'new_item\' => __(\'New Item\'),
    \'view_item\' => __(\'View Item\'),
    \'search_items\' => __(\'Search Items\'),
    \'not_found\' =>  __(\'No Items found\'),
    \'not_found_in_trash\' => __(\'No Items found in Trash\'), 
    \'menu_name\' => \'Archives\'
);
$rewrite = array(
    \'slug\' => \'drews\'
);
$args = array(
    \'labels\' => $labels,
    \'public\' => true,
    \'show_in_menu\' => true, 
    \'query_var\' => \'drews\',
    \'rewrite\' => $rewrite,
    \'has_archive\' => true, 
    \'hierarchical\' => false,
    \'supports\' => array(\'title\',\'editor\',\'author\',\'comments\'),
    \'_drew_made_this\' => true
); 
register_post_type(\'drews\',$args);
记下我的$args数组中的最后一行,“\\u draw\\u Make\\u this”设置为“true”。这使我可以通过几种有用的方式对此进行条件设置,比如在使用get\\u post\\u types()时如何过滤“\\u builtin”。正如我所说,我知道这是可行的,因为我正在使用它,但这是否有可能以任何奇怪的方式打破WordPress。

1 个回复
最合适的回答,由SO网友:Bainternet 整理而成

可以向register_post_type() $args,因为它们都保存在$wp_post_types 全局数组(例如:$wp\\u post\\u types[type]=$args),但我不确定这是否是最好的方法,因为这很容易在将来更改,然后您的函数将中断,更好的方法是在选项表中创建您自己的选项并使用它,我的意思是您可以使用get_post_types() 并以type=>$args的方式将它们保存为数组,这样您就不需要依赖核心功能来保持当前状态。希望这能引起轰动。

结束

相关推荐

Pagination with custom loop

我的问题可能是Pagination not working with custom loop, 但有一种不同。我使用自定义循环来显示flash游戏。我想按类别在游戏页面上分页。类别php:<?php if ($cat) { $cols = 2; $rows = 4; $paged = ((\'paged\')) ? get_query_var(\'paged\') : 1; $post_per_page = $cols * $rows; // -1 s