0

我需要展示多种风格的帖子;新闻 1 和新闻 2。我遇到的问题是我需要以不同的样式显示每个帖子。IE 对于 News1 中的帖子,他们需要有日期和共享按钮,但对于 News2,我不需要这个。

我有一个自定义字段设置为名为“存档类别”的 News2 部分,并且可以为此使用 get_post_meta。

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

如果有人可以提供帮助,将不胜感激。

谢谢

4

1 回答 1

0

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

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

在模板部分文件中,以不同方式布局单个帖子/页面。

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

伪代码将是(在 single.php 或 single-post.php 或 single-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
?>
于 2013-05-17T14:52:03.603 回答