Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a page for my posts, and a home.php for the structure.

I would like to show the page's name on the top of the posts, but the result is always the latest post.

My code:

<?php get_header(); ?>
<section id="primary-content" class="col-md-9">
    <?php the_title(); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div class="post">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
            <div class="byline">by <?php the_author_posts_link(); ?> on <a href="<?php the_permalink(); ?>"><?php the_time( 'l F d, Y'); ?></a></div>
            <?php the_content('Read More...'); ?>
        </div>
    <?php endwhile; else: ?>
        <p><?php _e('No posts were found. Sorry!'); ?></p>
    <?php endif; ?>

    <div class="navi">
        <div class="right">
        <?php previous_posts_link('Previous'); ?> / <?php
          next_posts_link('Next'); ?>
      </div>
    </div>

</section>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Where's the problem? Is it possible to show the page's name on the blog page?

share|improve this question
    
You have some encoding problems in that code. Note the squares about 2/3rds down. –  s_ha_dum May 30 at 13:59

2 Answers 2

up vote 0 down vote accepted

The call for the page title is outside the loop so you need to use this function instead:

<?php echo get_the_title(); ?> 

Here is the updated code:

<?php get_header(); ?>
<section id="primary-content" class="col-md-9">

    <?php echo get_the_title(); ?> 

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

        <div class="post">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h2>
            <div class="byline">by <?php the_author_posts_link(); ?> on <a href="<?php the_permalink(); ?>"><?php the_time( 'l F d, Y'); ?></a></div>
            <?php the_content('Read More...'); ?>
        </div>
    <?php endwhile; else: ?>
        <p><?php _e('No posts were found. Sorry!'); ?></p>
    <?php endif; ?>

    <div class="navi">
        <div class="right">
        <?php previous_posts_link('Previous'); ?> / <?php
          next_posts_link('Next'); ?>
      </div>
    </div>

</section>

<?php get_sidebar(); ?>

<?php get_footer(); ?>
share|improve this answer

You are using the_title() outside the Loop, which is technically incorrect. It is a "Loop" tag and it depends on the global variable $post which is set to the current post at each iteration of the Loop, but is prepopulated with the first post in the Loop, which is what you are seeing. To get the title of the page itself you need:

$qobj = get_queried_object();
echo $qobj->post_title;

But be aware that get_queried_object() will return different objects, or no objects at all, depending on the page you are on. You need to wrap your code in conditionals to avoid errors.

if (is_singular()) {
    $qobj = get_queried_object();
    echo $qobj->post_title;
}

Or...

$qobj = get_queried_object();
if(isset($qobj->post_title)) {
    echo $qobj->post_title;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.