首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WP自定义柱式输出

WP自定义柱式输出
EN

Stack Overflow用户
提问于 2016-01-17 19:47:03
回答 2查看 322关注 0票数 0

我决定在管理菜单中创建自定义的post类型。所以,在浏览WP和一些在线论坛后,我设法做到了。

为了完成这样一个小任务,我增加了以下代码:

代码语言:javascript
复制
/**
 * Register a book post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
function codex_book_init() {
    $labels = array(
        'name'               => _x( 'Projects', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Projects', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Projects', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Project', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'project', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Project', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Project', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Project', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Project', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Projects', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Project', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Project:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No projects found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No projects found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_admin_bar'  => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'projects' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_icon'          => 'dashicons-images-alt2',
        'menu_position'      => 5,
        'supports'           => array(
            'title',
            'editor',
            'excerpt',
            'trackbacks',
            'custom-fields',
            'comments',
            'revisions',
            'thumbnail',
            'author',
            'page-attributes')
    );

    register_post_type( 'projects', $args );

}
add_action( 'init', 'codex_book_init' );

function create_book_tax() {
    register_taxonomy(
        'project-types',
        'projects',
        array(
            'label' => __( 'Project Types' ),
            'rewrite' => array( 'slug' => 'project-types' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'create_book_tax' );

我已设法在主页上显示来自自定义post类型的帖子:

代码语言:javascript
复制
 <?php query_posts('post_type=projects');
            while (have_posts()) : the_post();
            ?>
                <?php if ( has_post_thumbnail() ) : ?>
                    <div class="project-module">
                        <a class="project-thumbnail" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                            <?php the_post_thumbnail(); ?>
                                <div class="project-module-title">
                                    <?php the_title(); ?>
                                </div>
                        </a>
                    </div>
                    <?php endif; ?>
                        <?php endwhile; ?>

但有个问题。WP说,“页面找不到”时,我点击帖子查看它。我尝试创建了单项目. and或归档-Projects.php,但没有任何帮助。我在这里错过了什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-17 20:17:38

你需要重建你的重写规则。

在管理中,转到设置、→Permalinks、并按保存更改

Screen

票数 2
EN

Stack Overflow用户

发布于 2016-01-17 19:57:39

根据WordPress代码索引,对于新的定制post类型,您需要手动运行flush_rewrite_rules()一次。

来源:rules

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34842850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档