• I am hoping to use the Best Related Posts plugin with thumbnails. It looks really good but there are some major issues with function. Hoping someone can help me, as the developer is not answering support questions at this time.

    You can see an example here on my test site (scroll down to 5 square images below post content).

    Problems I want to solve:

    1. Big problem: “related” posts are completely unrelated. Right now, some of the recommended related posts have nothing to do with the article (for example, on an “environment” related post, the plugin recommends a post on “hemorrhoids”!!). Using the “tags” in determining matches would give a much tighter post result. Can someone tell me how to adapt the code to incorporate that? Seems like an easy fix.

    I think this is the code involved with finding the matches:

    function boposts_find_posts()
    {
        global $wpdb, $post, $boposts_options;
    
        $max = $boposts_options['max'];
        if (!$max) $max = 5;
        $terms = preg_replace('/[^a-z0-9]/i', ' ', $post->post_title);
        $terms2 = preg_replace('/[^a-z0-9]/i', ' ', strip_tags($post->post_content));
        if (strlen($terms2) > 100) {
            $x = strpos($terms2, ' ', 100);
            if ($x > 0) $terms2 = substr($terms2, 0, $x);
        }
        $now = gmdate("Y-m-d H:i:s", time() + get_settings('gmt_offset')*3600);
    
        $query = 'select id, post_content, post_title, match(post_title, post_content) against (\'' . $terms . ' ' . $terms2 . '\') as score from ' . $wpdb->posts  .
        ' where match(post_title, post_content) against (\'' .
        $terms . ' ' . $terms2 . "') and post_date<='" . $now . "'" .
        ' and post_type in (\'post\') and post_status in (\'publish\') and id!=' . $post->ID .
        ' order by score desc limit ' . $max;
    
        return $wpdb->get_results($query);
    
    }

    2. Some of my images are not appearing. They all do show up on my archives/category listing page (using ‘thumbnail’). Is there any way to adapt the plugin to use the same image as what is used on my archives/category listing page to make sure an image displays?

    3. Some images are distorted. I want my images to be a square shape (110px x 110px). Currently, the images on my archives/category listing page are resized/cropped. Is there any way to do the same for the images that display from the plugin? Right now, they appear distorted and it looks sloppy.

    I believe this is the code that would need to be modified for the above two issues:

    /**
     * Outputs the HTML code with the related posts list.
     */
    function boposts_show()
    {
        global $wpdb, $post, $boposts_options;
    
        $results = boposts_find_posts();
        if (!$results) {
            echo 'No results';
            return;
        }
    
        echo $boposts_options['header'];
        $c = count($results);
        for ($i=0; $i<$c; $i++)
        {
            $r = &$results[$i];
            $p = get_post($r->id);
            $t = get_the_title($r->id);
            $excerpt = $r->post_content;
            $l = get_permalink($r->id);
    
            $content = $r->post_content;
            // Remove the short codes
            $content = preg_replace('/\[.*\]/', '', $content);
            // Image extraction
            $image = '';
            $x = stripos($content, '<img');
    
            if ($x !== false) {
                $x = stripos($content, 'src="', $x);
                if ($x !== false) {
                    $x += 5;
                    $y = strpos($content, '"', $x);
                    $image = substr($content, $x, $y-$x);
                }
            }
    
            if ($image == '') $image = get_option('siteurl') . '/wp-content/plugins/best-related-posts/empty.gif';
    
            // Excerpt extraction
            $excerpt = strip_tags($content);
            if (strlen($excerpt) > $boposts_options['excerpt'])
            {
                $x = strpos($excerpt, ' ', $boposts_options['excerpt']);
                if ($x !== false) $excerpt = substr($excerpt, 0, $x);
            }
            $s = $boposts_options['body'];
            $s = str_replace('{link}', $l, $s);
            $s = str_replace('{title}', $t, $s);
            $s = str_replace('{image}', $image, $s);
            $s = str_replace('{excerpt}', $excerpt . '...', $s);
    
            echo $s;
        }

    4. Impact on load time? This plugin looks great, but as I am new to WP and programming, I am unsure if it will have any big effect on load time for my posts. Will this slow my site down a lot?

    Huge thanks to anyone that can help me out.

Viewing 1 replies (of 1 total)
  • Thread Starter wildbug

    (@wildbug)

    Not sure if this will help for #2 and 3 above…on my archives.php it uses:

    <a class="attachmentarchives" href="<?php the_permalink(); ?>"><?php fs_attachment_image($post->ID, 'thumbnail', 'alt="' . $post->post_title . '"'); ?></a>

    to pull the thumbnail.

    How would I incorporate that into the Best Related Posts plugin:

    /**
     * Outputs the HTML code with the related posts list.
     */
    function boposts_show()
    {
        global $wpdb, $post, $boposts_options;
    
        $results = boposts_find_posts();
        if (!$results) {
            echo 'No results';
            return;
        }
    
        echo $boposts_options['header'];
        $c = count($results);
        for ($i=0; $i<$c; $i++)
        {
            $r = &$results[$i];
            $p = get_post($r->id);
            $t = get_the_title($r->id);
            $excerpt = $r->post_content;
            $l = get_permalink($r->id);
    
            $content = $r->post_content;
            // Remove the short codes
            $content = preg_replace('/\[.*\]/', '', $content);
            // Image extraction
            $image = '';
            $x = stripos($content, '<img');
    
            if ($x !== false) {
                $x = stripos($content, 'src="', $x);
                if ($x !== false) {
                    $x += 5;
                    $y = strpos($content, '"', $x);
                    $image = substr($content, $x, $y-$x);
                }
            }
    
            if ($image == '') $image = get_option('siteurl') . '/wp-content/plugins/best-related-posts/empty.gif';
    
            // Excerpt extraction
            $excerpt = strip_tags($content);
            if (strlen($excerpt) > $boposts_options['excerpt'])
            {
                $x = strpos($excerpt, ' ', $boposts_options['excerpt']);
                if ($x !== false) $excerpt = substr($excerpt, 0, $x);
            }
            $s = $boposts_options['body'];
            $s = str_replace('{link}', $l, $s);
            $s = str_replace('{title}', $t, $s);
            $s = str_replace('{image}', $image, $s);
            $s = str_replace('{excerpt}', $excerpt . '...', $s);
    
            echo $s;
        }
        echo $boposts_options['footer'];
        echo '<div align="right"><small></small></div>';
    }
    
    add_action('admin_menu', 'boposts_admin_menu');
    register_activation_hook(__FILE__, 'boposts_activate');
    register_deactivation_hook(__FILE__, 'boposts_deactivate');
    
    ?>
Viewing 1 replies (of 1 total)

The topic ‘How to add tags to match criteria for “related posts” plugin’ is closed to new replies.