Piklist入门教程四:自定义Post Statuses

自定义Post Statuses是WordPress创建任何Web应用的核心,打个比方在教程三里我们自定义了一个Product我们可以在这个Post type下创建不同的产品信息(Post Statuses)如estimate、complete、repair等等状态。Piklist很容易的实现它们,不再是WordPress默认的draft,pending review 和 published。你可以添加你想要的状态让自己选择。

重要的提示

  •  由于WordPress自身原因,当新建一个post时会保持”draft”状态。你可以重命名这个显示如”New” “Howdy”。注意是显示,它依然是”draft”。
  •  一些主题可能不能显示非published状态的post,所以你要更改一下你的主题的循环输出显示它。WP_Query class
  •  如果你使用这个status参数时,它不会在WordPress默认的三个状态上增加,它们将会被移除,可以重命名它来使用默认的状态。

本教程将自定义post status到自定义post type上:

  1. 首先,我们在教程三的代码基础上插入代码
  2. 我们将用数组形式来写进post status (详细查阅《Piklist中文手册》自定义post type参数。

下面代码把默认的”draft”重命名为”New”并添加了两个状态:

add_filter('piklist_post_types', 'product_post_type');
function product_post_type($post_types)
{
    $post_types['product'] = array(
    'labels' => piklist('post_type_labels', 'Product')
    ,'taxonomies' => array('post_tag')
    ,'public' => true
    ,'rewrite' => array(
            'slug' => 'product'
        )
    ,'supports' => array(
            'title'
            ,'author'
            ,'revisions'
            ,'editor'
            ,'thumbnail'
        )
    ,'title' => 'Enter Product Title'
    ,'edit_columns' => array(
         'title' => __('Product Name')
        ,'author' => __('operator')
        )
    ,'hide_meta_box' => array(
        'slug'
        ,'author'
        ,'revisions'
        ,'comments'
        ,'commentstatus'
        )
    ,'status' => array(
        'draft' => array(
            'label' => 'New'
        )
        ,'estimate' => array(
            'label' => 'Estimate Product'
        )
        ,'complete' => array(
            'label' => 'Complete Product'
        )
        ,'repair' => array(
            'label' => 'repair Product'
        )
        )
    );
    return $post_types;
}

效果如下图:

post_status2

我们要取出自定义Post status的posts集,我们可以使用标准的WP_Query class,用它的”post_status”参数。如果你想在你的主题里显示自定义post type “product”下的任意状态的post,可以参照下面代码:(记得添加几篇不同状态的post)

$query = new WP_Query(
    array(
        'post_type' => array( 'product')
        ,'post_status' => array( 'estimate', 'complete','repair' ) ) );
var_dump($query->posts);

你刷新你的主题类似下面结果:

wp_query_post

 

我们再新建一篇默认为”New”的post ,如下图所示,status并不是你所见的New而是被重命名的draft:
product_draft

文章分类 Piklist, 经验分享 标签: ,

发表评论


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /var/www/wp/code/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048