Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

show question first image for thumb in questions list

+2 votes
6,834 views
asked Feb 28, 2015 in Themes by Parsi

hi,

is there any way to show first question image for thumb in question list ?

in this plugin :

Tab Widget by Towhid @ QA-Themes. popular and recent questions in two tabs, responsive design.

first image of question showing as thumb in popular and recent questions widget

so it is possible but can i use this plugin to show thumb in question list below the question title ?

thanks for helping me !

 

Q2A version: 1.7
commented Mar 5, 2015 by Parsi
thank you very mush ;)
i will waiting for you
commented Mar 7, 2015 by Parsi
hi jatin
sorry, for my message again
do you find any code for this option ?
i realy need that code !
thank you very much
commented Mar 8, 2015 by Parsi
jatin ?  -----------
commented Mar 9, 2015 by jatin.soni
I will try to write it tonight. lets see if I would be able to get it done today.

1 Answer

+6 votes
answered Mar 10, 2015 by jatin.soni
selected Mar 11, 2015 by Parsi
 
Best answer

So here we go. I have made entire code to get first image from the question content. Add below code to your theme file. Just make sure your theme should not have q_list method. If it is there than you may need to merge both code.


Updated on (2015/03/11)

Added condition to show image only and if the content has at least one image.

Download code from Gist

/**
 * Add first image to the question list
 * 
 * Declearation
 * Base code is taken from Mouseover Layer plugin
 * 
 * Than I have extracted content using preg_match_all to get first image
 * and assing that to the q_list
 * 
 * How to use it:
 * Nothing complicated, just place 'q_list' method into your theme or plugin file and done.
 * 
 * Important:
 * Make sure your theme should not have this function already. If there is than add this code
 * to that function and modify as needed
 * 
 * @param array $q_list
 * @category Hook
 * @since 1.7
 * @author Q2A Market <dev@q2amarket.com>
 * @link http://www.q2amarket.com Q2A Market
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */
public function q_list($q_list)
{

    if (!empty($q_list['qs'])) { // first check it is not an empty list and the feature is turned on
        // Collect the question ids of all items in the question list (so we can do this in one DB query)
        $postids = array();
        foreach ($q_list['qs'] as $question)
        {
            if (isset($question['raw']['postid']))
                $postids[] = $question['raw']['postid'];
        }

        if (!empty($postids)) {

            // Retrieve the content for these questions from the database and put into an array fetching
            // the minimal amount of characters needed to determine the string should be shortened or not

            $result   = qa_db_query_sub('SELECT postid, content, format FROM ^posts WHERE postid IN (#)', $postids);
            $postinfo = qa_db_read_all_assoc($result, 'postid');

            // Get the regular expression fragment to use for blocked words and the maximum length of content to show
            $blockwordspreg = qa_get_block_words_preg();

            // Now add the popup to the title for each question
            foreach ($q_list['qs'] as $index => $question)
            {
                if (isset($postinfo[$question['raw']['postid']])) {
                    $thispost = $postinfo[$question['raw']['postid']];
                    $text     = qa_viewer_html($thispost['content'], $thispost['format'], array('blockwordspreg' => $blockwordspreg));

                    // Extract image source from content
                    preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $text, $matches);

                    // If content has image than show it!
                    if (!empty($matches[0])) {

                        // assign image to the variable
                        $image                           = '<img src="' . $matches[1][0] . '" alt="image" class="q-list-image" width="64"/>'; // change the size using attr or css
                        $q_list['qs'][$index]['content'] = $image;
                    }
                }
            }
        }
    }

    qa_html_theme_base::q_list($q_list);
}
commented Dec 20, 2015 by nabibux
thank you very much jatin.soni
commented Dec 20, 2015 by jatin.soni
Glad....You are welcome....!
commented Dec 26, 2015 by atsmod
where to add this code? I use Classic theme
commented Jul 28, 2016 by Ada Donie Abdullah
its not working with blobs , @jatin can you help me plz
...