2013-05-17 27 views
0

我需要显示多种类型的帖子; News1和News2。我遇到的问题是我需要以不同风格显示每篇文章。对于News1中的帖子,他们需要有日期和分享按钮,但对于News2我不需要这个。WordPress的条件帖子布局

我有一个自定义字段设置为News2关闭部分称为“档案类别”,并可以使用get_post_meta为此。

我的问题是只应该在帖子文件中的条件语句。

如果有人能帮助它将不胜感激。

感谢

回答

0

在single.php中,获得自定义字段的值,然后根据该值,调用不同的模板部分:

get_template_part('prefix', 'other-parts-of-the-file-except-extension'); 

在模板零件文件,布局一个职位/页不同。

详细说明,实际布局代码应写入模板部件文件。 single.php只检查自定义字段值,并根据该值调用不同的模板部分文件。

伪代码是(在single.php中或单post.php中或单customposttype.php,你用哪个):

<php 
    $value=get the custom field value; // replace this with your function to get the value 
    if($value=='News 1') 
    get_template_part('template', 'news1'); // then you'll put layout in template-news1.php 
    else 
    get_template_part('template', 'news2'); // layout in template-news2.php 
?> 
+0

顺便说一句,你也可以考虑岗位类别,而不是定制的领域。 WordPress可以为一个帖子类型设置多个分类法。 – golddc