Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
The hook will allows to limiting display long characters (as defined in condition) for Question view and will add `read more` link. It will expand all question content when click on read more link.
* hook: Add read more link to the question view q-content
* hook by: Q2A Market
* author: Jatin Soni
* url:
* version: 1.00
* Description:
* The hook will add 'read more' link to the
* question's content after defined number of characters
* and hide rest of the characters.
* usage:
* Place below code into your theme or plugin layer
* function q_view_content(){}
* Disclaimer:
* This program is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY
* See the GNU General Public License for more details.
public function q_view_content($q_view)
if (!empty($q_view['content'])) {
$this->output('<div class="qa-q-view-content">');
// remove div tag from content
// but there is a risk if someone add
// div within the content
$tag = 'div';
// Again adding entry-content to support core
$this->output('<div class="entry-content">');
// displaying content based on character count
if (strlen(strip_tags(preg_replace("/<\/?" . $tag . "(.|\s)*?>/", '', $q_view['content']))) > 100) { // change 500 with your number
$this->output('<div id="descitem">');
$str = substr(preg_replace("/<\/?" . $tag . "(.|\s)*?>/", '', $q_view['content']), 0, 100) . '...<a href="#" id="readmore">read more</a>'; // change 500 with your number
$this->output('<div id="fullcont" style="display:none">');
$this->output_raw(preg_replace("/<\/?" . $tag . "(.|\s)*?>/", '', $q_view['content']));
} else {
// this will output content without readmore
// if there is less then defined character count
$this->output_raw(preg_replace("/<\/?" . $tag . "(.|\s)*?>/", '', $q_view['content']));
$this->output('</div>'); // end of entry-content
$this->output('</div>'); // end of qa-q-view-content
//javascript to show hide divs
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.