Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
140 views
in Plugins by

<?php

class q2a_random_question_images {

    function allow_template($template) {

        $allow = false;

        switch ($template) {

            case 'account':

            case'activity':

            case'admin':

            case'ask' :

            case'categories' :

            case'custom' :

            case'favorites':

            case'feedback' :

            case'hot' :

            case'ip' :

            case'login':

            case'message':

            case'qa' :

            case'question':

            case'questions':

            case'register' :

            case'search' :

            case'tag' :

            case'tags' :

            case'unanswered':

            case'updates' :

            case'user' :

            case'users' :

                $allow = true;

                break;

        }

        return $allow;

    }

    function allow_region($region) {

        return true;

    }

    function output_widget($q_list) {

$random_question = qa_db_read_one_assoc( qa_db_query_sub('SELECT * FROM ^posts q INNER JOIN ^posttags t ON q.postid=t.postid INNER JOIN ^words w ON t.wordid=w.wordid WHERE q.type="Q" AND w.word=$ ORDER BY rand() LIMIT 1', "funny-images" ));

echo "<h2>Random Images</h2>";

//replace yourwebsite with your website

          echo "<div class='qa-q-list-item' ><div class='qa-q-item-title'><a href='https://website.com/" . $random_question['postid'] . "'>" . $random_question['title'] . "</a></div><br></div>";

          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" />'; // change the size using attr or css

                        $q_list['qs'][$index]['content'] = '<a href="/'.$question['raw']['postid'].'" >'.$image.'</a>';

                    }

                }

            }

        }

    }

         

}

}

by
Please describe the problem you're facing instead of just dumping a block of code. What do you expect the code to do? What does it actually do that you didn't expect? Are you getting errors? What do they say?
by
I want random images to show from random question in a widget
by
edited by
I have modified this plugin for random question from particular tag.
https://github.com/bluegenel/q2a-random-question

But I also want to show image from question.

Please log in or register to answer this question.

...