admin管理员组文章数量:1410674
I need to add a different custom class to each div inside my wordpress loop. I've read about post_class()
function and filter, but I'm not sure if it's a good approach to do this. For now I have this code, and I'm using the get_post_meta()
to get the custom field I set with the class I want to display, but it will work only for the second post displayed. Is there a fix or I need to use the post_class()
instead?
<?php $scrollable = new WP_Query( ['post_type' => 'home-slider', 'posts_per_page' => 3] ); ?>
<?php if( $scrollable->have_posts() ): while( $scrollable->have_posts() ): $scrollable->the_post(); ?>
<?php $class = get_post_meta($post->ID,'class',true); ?>
<div class="col-sm-12 col-md-12 col-lg-12 img-<?php echo $class; ?>"> <!-- For each div of the loop I need to assign the img-top ecc... custom class -->
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 img-text">
<h1 class="">Hello</h1>
<p class="lead">Nice to meet you.</p>
</div>
</div>
<img class="img-fluid bg-img" src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
I need to add a different custom class to each div inside my wordpress loop. I've read about post_class()
function and filter, but I'm not sure if it's a good approach to do this. For now I have this code, and I'm using the get_post_meta()
to get the custom field I set with the class I want to display, but it will work only for the second post displayed. Is there a fix or I need to use the post_class()
instead?
<?php $scrollable = new WP_Query( ['post_type' => 'home-slider', 'posts_per_page' => 3] ); ?>
<?php if( $scrollable->have_posts() ): while( $scrollable->have_posts() ): $scrollable->the_post(); ?>
<?php $class = get_post_meta($post->ID,'class',true); ?>
<div class="col-sm-12 col-md-12 col-lg-12 img-<?php echo $class; ?>"> <!-- For each div of the loop I need to assign the img-top ecc... custom class -->
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 img-text">
<h1 class="">Hello</h1>
<p class="lead">Nice to meet you.</p>
</div>
</div>
<img class="img-fluid bg-img" src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Share
Improve this question
edited Nov 16, 2019 at 16:56
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Nov 16, 2019 at 16:03
sialfasialfa
32910 silver badges29 bronze badges
2
- Try to use get_the_ID() instead of $post->ID in the get_post_meta function within a loop. – Pradipta Sarkar Commented Nov 16, 2019 at 18:13
- @PradiptaSarkar already tried, the result is the same. – sialfa Commented Nov 17, 2019 at 10:00
1 Answer
Reset to default -1In that case try to create custom sql query like this.
global $wpdb;
$query = "SELECT meta_value FROM wp_postmeta WHERE post_id=your post id AND meta_key='class';
$results = $wpdb->get_row($query) ; echo $results->meta_value;
本文标签: getpostmeta() not work for the first post inside the loop
版权声明:本文标题:get_post_meta() not work for the first post inside the loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744997652a2636774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论