BlogThemes

How to Truncate WordPress Post Titles with PHP

Whereas engaged on our shopper’s web site, the design’s width wouldn’t enable us to maintain the lengthy titles to be displayed on the homepage. After looking the net for choices, we got here throughout a number of plugins that may truncate WordPress titles, however they’d do it sitewide whereas we solely wished it on the homepage. On this article, we are going to share with you how one can truncate WordPress Publish Titles with PHP.

First open your index.php or the file location the place you need to truncate the title. Then paste the next code to interchange your the_title tag.

1
2
3
4
5
6
7
8
9
<a href="<?php the_permalink() ?>">
<?php
$thetitle = $publish->post_title; /* or you need to use get_the_title() */
$getlength = strlen($thetitle);
$thelength = 25;
echo substr($thetitle, 0, $thelength);
if ($getlength > $thelength) echo "...";
?>
</a>

Ensure you edit the $thelength variable from 25 to the character rely of your want. You would need to estimate the rely in your theme design. The code provides the size variable after which use the conditional tag to see if the title size matches our desired size. Whether it is longer, then the code provides ‘…’ in entrance. More often than not, you’ll use this solely on particular areas the place the width is fastened within the theme.

*This can be a good code to have as a theme designer*.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBest WordPress PluginsBlog

9 Best Social Proof Plugins for WordPress & WooCommerce (2020)

Tips

How To Disable The WordPress User Admin Toolbar

Tips

WordPress Front Page vs Homepage

Tips

5 Things You Must Avoid Doing in WordPress

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro