{"id":279,"date":"2013-01-08T05:12:03","date_gmt":"2013-01-08T05:12:03","guid":{"rendered":"http:\/\/pixert.com\/blog\/?p=279"},"modified":"2013-01-08T05:12:30","modified_gmt":"2013-01-08T05:12:30","slug":"keep-html-tags-wordpress-limit-posts","status":"publish","type":"post","link":"https:\/\/pixert.com\/blog\/keep-html-tags-wordpress-limit-posts\/","title":{"rendered":"Keep HTML Tags in WordPress Limit Posts &#8211; the_content_limit"},"content":{"rendered":"<p><a href=\"http:\/\/pixert.com\/blog\/how-to-upgrade-wordpress\/wordpresslogo\/\" rel=\"attachment wp-att-25\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"25\" data-permalink=\"https:\/\/pixert.com\/blog\/how-to-upgrade-wordpress\/wordpresslogo\/\" data-orig-file=\"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo.png?fit=498%2C113&amp;ssl=1\" data-orig-size=\"498,113\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;}\" data-image-title=\"WordPress &#8211; the logo\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo.png?fit=300%2C68&amp;ssl=1\" data-large-file=\"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo.png?fit=498%2C113&amp;ssl=1\" class=\"alignright size-medium wp-image-25\" alt=\"WordPress - the logo\" src=\"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo-300x68.png?resize=300%2C68\" width=\"300\" height=\"68\" srcset=\"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo.png?resize=300%2C68&amp;ssl=1 300w, https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo.png?w=498&amp;ssl=1 498w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>This function, the_content_limit \u00a0controls the maximum\u00a0amount of characters displayed for an entry on the main page on WordPress homepage posts, e.g Featured Posts, Recent Posts<br \/>\nHowever , the_content_limit stripped HTML tags just like the_excerpt. What if we want to keep HTML tags<\/p>\n<p>We make modification to the function, the_content_limit which is usually found in functions.php<\/p>\n<pre class=\"brush: php; title: Code Block; notranslate\" title=\"Code Block\">\r\n\r\nfunction the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {\r\n$content = get_the_content($more_link_text, $stripteaser, $more_file);\r\n\/\/ remove &#x5B;caption] shortcode\r\n$content = preg_replace(&quot;\/\\&#x5B;caption.*\\&#x5B;\\\/caption\\]\/&quot;, '', $content);\r\n\/\/ short codes are applied\r\n\r\n$content = apply_filters('the_content', $content);\r\n$content = str_replace(']]&gt;', ']]&gt;', $content);\r\n$content = strip_tags($content,'\r\n,&lt;b&gt;,&lt;strong&gt;,&lt;a&gt;,&lt;i&gt;,\r\n');\r\n\r\nif ((strlen($content)&gt;$max_char) &amp;&amp; ($espacio\u00a0= strpos($content, &quot; &quot;, $max_char ))) {\r\n$content = substr($content, 0, $espacio);\r\n$content = $content;\r\necho &quot;\r\n&quot;;\r\necho $content;\r\necho &quot;...&quot;;\r\necho &quot;\r\n\r\n&quot;;\r\n}\r\nelse {\r\necho &quot;\r\n&quot;;\r\necho $content;\r\necho &quot;\r\n\r\n&quot;;\r\n}\r\n}\r\n\r\n<\/pre>\n<p>We remove $content = strip_tags($content);\u00a0 from <a title=\"WordPress Limit Post\" href=\"http:\/\/pixert.com\/blog\/wordpress-limit-post\/\">original code of the_content_limit<\/a> and replace it with\u00a0$content = strip_tags($content,'&lt;p&gt;,&lt;b&gt;,&lt;strong&gt;,&lt;a&gt;,&lt;i&gt;,&lt;br&gt;&#8217;);<br \/>\nThis will allow the_content_limit to show paragraph, bold texts, italic texts.<\/p>\n<p><strong>What is strip_tags?<\/strong><\/p>\n<p>We can remove all html tags from the content by using strip_tags string function in PHP. This function can be used to collect only the content part without reading the html formatting of the page. Here is the syntax<\/p>\n<h3>strip_tags (input string [, string allowable_tags])<\/h3>\n<p>This function also allows one optional string where we can keep the tags which are not to be removed. Say we don&#8217;t want the hyper links to be removed. So we will allow the linking tag\u00a0<i>&lt;a href<\/i>\u00a0to be kept as it is. Here is the syntax for this.<\/p>\n<h3>strip_tags($string_str, &#8216;&lt;a&gt;&#8217;);<\/h3>\n<p>In the above code\u00a0<i>$string_str<\/i>\u00a0is the input string, in our case it is $content<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This function, the_content_limit \u00a0controls the maximum\u00a0amount of characters displayed for an entry on the main page on WordPress homepage posts, e.g Featured Posts, Recent Posts However , the_content_limit stripped HTML tags just like the_excerpt. What if we want to keep HTML tags We make modification to the function, the_content_limit which is usually found in functions.php [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[54,5],"tags":[55,39,6],"class_list":["post-279","post","type-post","status-publish","format-standard","hentry","category-code-snippet","category-wordpress","tag-code-snippet-2","tag-php","tag-wordpress-2"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1pvi1-4v","jetpack-related-posts":[{"id":268,"url":"https:\/\/pixert.com\/blog\/wordpress-limit-post\/","url_meta":{"origin":279,"position":0},"title":"WordPress Limit Post","author":"Pixel Insert \/ Pixert","date":"November 30, 2012","format":false,"excerpt":"This function controls the maximum\u00a0amount of characters displayed for an entry on the main page. If the set limit is surpassed, the text on the entry will be chopped\u00a0to that amount of characters, otherwise the content will be showed unchanged. Add following code to functions.php in your theme folder [php]\u2026","rel":"","context":"In &quot;Code Snippet&quot;","block_context":{"text":"Code Snippet","link":"https:\/\/pixert.com\/blog\/category\/code-snippet\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo-300x68.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":130,"url":"https:\/\/pixert.com\/blog\/enable-formatting-tags-for-content-limit-in-genesis-featured-posts-widget\/","url_meta":{"origin":279,"position":1},"title":"Enable Formatting Tags for Content Limit in Genesis Featured Posts widget","author":"Pixel Insert \/ Pixert","date":"January 10, 2013","format":false,"excerpt":"Genesis Framework has same function that return the content after trimming and it strips all HTML The following code help you keep or filter some HTML tags when you are using Content Limit in Theme Settings or Widgets (e.g Genesis - Featured Posts widget) Add following to functions.php in the\u2026","rel":"","context":"In &quot;Code Snippet&quot;","block_context":{"text":"Code Snippet","link":"https:\/\/pixert.com\/blog\/category\/code-snippet\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2013\/01\/gallerygenesis.jpg?fit=600%2C340&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2013\/01\/gallerygenesis.jpg?fit=600%2C340&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2013\/01\/gallerygenesis.jpg?fit=600%2C340&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":284,"url":"https:\/\/pixert.com\/blog\/how-to-add-show-thumbnail-option-in-wpzoom-featured-category-widget\/","url_meta":{"origin":279,"position":2},"title":"How to add &#8216;Show Thumbnail&#8217; option in WPZOOM Featured Category Widget","author":"Pixel Insert \/ Pixert","date":"February 18, 2013","format":false,"excerpt":"Tribune is a lovely magazine theme from WPZOOM. The theme has custom widgets name WPZOOM Featured Category Widget You could use this widget to display categories on homepage like the demo site\u00a0(Politics - World - Sports - Automotive). The widget show thumbnails by default, it doesn't have feature to disable\u2026","rel":"","context":"In &quot;Code Snippet&quot;","block_context":{"text":"Code Snippet","link":"https:\/\/pixert.com\/blog\/category\/code-snippet\/"},"img":{"alt_text":"WPZOOMFeaturedCategory","src":"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2013\/02\/WPZOOMFeaturedCategory.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":447,"url":"https:\/\/pixert.com\/blog\/wordpress-randomize-posts-category-page\/","url_meta":{"origin":279,"position":3},"title":"WordPress: Randomize posts in a category page","author":"Pixel Insert \/ Pixert","date":"July 19, 2016","format":false,"excerpt":"The following lines of code change the way of displaying posts in a category page from default to date to random order. Please add following lines of code to functions.php [php] add_action( 'pre_get_posts', 'random_category_posts' ); function random_category_posts( $query ) { if ( $query-&gt;is_category() &amp;&amp; $query-&gt;is_main_query() ) { $query-&gt;set( 'orderby', 'rand'\u2026","rel":"","context":"In &quot;Code Snippet&quot;","block_context":{"text":"Code Snippet","link":"https:\/\/pixert.com\/blog\/category\/code-snippet\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":292,"url":"https:\/\/pixert.com\/blog\/wordpress-conditional-tag-for-login-page\/","url_meta":{"origin":279,"position":4},"title":"WordPress Conditional Tag for Login Page","author":"Pixel Insert \/ Pixert","date":"March 6, 2013","format":false,"excerpt":"WordPress have conditional tags. The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches Following conditional tag is specific to Login Page, that is currently not available\u2026","rel":"","context":"In &quot;Code Snippet&quot;","block_context":{"text":"Code Snippet","link":"https:\/\/pixert.com\/blog\/category\/code-snippet\/"},"img":{"alt_text":"wordpress-logo-notext-rgb","src":"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2013\/08\/wordpress-logo-notext-rgb.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":257,"url":"https:\/\/pixert.com\/blog\/conditional-tags-for-wp-e-commerce\/","url_meta":{"origin":279,"position":5},"title":"Conditional Tags for WP E-Commerce","author":"Pixel Insert \/ Pixert","date":"August 29, 2012","format":false,"excerpt":"The following conditional works when you install WP E-Commerce and you want to add PHP code in page.php .I want to breadcrumbs in pages but doesn't want it to display on WP E-Commerce Product page, because the product already showing its own breadcrumbs The following conditional tag apple to non-Product\u2026","rel":"","context":"In &quot;Code Snippet&quot;","block_context":{"text":"Code Snippet","link":"https:\/\/pixert.com\/blog\/category\/code-snippet\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/pixert.com\/blog\/wp-content\/uploads\/2011\/03\/wordpresslogo-300x68.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/posts\/279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/comments?post=279"}],"version-history":[{"count":0,"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/posts\/279\/revisions"}],"wp:attachment":[{"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/media?parent=279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/categories?post=279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixert.com\/blog\/wp-json\/wp\/v2\/tags?post=279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}