php - Simple Wordpress Loop result completely wrong and full of breaks and <p> elements

103

I am trying to make a simple loop through my wordpress posts but when it comes to output the whole thing the loop puts it anywhere but not where I said, it should be. For example the loop adds breaks and

but they are not in my posts. I had this problem once but dont know anymore what i have done to get over this problem.

MY LOOP:

<?php
$args = array(
  'post_type' => 'post',
  'posts_per_page' => 4
);

$loop = new WP_Query($args);
if ($loop->have_posts()) {
  while ($loop->have_posts()) : $loop->the_post();
    $post_id = get_the_ID();
    $content = the_content();
    $headline = get_post_meta($post_id, 'headline', true);
    $subtitle = get_post_meta($post_id, 'untertitel', true);
    $class = wp_get_post_terms( $post_id, 'post_tag', $termsargs );
    $bgimage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );

    echo '<section >';
    echo '<article >';
    echo '<header>';
    echo '<h2><strong>'.$headline.'</strong></h2>';
    echo '<h3>'.$subtitle.'</h3>';
    echo '<div >';
    echo $content;
    echo '</div>';
    echo '</header>';
    echo '</article>';
    echo '</section>';

  endwhile;
} else {
  echo __('Fehler!');
}
wp_reset_postdata();
?>

RESULT:

result of loop

What is going wrong here?

87

Answer

Solution:

The functionthe_content() is printing the data. you should useget_the_content() instead.

832

Answer

Solution:

I can see what's going wrong here. Theecho $content; line is printing improper HTML. I know this because you've said in your caption there are many<p> tags where they should not be; and you're not (visibly) printing any<p> tags in this block of code.

Investigate what this method is returning:$content = the_content(); That should do the trick.

People are also looking for solutions to the problem: php - How to pass data or data array while rendering view in codeigniter?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.