Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress |自定义帖子|显示内容_Wordpress_Wordpress Theming_Custom Post Type - Fatal编程技术网

Wordpress |自定义帖子|显示内容

Wordpress |自定义帖子|显示内容,wordpress,wordpress-theming,custom-post-type,Wordpress,Wordpress Theming,Custom Post Type,我正在尝试创建一个Wordpress主题,它需要一个定制的帖子类型 我已经通过functions.php创建了post类型。这似乎已经工作,并显示在管理领域,并允许我生成帖子。代码如下: // Register Custom Post Type function courses_post_type() { $labels = array( 'name' => _x( 'Courses', 'Post Type General Nam

我正在尝试创建一个Wordpress主题,它需要一个定制的帖子类型

我已经通过functions.php创建了post类型。这似乎已经工作,并显示在管理领域,并允许我生成帖子。代码如下:

// Register Custom Post Type
function courses_post_type() {
    $labels = array(
        'name'                  => _x( 'Courses', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'Course', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'Training Courses', 'text_domain' ),
        'name_admin_bar'        => __( 'Course Type', 'text_domain' ),
        'archives'              => __( 'Course Archives', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Course:', 'text_domain' ),
        'all_items'             => __( 'All Courses', 'text_domain' ),
        'add_new_item'          => __( 'Add New Course', 'text_domain' ),
        'add_new'               => __( 'New Course', 'text_domain' ),
        'new_item'              => __( 'New Course', 'text_domain' ),
        'edit_item'             => __( 'Edit Course', 'text_domain' ),
        'update_item'           => __( 'Update Course', 'text_domain' ),
        'view_item'             => __( 'View Course', 'text_domain' ),
        'search_items'          => __( 'Search Courses', 'text_domain' ),
        'not_found'             => __( 'No courses found', 'text_domain' ),
        'not_found_in_trash'    => __( 'No courses found in Trash', 'text_domain' ),
        'featured_image'        => __( 'Featured Image', 'text_domain' ),
        'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
        'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
        'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
        'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
        'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $rewrite = array(
        'slug'                  => 'post_type',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
    );
    $args = array(
        'label'                 => __( 'Course', 'text_domain' ),
        'description'           => __( 'Training Course information pages', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => true,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-welcome-learn-more',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,       
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'query_var'             => 'course',
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
    );
    register_post_type( 'courses', $args );

}
add_action( 'init', 'courses_post_type', 0 );
我的问题是如何从帖子中获取内容以显示在自定义主题中?

我目前陷入困境,遇到错误。这就是我的目标:

我已经创建了一个名为single course.php的模板,目前没有更改任何与我的主模板不同的内容

但是,当我在浏览器中查看帖子时,会收到错误消息:

对不起,没有帖子符合您的标准。

在single-course.php中,我将下面的代码放在我希望显示内容的

如果它有助于可视化,那么我试图实现的总体目标是自定义帖子类型(在我的例子中,每个帖子都是一个培训课程)和显示每个自定义帖子的页面。同样,它也可以是shop,custom post是一个产品,在这个产品中,我的single-course.php应该在一个div中显示特定的产品信息

// This div shows the post title using a wordpress php function
<div class="page-title bg-alpha-moderateblue">
<h1 style="margin:0px;"><?php echo get_the_title(); ?></h1>
</div>

//This div should show the post or in my case the training course overview
//From my understading I have to use the loop although I only want to SHOW THE CONTENT OF A PARTICULAR CUSTOM (course) POST.
<div class="page-content bg-white">
<?php 

//Define your custom post type name in the arguments

$args = array('post_type' => 'courses');

//Define the loop based on arguments

$loop = new WP_Query( $args );

//Display the contents

while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;?>
</div>
//此div使用wordpress php函数显示文章标题
//该部门应显示职位或培训课程概述(以我为例)
//据我所知,我必须使用循环,尽管我只想显示特定自定义(课程)帖子的内容。

非常感谢所有建设性的反馈和建议,提前感谢

我不确定是您自己编写的所有代码(来自functions.php文件)还是您从其他地方获得的代码,但我建议在使用示例代码时尽量少定制。您可能还想使用Wordpress'Twenty16 single template()中的代码毕竟,谁比Wordpress更了解Wordpress呢?我的意思并不是说在生产中使用代码作为起点来帮助您消除错误出现在这两个代码块中的可能性

也就是说,您是否尝试过将single-courses.php重命名为single-courses.php?毕竟,这是您在register\u post\u type调用中指定的名称


希望有帮助,祝你好运

使用代码生成器时,问题已解决,我未能将默认的“slug”=>“post_type”更改为我的自定义帖子类型名称,在我的示例中为“courses”。

如果您使用的是single-courses.php,则不需要使用

<?php 

//Define your custom post type name in the arguments

$args = array('post_type' => 'courses');

//Define the loop based on arguments

$loop = new WP_Query( $args );

//Display the contents

while ( $loop->have_posts() ) : $loop->the_post();
?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;?>
</div>

如果主题模板命名正确,您应该能够使用标准的wordpress循环和标记来显示您的内容

也就是说,不需要再打电话询问

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

//Content

<?php endwhile; endif; ?>

//内容
当然,除非您修改查询以获取单个查询中的相关内容

在这种情况下,我建议使用get_posts();用一个简单的foreach

$args = array(
  'post_type' = 'courses',
  'posts_per_page' = -1,
);

<?php $related_posts = get_posts($args);

if ( $related_posts ) : ?>

<?php $temp_post = $post; //temporarily store the $post query for use after the related content
foreach ( $related_posts as $post ) : setup_postdata( $post ); ?>

  <h3><?php the_title(); ?></h3>

  <?php the_content(); ?>

<?php endforeach;
$post = $temp_post; //set the post back to the main query 
endif; ?>
$args=array(
“post_type”=“courses”,
“每页帖子”=-1,
);

我可能已关闭,但您是否尝试过转到“设置”>“永久链接”,然后单击“保存”按钮?有时这会修复重定向问题。此外,如果模板命名正确,则不需要自定义循环。默认循环应该知道该做什么。