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()
the_content()
. Does the content actually include the title? If not you likely have a filter somewhere that is adding it to begin with.the_title()
part, leaving just thethe_content()
part, does the same issue persists? Or try vice-versa - remove the latter part and keep the former.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.