php - 循环自定义帖子类型

标签 php css wordpress frameworks content-management-system

我是 wordpress 的新手。我想创建一个新的自定义帖子类型,并生成了这段代码,它可以正常工作。

<?
// Register Custom Post Type
function berita_post_type() {

$labels = array(
    'name'                => _x( 'berita-internal', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'berita-internal', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Berita Internal', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
    'all_items'           => __( 'Semua Berita', 'text_domain' ),
    'view_item'           => __( 'Lihat', 'text_domain' ),
    'add_new_item'        => __( 'Tambah', 'text_domain' ),
    'add_new'             => __( 'Tambah Berita', 'text_domain' ),
    'edit_item'           => __( 'Edit Berita', 'text_domain' ),
    'update_item'         => __( 'Update berita', 'text_domain' ),
    'search_items'        => __( 'Search berita', 'text_domain' ),
    'not_found'           => __( 'Not found', 'text_domain' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
);
$args = array(
    'label'               => __( 'berita-internal', 'text_domain' ),
    'description'         => __( 'berita-internal', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', ),
    'taxonomies'          => array( 'category', 'post_tag' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'rewrite'             => true,
    'capability_type'     => 'page',
);
register_post_type( 'internal', $args );

}

// Hook into the 'init' action
add_action( 'init', 'berita_post_type', 0 );

?>

它有效,但我如何显示该自定义帖子类型中的所有帖子? 它就像一篇普通的博文 谢谢

最佳答案

试试这个:创建新文件 berita-Internal.php 上传到当前主题目录

<?php
/**
 * Template Name: Berita Internal
 *
 * @package WordPress
 */

get_header(); ?>

<div id="primary" class="site-content">
    <div id="content" role="main">

        <?php
        $args=array(
          'post_type' => 'berita-internal',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1);

        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          endwhile;
        }
        wp_reset_query();  // Restore global post data stomped by the_post().
        ?>

    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

然后转到 wp-admin 创建新页面(页面->添加新页面)。看右边,

Go go dropdow Template->选择 Berita Internal。然后保存并查看页面。

关于php - 循环自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26010155/

相关文章:

php - 如何在php中与ZK考勤机数据库进行交互?

html - 在 div 上使用 "stand alone" "display: table-cell;"有效;但这是允许的吗?

javascript - 如何在 HTML5 对话框中淡入淡出?

Chrome 中的 Javascript 与 Firefox Mozilla 不兼容

php - 按正/负值和表格中未找到的任何其他帖子排序

javascript - jQuery 和 Wordpress 根据所选页面更改 css

php - 使用正则表达式以任意顺序匹配多个单词

php - 在 Mac OS 上禁用 PHP 邮件?

html - 仅在某些设备中加载 css

php - Kohana 3 中的基本 URL