0

I have this code that I use to get the posts that are in a certain category. The problem is the function the_content() returns the title of the page at the top of the rest of the content.

Is there a way to filter the title out? I am not sure how to use filters and I am not sure that is what I am supposed to be using for this?

<?php 
$args = array(
  'post_type' => 'post',
  'suppress_filters' => true,
  'orderby' => 'menu_order', 
  'posts_per_page' => 30,                
  'tax_query' => array(
    array(
    'taxonomy' => 'category',
    'field'    => 'slug',
    'terms'    => array( 'services' ),
  ),)
);
$additional_loop = new WP_Query($args); 
while ($additional_loop->have_posts()) : $additional_loop->the_post();
?>  
<article id="post-<?php the_ID(); ?>" class="post_article">
  <div class="col-xs-12  no_pad">
      <h3><?php echo the_title();?></h3>
    <?php the_content(); ?>
  </div>
</article><!-- #post-## -->
<?php
endwhile;
?>

Just to be clear, the code above will out put something like:

<article id="post-111" class="post_article">
  <div class="col-xs-12  no_pad">
      <h3>This is my title</h3>
      This is my title<br />
      This is the rest of the content for the post. bla bla bla
  </div>
</article><!-- #post-## -->

I have not found an answer to my question. I did find a workaround. Here is my work around. I ended up using the_excerpt instead. Then added code to the function.php file so that I could control the length of the excerpt.

<?php 
$args = array(
  'post_type' => 'post',
  'suppress_filters' => true,
  'orderby' => 'menu_order', 
  'posts_per_page' => 30,                
  'tax_query' => array(
    array(
    'taxonomy' => 'category',
    'field'    => 'slug',
    'terms'    => array( 'services' ),
  ),)
);
$additional_loop = new WP_Query($args); 
while ($additional_loop->have_posts()) : $additional_loop->the_post();
?>  
<article id="post-<?php the_ID(); ?>" class="post_article">
  <div class="col-xs-12  no_pad">
      <h3><?php echo the_title();?></h3>
    <?php the_exerpt(); ?>
    <p><a class="btn btn-default read-more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More</a></p>

  </div>
</article><!-- #post-## -->
<?php
endwhile;
?>




This is the function that I put in the function.php

function wpdocs_custom_excerpt_length( $length ) {
return 300;
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

I would still love to know how to get the title out of the_content(). Meaning I do not want the title to be printed out when I use the function the_content()

8
  • 1
    This is not the normal behaviour of the_content(). Does the content actually include the title? If not you likely have a filter somewhere that is adding it to begin with. Commented Jul 18, 2021 at 4:50
  • hummmm. This is not the first time I have run into this. The other time I was using a different theme and NO the title is not actually in the content. Maybe I should try searching the code for add_filter( 'the_content'
    – PeggyMe
    Commented Jul 18, 2021 at 6:21
  • If you remove the the_title() part, leaving just the the_content() part, does the same issue persists? Or try vice-versa - remove the latter part and keep the former.
    – Sally CJ
    Commented Jul 18, 2021 at 7:05
  • Yes. it does. I also tried searching the entire site for add_filter. With only looking at the code that comes directly after add_filter, I do not see any filter that could have produced this error
    – PeggyMe
    Commented Jul 18, 2021 at 7:48
  • I have edited your post - the sample HTML output was not properly formatted. So if you look at that output, the duplicate title is part of the post content or maybe the excerpt, so you could try looking for the_excerpt, and/or try deactivating all plugins and see if that helps. There could also be some JS which added the extra title there, so try checking for suspicious scripts.
    – Sally CJ
    Commented Jul 18, 2021 at 8:32

1 Answer 1

-2

you need to remove the echo from the the_title().

Change this:

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

Source : https://developer.wordpress.org/reference/functions/the_title/

1
  • You are right, the echo should not be there. Only that is not the problem. The problem is that the title comes in along with the content. See, the title is on the page two times.
    – PeggyMe
    Commented Jul 18, 2021 at 6:21

Your Answer

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

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