【问题标题】:Wordpress conditional post layoutWordpress 条件文章布局
【发布时间】:2013-05-17 14:33:52
【问题描述】:

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

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

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

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

谢谢

【问题讨论】:

    标签: php wordpress conditional conditional-statements


    【解决方案1】:

    在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
    ?>
    

    【讨论】:

    • 顺便说一句,您也可以考虑发布类别而不是自定义字段。 WordPress 能够为一种帖子类型设置多种分类法。
    猜你喜欢
    • 2018-07-25
    • 2019-04-12
    • 1970-01-01
    • 2013-02-11
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多