Return new layout based on get_post_meta
-
I have a home page built where each section is made through a custom post type. Within that post type I created a custom meta box that has a dropdown menu to select the visual layout of that section. The dropdown consists of Main (first section on the homepage), Left (text on left, image on right), and Right (text on right, image on left).
I’m trying to set up my query to be something like this:
If dropdown is set to Main, use this code…
If dropdown is set to Left, use this code…
If dropdown is set to Right, use this code…This is where I’m at right now, but doesn’t work when I test it out.
<?php query_posts( array ('post_type' => 'home')); while ( have_posts() ) : the_post(); ?> <?php $thumb = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?> <?php if (get_post_meta($post->ID, 'layout-type', 'main' )) : ?> <!-- MAIN --> <div class="row home fullScreen" style="background-image: url(<?php echo $thumb; ?>);"> <div class="container"> <div class="col-sm-6 col-sm-offset-6"> <h1><?php the_title(); ?></h1> <p><?php the_content(); ?></p> <p><a href="<?php echo get_post_meta($post->ID, 'button-url', true) ?>" class="btn btn-primary btn-lg"><?php echo get_post_meta($post->ID, 'button-text', true) ?></a></p> </div> </div> <div class="scroll center-block"> <p>Scroll to Continue</p> <img src="<?php bloginfo('template_url'); ?>/images/scroll-arrow.png"> </div> </div> <?php endif; ?> <?php if (get_post_meta($post->ID, 'layout-type', 'left' )) : ?> <!-- LEFT --> <div class="row home fullScreen" style="background-image: url(<?php echo $thumb; ?>);"> <div class="container"> <div class="col-sm-6"> <h1><?php the_title(); ?></h1> <p><?php the_content(); ?></p> <p><a href="<?php echo get_post_meta($post->ID, 'button-url', true) ?>" class="btn btn-primary btn-lg"><?php echo get_post_meta($post->ID, 'button-text', true) ?></a></p> </div> </div> </div> <?php endif; ?> <?php if (get_post_meta($post->ID, 'layout-type', 'right' )) : ?> <!-- RIGHT --> <div class="row home fullScreen" style="background-image: url(<?php echo $thumb; ?>);"> <div class="container"> <div class="col-sm-6 col-sm-offset-6"> <h1><?php the_title(); ?></h1> <p><?php the_content(); ?></p> <p><a href="<?php echo get_post_meta($post->ID, 'button-url', true) ?>" class="btn btn-primary btn-lg"><?php echo get_post_meta($post->ID, 'button-text', true) ?></a></p> </div> </div> </div> <?php endif; ?> <?php endwhile; ?>
-
please do not use
query_posts()
– for why and for alternatives, see http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-postshttp://codex.wordpress.org/Function_Reference/get_post_meta
instead:
<?php if (get_post_meta($post->ID, 'layout-type', 'main' )) : ?>
try:
<?php if ( get_post_meta($post->ID, 'layout-type', true ) == 'main' ) : ?>
etc…
also, try to use the structure ‘if/elseif/else/endif’ – http://php.net/manual/en/language.control-structures.php
This works perfectly! Thank you so much for pointing me in the right direction. I also implemented your suggestions regarding query_posts and if/elseif/else/endif.
- The topic ‘Return new layout based on get_post_meta’ is closed to new replies.