dpd2349 2015-05-11 18:38
浏览 93
已采纳

Wordpress基于自定义字段引入template_part

I have a Wordpress loop on my index.php which brings in a specific template-part.

<?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 has an article with the blog post information (title, featured image, etc) with specific html.

I have a custom-field, ExtraCSS, applied to each post through the content.php with values 'post-right' or 'post-left' (so I can change css of individual posts).

content.php code

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

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

...

I want to have 2 different template_parts which can get called with the loop depending on the custom-field or value applied to the post.

EX. so if the post has the ExtraCSS custom-field value 'post-right' applied to it, the loop will bring in

get_template_part( 'content' , 'right' );

if the post has the ExtraCSS custom-field value 'post-left' it will bring in

get_template_part( 'content' , 'left' );

This may not be the right way to do it, and I'm open to other suggestions, but thats the overall idea. Wanting two different post-templates for one loop There will be numerous posts that have either one, and I want them all brought in one after the other.

  • 写回答

1条回答 默认 最新

  • dpoppu4300 2015-05-11 21:06
    关注

    As you said it's either one or the other template:

    <?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' );   
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题