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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题