单个帖子ID的自定义帖子类型条件语句

时间:2017-07-03 作者:Mr Jonny Wood

我试图在循环中的自定义帖子类型中为特定帖子输出不同的文本。这是循环:

$args=array(
    \'post_type\'     => \'products\',
    \'post_status\'   => \'publish\',
    \'post_parent\'   => 0,
    \'order\'         => \'ASC\',
    \'orderby\'       => \'menu_order\'
);
$query_parent_products = new WP_Query($args);
因此,我已经在custom post type类别中,现在需要使用条件语句将自定义post的特定ID作为目标,例如:

if ($post->ID == 116) {
    _e (\'my ID is 116\', \'theme_domain\');
} else {
    the_title();
} 
我尝试了几种不同的尝试,但都没能成功。

更新:这是我如何设置cpt的:

add_action( \'init\', \'products_post_type\');
function products_post_type() {
    $labels = array(
        \'name\'                  => \'Products\',                              // This is the Title of the Group
        \'singular_name\'         => \'Product\',                               // This is the individual type
        \'add_new\'               => \'Add New\',                                   // The add new menu item
        \'add_new_item\'          => \'Add New Product\',                       // Add New Display Title
        \'edit\'                  => \'Edit\',                                      // Edit Dialog
        \'edit_item\'             => \'Edit Products\',                             // Edit Display Title
        \'new_item\'              => \'New Product\',                           // New Display Title
        \'view_item\'             => \'View Product\',                          // View Display Title
        \'search_items\'          => \'Search Products\',                       // Search Custom Type Title
        \'not_found\'             => \'No products yet, why not create some?\',     // This displays if there are no entries yet
        \'not_found_in_trash\'    => \'No products found in Trash\',                // This displays if there is nothing in the trash
        \'parent_item_colon\'     => \'\'
    );
    register_post_type( \'products\', array(
        \'labels\'                => $labels,
        \'public\'                => true,
        \'publicly_queryable\'    => true,
        \'exclude_from_search\'   => false,
        \'show_ui\'               => true,
        \'menu_position\'         => 20,                                          // this is what order you want it to appear in on the left hand side menu
        \'menu_icon\'             => \'dashicons-index-card\',                      // the icon for the custom post type menu. uses built-in dashicons (CSS class name)
        \'query_var\'             => true,
        \'rewrite\'               => array(
            \'with_front\' => false,
            \'slug\' => \'products\'                                                 // you can specify its url slug
        ),
        \'capability_type\'       => \'page\',
        \'has_archive\'           => true,
        \'hierarchical\'          => true,
        \'taxonomies\'            => array(\'post_tag\'),
        \'supports\'              => array( \'title\', \'editor\', \'excerpt\', \'thumbnail\', \'custom-fields\', \'revisions\', \'page-attributes\')
    ));

}

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

我假设您正在以通常的方式循环查询,如果是这样,请使用以下代码

if ( $query_parent_products->have_posts() ) :
    while ( $query_parent_products->have_posts() ) {
        $query_parent_products->the_post();

        // your post specific logic here
        if ( get_the_ID() == 116 ) {
            _e (\'my ID is 116\', \'theme_domain\');
        } else {
            the_title();
        } 
    }
endif;

结束

相关推荐