WordPress loop displaying other post titles instead of just one

I have a problem with posts showing titles from other posts, instead of just the single post unique to it. Other parts are fine, i.e. no other repetition, just the titles.

My code that generates the content:

get_header();

$the_query = new WP_Query( 'category_name=careers&showposts=5' );
while ( $the_query->have_posts() ) :
    $output = "";
    $the_query->the_post();
    $output_title .= get_the_title();
    $output_content .= get_the_content();
    $output_type = get_field('job_type');
    $output_salary = get_field('job_salary');
    $output_intro = get_field('job_intro');

    $careers.= '
        <div class="careers">
            <h3>'.$output_title.'</h3>
            <p class="type">'.$output_type.'</p>
            <p class="salary">'.$output_salary.'</p>
            <p>'.$output_intro.'</p>
    </div>
    ';
endwhile;
wp_reset_postdata();

So what's happening is that the latest post shows a single title, which is fine, but then the second post shows its title + latest, the third job post shows its title +2+1st. For example:

  • Job post 1
  • Job Post 1Job post 2
  • Job Post 1 Job Post 2Job Post 3..

When it should be:

  • Job post 1
  • Job post 2
  • Job Post 3

Answers


Well it looks like you are concatenating strings i.e. remove the dot in front of the =

$output_title .= get_the_title();

Should be

$output_title = get_the_title();

The issue is caused by you using $output_title .= get_the_title(). The .= should be changed to just =.

while ( $the_query->have_posts() ) :
    $output = "";
    $the_query->the_post();
    $output_title = get_the_title(); // Change this line
endwhile;

Need Your Help

Insert input text data only if associated checkbox checked MySql

javascript php jquery html mysql

here is my HTMl code actually i want to insert the input text data if only the checkbox is checked and there are 50+ input fields with unique data to insert like this HTML

Stopping PHP script putting URL's in address bar?

php

I have a script that pulls in meta data from a list of URL's but when I try to out too many it it says URL is too long and wont run.