Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
–1 vote
55 views
ago in Themes by

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" max-width="100%"/>'; // change the size using attr or css

                        $q_list['qs'][$index]['content'] = $image;

                    }

                }

            }

        }

    }

    qa_html_theme_base::q_list($q_list);

}

1 Answer

+1 vote
ago by
selected ago by
 
Best answer

Try

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

Or maybe just

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

Correct syntax errors if any.

It is advisable to use rel="nofollow" tags because the links will redirect to the questions will full urls, and it won't hurt your website crawl budget if Googlebots discover them.

ago by
This would probably suffice:

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

Presumably you do want bots to follow links to the actual questions, so "nofollow" would be counter-productive here.
ago by
edited ago by
This is a script to display images on question lists. He's already had the question urls in the titles.
ago by
$q_list['qs'][$index]['content'] = '<a href="/'.$question['raw']['postid'].'" rel="nofollow">'.$image.'</a>';

This works
Thanks alot....
...