我剛剛想出如何使自定義帖子類型,並將它們送出到頁面上,我想知道如果我可以更改他們的單個帖子頁面?例如,我可以設置一個「single-news.php」嗎?如果是這樣,該PHP文件將如何格式化,以及wordpress如何知道如何使用該single-news.php文件?我只是想讓他們吐出整篇文章。自定義單頁爲自定義帖子類型,wordpress
謝謝!
我剛剛想出如何使自定義帖子類型,並將它們送出到頁面上,我想知道如果我可以更改他們的單個帖子頁面?例如,我可以設置一個「single-news.php」嗎?如果是這樣,該PHP文件將如何格式化,以及wordpress如何知道如何使用該single-news.php文件?我只是想讓他們吐出整篇文章。自定義單頁爲自定義帖子類型,wordpress
謝謝!
您創建的CPT之後,顯示您的CPT的單個職位做到這一點:
single.php
文件在你的模板和將其重命名爲 single-{post_type}.php
(例如,single-movie.php
)您可以從this post
得到更多的細節現在,如果你想顯示CPT的列表,你可以使用get_posts()與ARGS:
$args = array( ... 'post_type' => 'movie' )
爲此,您可以爲您的single-news.php頁面創建模板文件。 ,並通過wordpress查詢獲取您的文章。 例如您的單頁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
這太棒了!非常感謝。原諒我的不負責任,但是在單個帖子頁面(讓我們來看這個例子中的新聞)只是吐出標題和內容而已? – ajmajmajma
閱讀此,https://codex.wordpress.org/Post_Type_Templates – Dinesh
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.
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步:創建.PHP 或 單services.php的節目單後這樣
單{自定義後類型名稱}新模板
第4步:粘貼代碼in 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; ?>
這是自定義帖子類型的示例與單個帖子頁。
只是在這裏添加一個筆記。如果您創建了新的CPT,但沒有看到使用正確命名的模板(包括'single.php'或'single-whatever.php')的結果 - 刷新您的PERMALINKS。只需進入WP-admin的永久鏈接頁面,然後點擊「保存」即可。這解決了問題9/10次。 – staypuftman