I am building a single page theme, And I am having a problem with get_post_meta()
This code bellow does not work, and I cannot see anything on the website as it's blank. Here is my index.php
.
<?php get_header(); ?>
<?php
global $query_string, $post;
query_posts($query_string . "post_type=page&post_status=publish&posts_per_page=9");
if ( have_posts() ) : while ( have_posts() ) : the_post();
$home = get_post_meta($post->ID, 'home', true);
$about = get_post_meta($post->ID, 'about', true);
$contact = get_post_meta($post->ID, 'contact', true);
$services = get_post_meta($post->ID, 'services', true);
?>
<?php if ( $home ) : ?>
<div id="home" class="borderline">
<?php the_content(); ?>
</div>
<?php endif; ?>
<?php if ( $about ) : ?>
<section class="about-us" id="about-us">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
The code above does not display anything.
Thank you in advance.
'Ace'
I am building a single page theme, And I am having a problem with get_post_meta()
This code bellow does not work, and I cannot see anything on the website as it's blank. Here is my index.php
.
<?php get_header(); ?>
<?php
global $query_string, $post;
query_posts($query_string . "post_type=page&post_status=publish&posts_per_page=9");
if ( have_posts() ) : while ( have_posts() ) : the_post();
$home = get_post_meta($post->ID, 'home', true);
$about = get_post_meta($post->ID, 'about', true);
$contact = get_post_meta($post->ID, 'contact', true);
$services = get_post_meta($post->ID, 'services', true);
?>
<?php if ( $home ) : ?>
<div id="home" class="borderline">
<?php the_content(); ?>
</div>
<?php endif; ?>
<?php if ( $about ) : ?>
<section class="about-us" id="about-us">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php the_content(); ?>
</div>
</div>
</div>
</section>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
The code above does not display anything.
Thank you in advance.
'Ace'
Unless you have generated metafields called 'home', 'about', 'contact' and 'services' in functions.php
using add_post_meta
your calls to get_post_meta
will always return false. So if you test if they are true later on, the answer is 'no' and nothing will be displayed.
WP_Query()
? wordpress.stackexchange.com/questions/1753/… – Michael Commented Jul 14, 2016 at 3:28