Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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
Php 无法在wordpress中显示单个自定义帖子类型_Php_Wordpress - Fatal编程技术网

Php 无法在wordpress中显示单个自定义帖子类型

Php 无法在wordpress中显示单个自定义帖子类型,php,wordpress,Php,Wordpress,我通过简单的查询来显示自定义添加到主页上的项目。使用这些代码行 $args = array( 'post_type' => 'recent-projects', 'posts_per_page' => 10 ,'order' => 'ASC'); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); the_title();

我通过简单的查询来显示自定义添加到主页上的项目。使用这些代码行

$args = array( 'post_type' => 'recent-projects', 'posts_per_page' => 10 ,'order' => 'ASC');
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo get_post_permalink( $post->ID, $leavename, $sample );
    endwhile;
当我点击自定义帖子的永久链接时,它会显示索引页面。而我也有archive.php。 下面是functions.php中的代码

register_post_type( 'recent-projects',
// CPT Options
    array(
        'labels' => array(
            'name' => __( 'Recent Projects' ),
            'singular_name' => __( 'Recent Project' )
        ),
        'taxonomies' => array('recordings', 'category', 'whatever'),  //add this....
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'recent-projects'),
        'supports' => array(
        'title',
        'editor',
        'excerpt',
        'trackbacks',
        'custom-fields',
        'comments',
        'revisions',
        'thumbnail',
        'author',
        'page-attributes',)

    )
);
这是名为archive-recent-projects.php的文件中的代码

<?php get_header(); ?>
<div class="content-area">
    <div class="container main_content_wrap">
      <div class="page_wrapper">  

        <section id="site-main" class="site-main content-part" >        
            <div class="blog-post">
                <h1 class="classic-title"><span>Recent Projects</span></h1>
                <br>
                <br>
                <ul>
                <?php
                    $args = array( 'post_type' => 'recent-projects','order' => 'ASC');
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();?>
                       <li>
                        <small><?php the_time('F jS, Y'); ?></small><br>
                        <strong>-</strong>  <?php the_title(); ?><br> <a href="<?php the_permalink(); ?>">Read</a><br><br>
                    </li>

                <?php endwhile; // end of the loop. ?>
            </ul>
            </div>        
        </section>
        </div><!--end .page_wrapper-->
    </div>
</div>
<?php get_footer(); ?>

近期项目



  • -


使用
获取ID()
而不是
$post->ID
来获取
发布永久链接
$post->ID
将获取当前页面的ID,在您的情况下,该页面就是主页。

$recent\u posts=new WP\u Query(数组(
$recent_posts = new WP_Query( array(
'post_type' => 'recent-projects',
'posts_per_page' => 10 ,
'order' => 'ASC'
));
if($recent_posts->have_posts()) :
    while($recent_posts->have_posts()) : $recent_posts->the_post();
        echo '<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
        the_content();
    endwhile;
endif;
wp_reset_postdata();
“post_type”=>“最近的项目”, “每页帖子数”=>10, “订单”=>“ASC” )); 如果($recent_posts->have_posts()): while($recent_posts->have_posts()):$recent_posts->the_post(); 回声'; _内容(); 结束时; endif; wp_reset_postdata();
只是一个旁注。您不需要在
get_post\u permalink
中添加
,$leavename,$sample
,这是输出自定义post类型循环的
?如果是这样,请尝试将其移动到
存档最近的项目.php
模板中的
while循环之外。@WizardCoder我更新了我的存档-recent-projects.php,请再看一次。自从创建自定义帖子类型以来,您是否已进入管理区域的永久链接设置页面并单击保存?我已将此代码发布在archive-recent-projects.php中,但它仍然不起作用。