【发布时间】:2014-05-13 09:46:10
【问题描述】:
我正在使用下面的插件和设置。我在显示我的自定义帖子类型时遇到了一些问题。我专注于打印机自定义帖子类型。我让它们显示在自定义分类法部分下,并创建了 4 个分类法(制造商),这样就可以了。 (http://developmentscene.co/CKH/printers/)
问题
当我点击上面页面上的帖子时,它会显示该类别中的所有帖子。我只是希望它显示您单击的链接的帖子,并且只显示该帖子。我曾尝试使用 ('posts_per_page' => 1,) 来执行此操作,但这段代码只为我在类别页面中单击的每个帖子显示相同的第一篇帖子。
我的理想解决方案
所以我的想法是说只显示一个帖子,其中包含您之前在该类别中单击的链接的 ID。我不知道这是否是最好的方法,但我已将代码放在下面。
<?php get_header(); ?>
<div id="wrap" class="container">
<section id="content" class="primary" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php $printers = new WP_Query(array(
'post_type' => 'printers',
'posts_per_page' => 1,
)); ?>
<?php while($printers->have_posts()) : $printers->the_post(the_ID()); ?>
<h2 class="post-title" style="color:#333;" ><?php the_title(); ?> - <?php echo the_ID();?></h2>
<hr >
<p><?php the_content(); ?></p>
<p> </p>
<?php the_post_thumbnail(); ?>
<br />
</section>
<section id="sidebar" class="secondary clearfix" role="complementary">
<h3 class="widgettitle" ><b>Features and Benefits</b></h3>
<?php $benefits = get_field( "features-and-benefits" );
if($benefits){
echo '<table cellspacing="0" cellpadding="0"><tbody>' ;
foreach($benefits as $benefits){
echo '<tr><td><p>' . $benefits['features'] . '</p></td></tr>' ;
}
echo '</tbody></table>';
}
?>
<br />
<h3 class="widgettitle" ><b>More Information</b></h3>
<?php $specifications = get_field( "specifications" );;
if($specifications){
echo '<table cellspacing="0" cellpadding="0"><tbody>' ;
foreach($specifications as $specifications){
echo '<tr><td><p>' . '<a target="_blank" href="http://' . $specifications['link'] . '/">' . $specifications['details'] . '</a></p></td></tr>' ;
}
echo '</tbody></table>';
}
?>
</section>
<?php endwhile; ?>
</div>
<?php get_footer(); ?>
设置
使用的插件:
* 自定义字段
* 自定义帖子类型 UI
我还将永久链接设置为 postname。
【问题讨论】:
-
你为什么要在页面上运行两个循环
-
您好 Pieter,由于我是 Wordpress 开发的新手,所以我不知道我正在这样做。我为我的愚蠢道歉。您对改进我的代码有什么建议吗?
-
不,你不傻。你不问问题会保持愚蠢:)。我很快就会回家,然后会适当地改进您的代码
标签: php wordpress custom-post-type