用于自定义帖子类型的自定义单页,wordpress

问题描述 投票:0回答:5

我刚刚弄清楚如何制作自定义帖子类型并将它们提供到页面上,我想知道是否可以更改它们的单个帖子页面?

我可以只设置一个

single-news.php
吗?如果是这样,该 PHP 文件将如何格式化以及 WordPress 如何知道使用该文件
single-news.php file
?我只是想让他们吐出完整的文章。

谢谢!!

wordpress posts
5个回答
25
投票

创建 CPT 后,执行以下操作以显示 CPT 的单个帖子:

  • 复制模板中的
    single.php
    文件并将其重命名为
    single-{post_type}.php
    (例如
    single-movie.php
  • 从 WordPress 刷新永久链接

您可以从这篇文章

获得更多详细信息
  • 现在如果你想显示 CPT 列表,你可以使用 get_posts() 和 args:

    $args = array(
    ...
    'post_type' => 'movie'
    )


2
投票
Custom Post Type in wordpress. Basic four steps 

Step 1 : File Path location : theme/function.php in your theme

  Paste code in function.php (register custom post type )

  <?php

add_action( 'init', 'custom_post_type_func' );
function custom_post_type_func() {
    //posttypename = services
$labels = array(
'name' => _x( 'Services', 'services' ),
'singular_name' => _x( 'services', 'services' ),
'add_new' => _x( 'Add New', 'services' ),
'add_new_item' => _x( 'Add New services', 'services' ),
'edit_item' => _x( 'Edit services', 'services' ),
'new_item' => _x( 'New services', 'services' ),
'view_item' => _x( 'View services', 'services' ),
'search_items' => _x( 'Search services', 'services' ),
'not_found' => _x( 'No services found', 'services' ),
'not_found_in_trash' => _x( 'No services found in Trash', 'services' ),
'parent_item_colon' => _x( 'Parent services:', 'services' ),
'menu_name' => _x( 'Services', 'services' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Hi, this is my custom post type.',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'services', $args );
}
?>

第2步:如何在wordpress模板页面中显示wordpress自定义帖子类型?

  : you can show anywhere in template page like this :



<?php   $args = array( 'post_type' => 'services', 'posts_per_page' => 20 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); ?>
            <div class="services-items">
            <?php the_title(); 
        if ( has_post_thumbnail( $post->ID ) ) {
        echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
        echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
        echo '</a>'; }

?>
            </div>
    <?php endwhile; ?>

第 3 步:创建新模板来显示单个帖子,如下所示

single-{自定义帖子类型名称}.php 或者 单服务.php

第 4 步:将代码粘贴到 single-services.php 文件中

 <?php /* The loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <div class="main-post-div">
                <div class="single-page-post-heading">
                <h1><?php the_title(); ?></h1>
                </div>
                <div class="content-here">
                <?php  the_content();  ?>
                </div>
                <div class="comment-section-here"
                <?php //comments_template(); ?>
                </div>
                </div>

            <?php endwhile; ?>

这是具有单个帖子页面的自定义帖子类型示例。


0
投票

为此,您可以为 single-news.php 页面创建模板文件。 并通过 WordPress 查询获取您想要的帖子。 例如您的 single-news.php 页面

<?php
//Template Name: Single News
?>

<?php
//get your content
$args = array('category' =>'9','posts_per_page'=> 3);
                        $myposts = get_posts( $args );
                        global $post;
                        foreach ( $myposts as $post ) : setup_postdata( $post ); ?> 

//go with your content

0
投票
Here are some urls u can idea about the creating custom post templates.

https://codex.wordpress.org/Post_Type_Templates
http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
https://codex.wordpress.org/Post_Type_Templates

With the help of above urls u can get idea how you can create new templates for custom post types and custom taxonomy. I think this will help you.

0
投票

这对我来说真的非常有帮助,我不得不为德里最好的离婚律师创建一个特殊的帖子页面。但是这个页面帮助我重新编码并最终完成了。

感谢您提供此类信息。

© www.soinside.com 2019 - 2024. All rights reserved.