我通过简单的排队方式显示自定义添加到我的主页的项目。使用这些代码行
$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(); ?>
答案 0 :(得分:0)
get_the_ID()
使用$post->ID
代替get_post_permalink
。 $post->ID
将获取当前页面的ID,在您的情况下是主页。
答案 1 :(得分:0)
$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();