theme development - Display content on Single page

admin2025-01-08  6

I am building a single page theme, And I am having a problem with get_post_meta()

The Problem

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.

Question

  • How can I display my content using the code above?
  • Guidance on developing single page themes, going forward?
  • What page template hierarchy should I put my code?

My Pages look like this

Thank you in advance.

'Ace'

I am building a single page theme, And I am having a problem with get_post_meta()

The Problem

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.

Question

  • How can I display my content using the code above?
  • Guidance on developing single page themes, going forward?
  • What page template hierarchy should I put my code?

My Pages look like this

Thank you in advance.

'Ace'

Share Improve this question asked Jul 13, 2016 at 16:21 AneleAnele 14 bronze badges 2
  • you are missing the '&' just before 'post_type'; developer.wordpress.org/reference/functions/query_posts/… - also, why are you not using a custom query based on WP_Query() ? wordpress.stackexchange.com/questions/1753/… – Michael Commented Jul 14, 2016 at 3:28
  • I don't understand. Do you mind showing an example? – Anele Commented Jul 15, 2016 at 6:51
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267781a1221.html

最新回复(0)