WordPress.org

Forums

[resolved] Wrong Title Showing on Blog Page (3 posts)

  1. Timothy Jacobs
    Member
    Posted 2 years ago #

    Hi All,

    First time coding site from "scratch" in wp, and I think I screwed up the index.php page. The title that is shown is always the titile of the latest post, not the title set on the page. Code below:

    <?php get_header(); ?>
    
    <h1><?php the_title(); ?></h1>
        <div class="content">
    
        <?php if (have_posts()) : while(have_posts()) : the_post(); ?>
            <div <?php post_class('row'); ?> id="post-<?php the_ID(); ?>">
                <aside class="span3 blog-meta">
                    <?php if (has_post_thumbnail()) : ?>
                        <div class="blog-featured"><?php the_post_thumbnail(); ?></div>
                    <?php endif; ?>
                    <div class="meta hidden-phone">
                        <div class="time">Published <span class="no-wrap"><?php the_time(get_option('date_format')); ?></span></div>
                        <div class="author">By <?php the_author_posts_link(); ?></div>
                        <div class="category">Filed under <?php the_category('&nbsp;/&nbsp;'); ?></div>
                        <?php if (has_tag()) : ?><div class="tags">Tags: <?php the_tags('&nbsp;'); ?></div><?php endif; ?>
                    </div>
                </aside>
                <article class="span9 blog-content">
                    <h3 class="blog-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="meta-mobile visible-phone">
                        <div class="time">Published <span class="no-wrap"><?php the_time(get_option('date_format')); ?></span></div>
                        <div class="author">By <a class="light-links" href="<?php echo get_the_author_link(); ?>"><?php the_author(); ?></a></div>
                    </div>
                    <?php the_content('Read More...'); ?>
                </article>
            </div>
    
            <hr class="featurette-divider">
        <?php endwhile; ?>
            <div class="bottom-navigation">
                <span class="left-arrow"><?php next_posts_link('&larr; Older Posts'); ?></span>
                <span class="right-arrow"><?php previous_posts_link('Newer Posts &rarr;'); ?></span>
            </div>
            <?php else : ?>
            <h1>No Posts</h1>
        <?php endif; ?>
           </div>
    
    <?php get_footer(); ?>

    Thanks so much,

    Tim

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    the_title() should not be used outside of the loop; the result can be random.
    it usually seems to be ok if the template is only for one static page or post; however in multipost templates, it will show one of the ost titles from the loop (possibly either the last or first - haven't checked that).

    the 'posts page' actually uses index.php or home.php as the template to display the page, so the page title is not easily available for display.

    to get a reliable output of the page title of a page which is set as 'posts page' under dashboard - settings - reading, you will need to use this kind of code instead of

    <h1><?php the_title(); ?></h1>

    example:

    <?php if( is_home() && get_option('page_for_posts') ) { ?>
    <h1><?php echo apply_filters('the_title',get_page( get_option('page_for_posts') )->post_title); ?></h1>
    <?php } elseif( is_singular() ) { ?>
    <h1><?php the_title(); ?></h1>
    <?php } ?>

    depending on your design ideas and what templates are in your theme, you might need to expand this to show various titles for archives and search pages....

  3. Timothy Jacobs
    Member
    Posted 2 years ago #

    Ahh, I see. Thanks alchymyth, that worked great!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags