自定义POST类型的类中的Add_action‘init’

时间:2014-01-23 作者:user45561

I\'m trying to create archive pages for my custom post type. I create custom post types from within custom classes:

functions.php

include_once(get_template_directory() . \'/admin/admin_init.php\');

admin_init.php

include_once(ADMIN_CPT_DIR . \'/CPT.php\');

CPT.php

class CPT
{
    protected $cpt_name;

    public function __construct($cpt_name)
    {
        $this->cpt_name = $cpt_name;
        add_action(\'init\', array( $this, \'cpt_init\' ) );
    }

    public function cpt_init()
   {
        $labels = array(
        \'name\' => __(\'custom_name\', \'textDomain\'),
        \'singular_name\' => __(\'custom_name\', \'textDomain\')
         );

        $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'custom_name\'),
        \'supports\' =>array(\'title\',\'editor\', \'custom-fields\',\'thumbnail\')
    );

    register_post_type( $this->cpt_name, $args );
   }
}

if( is_admin() )
    $cpt = new CPT(\'cpt_child\');

The custom post type is registered and all works fine, but I always get a 404 when trying to access the archive page (archive-custom_name.php).

If I take the inner part of the cpt_init - method and put it into the functions.php like so:

add_action( \'init\', \'create_post_type\' );

    function create_post_type()
    {
        $labels = array(
        \'name\' => __(\'mycpt\', \'synTh_textDomain\'),
        \'singular_name\' => __(\'mycpt\', \'synTh_textDomain\')
        );

        $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'boom\'),
        \'supports\' =>array(\'title\',\'editor\', \'custom-fields\',\'thumbnail\')
        );  

        register_post_type( \'mycpt\', $args );
    }

everything works as expected. There is an archive-boom.php, which I can access via: www.domain.com/boom/

I flushed the permalinks settings and I have them set to postname.

Is there a difference, when attaching to a hook from inside a class??

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

您只在管理请求上创建类的实例,因此您的CPT在前端请求上不存在。而不是:

if( is_admin() )
    $cpt = new CPT(\'cpt_child\');
在所有请求上创建实例:

$cpt = new CPT(\'cpt_child\');

结束

相关推荐

Custom Post Row Actions

我偶然发现this question 在写这个问题的时候。我有一个问题是关于这个问题的。我发现你用的是get_delete_post_link 筛选为我的操作创建一个新的url(或一个类似的函数——在任何情况下,我都会将该函数与布尔值一起使用)。唯一的问题是,I don\'t know how to capture the event now. 考虑到我在谷歌上找不到很多关于行后操作的例子,我将不胜感激-/public function _wp_filter_get_delete_post_link( $

自定义POST类型的类中的Add_action‘init’ - 小码农CODE - 行之有效找到问题解决它

自定义POST类型的类中的Add_action‘init’

时间:2014-01-23 作者:user45561

I\'m trying to create archive pages for my custom post type. I create custom post types from within custom classes:

functions.php

include_once(get_template_directory() . \'/admin/admin_init.php\');

admin_init.php

include_once(ADMIN_CPT_DIR . \'/CPT.php\');

CPT.php

class CPT
{
    protected $cpt_name;

    public function __construct($cpt_name)
    {
        $this->cpt_name = $cpt_name;
        add_action(\'init\', array( $this, \'cpt_init\' ) );
    }

    public function cpt_init()
   {
        $labels = array(
        \'name\' => __(\'custom_name\', \'textDomain\'),
        \'singular_name\' => __(\'custom_name\', \'textDomain\')
         );

        $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'custom_name\'),
        \'supports\' =>array(\'title\',\'editor\', \'custom-fields\',\'thumbnail\')
    );

    register_post_type( $this->cpt_name, $args );
   }
}

if( is_admin() )
    $cpt = new CPT(\'cpt_child\');

The custom post type is registered and all works fine, but I always get a 404 when trying to access the archive page (archive-custom_name.php).

If I take the inner part of the cpt_init - method and put it into the functions.php like so:

add_action( \'init\', \'create_post_type\' );

    function create_post_type()
    {
        $labels = array(
        \'name\' => __(\'mycpt\', \'synTh_textDomain\'),
        \'singular_name\' => __(\'mycpt\', \'synTh_textDomain\')
        );

        $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'boom\'),
        \'supports\' =>array(\'title\',\'editor\', \'custom-fields\',\'thumbnail\')
        );  

        register_post_type( \'mycpt\', $args );
    }

everything works as expected. There is an archive-boom.php, which I can access via: www.domain.com/boom/

I flushed the permalinks settings and I have them set to postname.

Is there a difference, when attaching to a hook from inside a class??

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

您只在管理请求上创建类的实例,因此您的CPT在前端请求上不存在。而不是:

if( is_admin() )
    $cpt = new CPT(\'cpt_child\');
在所有请求上创建实例:

$cpt = new CPT(\'cpt_child\');

相关推荐

从unctions.php调用的Add_Actions未返回好值

我正试图通过这些功能为我的网站添加一些安全/访问防护。php。然而,每当我尝试通过函数添加时。php(而不是作为插件,我以前做过)失败(总是返回false)。例如:add_action(\"parse_query\", checkaccess()); // in functions.php 以及function checkaccess() { $allowAccess = false; if(is_admin()||is_front_page()||i