-1

I'm facing a problem where my title is being displayed multiple times.

Using the following code the page title 'News' is displayed 10 times, the same number as the number of posts I've elected to display.

<?php while ( have_posts() ) : the_post();
    if( is_singular() ) { ?>
        <h1 class="row-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
        <p class="meta">Written By: <?php echo get_the_author() ?><span>&bull;</span><?php the_date(); ?></p>
    <?php } else { ?>
        <h1 class="row-title"><?php wp_title(); ?></h1>
    <?php } endwhile; ?>

However, using this version of code, the latest 10 post titles are displayed instead.

<?php while ( have_posts() ) : the_post();
    if( is_singular() ) { ?>
        <h1 class="row-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
        <p class="meta">Written By: <?php echo get_the_author() ?><span>&bull;</span><?php the_date(); ?></p>
    <?php } else { ?>
        <h1 class="row-title"><?php the_title(); ?></h1>
    <?php } endwhile; ?>

Any help would be greatly appreciated. Thanks.

0

The function wp_title() is supposed to be used to generate the text for the title tag:

<head>
    <title><?php wp_title();?></title>
    ...
</head>

but to display the current post title within the loop, you should instead use the the_title() function.

  • Hi Birgire, thanks for that. I'm aware it should be used in the head section but wanted to illustrate that I can get the page title to display as opposed to just the post titles. However, this still doesn't resolve the problem I'm having of multiple occurrences – estin92 Jul 30 '14 at 10:19
  • Hi @estin92, I assumed from your question that when you used the the_title() function everything worked as expected but not when you tried the wp_title() function, that gave you always the same result. I guess I misunderstood your question then - can you please rephrase the problem, thanks ;-) – birgire Jul 30 '14 at 10:28
  • Hi @Birgire. I'll try and break it down as best I can. On my static posts page I've opted to show 10 recent posts. My problem is that the title for my page is being displayed 10 times also but with the individual values of each post for that page, archive or category using the second part of code detailed in my original post. – estin92 Jul 30 '14 at 10:46
  • @estin Please add all that info directly to your question in the form of an edit. Thank you – Pieter Goosen Jul 30 '14 at 10:59
  • @estin92 sorry but I'm still confused ;-) I expect is_singular() to be true on your static front page. What do you expect your code to show on the front page? Can you 1) give the exact HTML source of what you expect to see on the front page and 2) then the HTML source of what you got instead, 3) explaining also what PHP code snippet you used exactly. Thanks. – birgire Aug 2 '14 at 12:25
-4

You can use this code.

<?php

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
  • Please explain why your code should be used, and what is the relevance to the OP. And please correct your mark up – Pieter Goosen Jul 30 '14 at 10:17
  • You can pass attribute in array new WP_Query( $args ); and get what result you want. – user57366 Jul 30 '14 at 11:17
  • Pieter Goosen, can I ask one question ? – user57366 Jul 30 '14 at 12:13
  • Feel free to do so :-) – Pieter Goosen Jul 30 '14 at 12:17
  • Kindly give me suggestion for this quetion wordpress.stackexchange.com/questions/156122/… We need also same thing. – user57366 Jul 30 '14 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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