【问题标题】:Wordpress bringing in template_part based on custom fieldWordpress 基于自定义字段引入 template_part
【发布时间】:2015-05-11 18:38:49
【问题描述】:

我的 index.php 上有一个 Wordpress 循环,它引入了一个特定的模板部分。

<?php if ( have_posts() ) : ?>

  <?php /* Start the Loop */ ?>
    <?php while ( have_posts() ) : the_post(); ?>

            <?php
                get_template_part( 'content' );
            ?>

        <?php endwhile; ?>

        <div class="clearfix"></div>

    <?php else : ?>

        <?php get_template_part( 'no-results', 'index' ); ?>

<?php endif; ?>

content.php 有一篇带有特定 html 的博客文章信息(标题、特色图片等)的文章。

我有一个自定义字段,ExtraCSS,通过 content.php 应用于每个帖子,其值为“post-right”或“post-left”(因此我可以更改单个帖子的 css)。

content.php 代码

<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);

<article class="<?php echo $ExtraCSS?>"> 

...

我希望有 2 个不同的 template_parts 可以通过循环调用,具体取决于应用于帖子的自定义字段或值。

前。因此,如果帖子应用了 ExtraCSS 自定义字段值“post-right”,则循环将引入

get_template_part( 'content' , 'right' );

如果帖子具有 ExtraCSS 自定义字段值 'post-left',它将引入

get_template_part( 'content' , 'left' );

这可能不是正确的做法,我愿意接受其他建议,但总体思路就是这样。想要一个循环使用两个不同的帖子模板 会有很多帖子都有一个,我希望它们一个接一个地引入。

【问题讨论】:

    标签: php html css wordpress


    【解决方案1】:

    正如你所说,它要么是一个模板,要么是另一个模板:

    <?php 
    $extraCSS = get_post_meta( get_the_ID(), 'ExtraCSS', true );
    if ( $extraCSS == 'post-right' ) {
        get_template_part( 'content' , 'right' );     
    }
    else {
         get_template_part( 'content' , 'left' );   
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多