【问题标题】:WordPress Custom Post Types displayed on a specific location显示在特定位置的 WordPress 自定义帖子类型
【发布时间】:2018-11-28 14:44:04
【问题描述】:

我是 WordPress 新手,所以我对自定义帖子类型有一点问题。 我创建了一个新的自定义帖子类型,如下所示:

function awesome_custom_post_type(){
    $labels = array(
        'name' => 'Top Categorii',
        'singular-name' => 'Top Categorie',
        'add_new' => 'Adauga Categorie',
        'all_items' => 'Toate Categoriile',
        'add_new_item' => 'Adauga Categorie',
        'edit_item' => 'Editeaza Categorie',
        'new_item' => 'Categorie Noua',
        'view_item' => 'Vezi Categorie',
        'search_item' => 'Cauta Categorie',
        'not_found' => 'Categoria nu a fost gasita',
        'not_found_in_trash' => 'Categoria nu a fost gasita in cos',
        'Parent_item_colon' => 'Categorie parinte',
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'revisions',
        ),
        'taxonomies' => array('category', 'post_tag'),
        'menu_position' => 10,
        'exclude_from_search' => true
    );
    register_post_type('top_categorii', $args);
}
add_action('init', 'awesome_custom_post_type');

一切正常,但我想在特定位置显示这种类型的帖子,更准确地说,在普通帖子上方的 page.php 模板中,我该怎么做?有可能吗?

关于这个主题的另一个问题,如果我点击一个自定义帖子,我希望它让我进入显示特定类别的一些正常帖子的页面。 (例如单击类别时显示的页面)。这也可能吗?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    我正在使用此代码在特定页面上显示 CPT(自定义帖子类型),

    <?php
    $query = new WP_Query(array(
    'post_type' => 'slug', // Put here your Custom Post Type Slug/s
    'posts_per_page' => -1, // -1 for displaying all the Posts
    'order' => ASC //ASC for Ascending Order,and USE DESC for Descending Order
    ));
    if ( $query->have_posts() ) : while ($query->have_posts()) : $query->the_post();
    ?>
    
    <h3><?php the_title() ;?></h3>
    <p>
    <?php the_content(); ?>
    </p>
    <?php endwhile;?>
    <?php endif; ?>
    <?php wp_reset_postdata();?> 
    

    希望对您有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多