1

I am using this code to display a post on my homepage. <?php the_title(); ?> is returning blank.

Source: idolizeonline.com (NSFW link)

-- If you hover over the thumbnails, the title and excerpt of the post should appear over the image (excerpt is title is not).

<?php if(!of_get_option('ttrust_open_project_single')) : ?>

    <div class="project small ajx <?php echo $p; ?>" id="project-<?php echo $post->post_name;?>">
        <a href="<?php the_permalink() ?>" rel="bookmark" ></a>

        <a href="#<?php echo $post->post_name; ?>" ><?php the_post_thumbnail($project_thumb_size, array('class' => 'thumb', 'alt' => ''.get_the_title().'', 'title' => ''.get_the_title().'')); ?></a>
        <span class="title">
            <div>
                <span id="theTitle"><?php the_title(); ?></span>
                <span id="theExcerpt"><?php the_excerpt(); ?></span>
            </div>
        </span>
    </div>

<?php else: ?>

    <div class="project small <?php echo $p; ?>" id="project-<?php echo $post->post_name;?>">
        <a href="<?php the_permalink() ?>" rel="bookmark" ></a>

        <a href="<?php the_permalink() ?>" ><?php the_post_thumbnail($project_thumb_size, array('class' => 'thumb', 'alt' => ''.get_the_title().'', 'title' => ''.get_the_title().'')); ?></a>
        <span class="title">
            <div>
                <span id="theTitle"><?php the_title(); ?></span>
                <span id="theExcerpt"><?php the_excerpt() ?></span>
            </div>
        </span>
    </div>
<?php endif; ?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59

5 Answers5

3

Put the following code at the top of your file:

<?php if( have_posts() ) the_post(); ?>

See: The Loop

3

I am still unsure what exactly was the issue, but replacing the_title(); with <?php echo $post->post_title; ?> worked fine.

Eric Goncalves
  • 5,253
  • 4
  • 35
  • 59
0

Check your page.php file. Do you have all these things included in that file?

global $post;global $wpdb;
// Your header goes here

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="your_main_div_to_display_content">
// Your title goes here
<?php the_title();?>
</div>
<?php endwhile; endif; ?>

// Your footer goes here
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
0

I had same issue, and in my case I was using $post for another purpose, which was conflicting and causing the_title() to display blank.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
papoola
  • 143
  • 1
  • 7
0

I recently ran into this issue while developing a WordPress theme. WordPress has some declared variables being declared globally, like $post, $posts, Post and so on, if you are coding a WordPress plugin or theme, you need to make sure you treat those variable names like a reserved word. This is one of the errors you can encounter if you don't.

Aderemi Dayo
  • 705
  • 1
  • 11
  • 25