我有一个自定义的帖子类型“people”。对于每个人,我可以输入以下自定义字段:“地址”、“职业”和“图片”。
在单身人群中。php我显示地址和职业。此外,这个单一模板包含一个图库按钮-当网站用户单击它时,我想将用户带到一个单独的页面,其中只显示他刚才看到的人的照片。
我如何做到这一点?我不能只创作第二首单曲。php文件,对吗?
我有一个自定义的帖子类型“people”。对于每个人,我可以输入以下自定义字段:“地址”、“职业”和“图片”。
在单身人群中。php我显示地址和职业。此外,这个单一模板包含一个图库按钮-当网站用户单击它时,我想将用户带到一个单独的页面,其中只显示他刚才看到的人的照片。
我如何做到这一点?我不能只创作第二首单曲。php文件,对吗?
我是页面模板的粉丝-下面是我要做的。
首先创建一个名为Person Gallery
, 随便你叫什么都行。
然后我们可以创建一个单独的模板page-person-gallery.php
或者打开我们的page.php
并在循环中运行一个条件,以测试我们是否正在查看Person Gallery页面:
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php if($post->post_name == \'person-gallery\') : /** Better to test for ID **/ ?>
<!-- We\'re on the Person Gallery Page -->
<?php else : ?>
<!-- Run default info -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
无论您选择哪种方法,我们现在都可以根据我们对此人的了解构建一个查询,但目前还没有什么。我们需要在您的single.php
这可以为我们提供更多关于此人的信息。因此,当您创建指向person-gallery
我们可以传递我们的个人ID。它将如下所示:<!-- Again, better getting permalink by ID -->
<a href="<?php echo get_permalink( get_page_by_path( \'person-gallery\' ) ).\'?person=\'.$post->ID ?>">View Gallery</a>
现在,当有人单击该链接时,它将向我们传递该个人ID,然后我们可以根据该ID进行查询。让我们创建并获取我们的图像:<?php
if(isset($_GET[\'person\']) && is_numeric($_GET[\'person\'])) {
$pesonID = $_GET[\'person\'];
$attachments = get_children(
array(
\'numberposts\' => -1,
\'post_mime_type\' => \'image\',
\'post_parent\' => $personID,
\'post_status\' => \'any\',
\'post_type\' => \'attachment\'
)
);
/** Now we can display our images! **/
$attachments = get_children(
array(
\'numberposts\' => -1,
\'post_mime_type\' => \'image\',
\'post_parent\' => $post->ID,
\'post_status\' => \'any\',
\'post_type\' => \'attachment\'
)
);
/** Now we can display our images! **/
foreach($attachments as $imgID => $img){
echo wp_get_attachment_image($imgID);
}
}
else{
echo "<p>No Images Found.</p>";
}
?>
您可以将一些尺寸等传递给wp_get_attachment_image
, View The Codex 查看返回的内容。您可能会使用同一个页面。php。页眉、页脚和条件提要栏相同,只是中间内容不同。
if(is_singular( \'custom_post_title\' ) ){
// do your conditional stuff
}else if(is_singular( \'custom_post_title_2\' )){
// do your conditional stuff 2
}else{
//do other else stuff
}
我已经设置了一个名为cpt\\u top\\u ten\\u list的自定义帖子类型。我正在尝试设置一个页面来显示所有自定义帖子类型。我把我的新页面命名为前十页。并在页面顶部添加了一个模板标记。在仪表板中创建了一个名为“前十名列表”的页面,并将该页面分配给了我的新模板。现在,当我转到页面时,它似乎会将我重定向到自定义帖子类型的归档页面。如果我关闭cpt的存档功能,它会将我重定向到第页。php。如果我添加flush_rewrite_rules(); 转换为函数。php,我的新自定义页面可以工作,但其他页面