1

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?

1
  • You have some encoding problems in that code. Note the squares about 2/3rds down.
    – s_ha_dum
    May 30, 2014 at 13:59

2 Answers 2

1

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(); ?>
0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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