Forums

[resolved] [Plugin: DG Random Image] link from image to page of gallery (3 posts)

  1. now
    Member
    Posted 1 year ago #

    Hello,
    Have anyone experience with the DG random image plugin?

    I have a problem with the link from image to page.

    In my right sidebar, i have the image displayed randomly by the plugin. Everything looks perfect. Except that when I click on the image, i'm directed to the home page of the site and not the parent subpage containing the gallery from which the image is taken.

    The title of the image displayed when mouse is hover is correct.
    Have a look here http://buvezmadison.com/site
    I managed to display the title of the page containing the list of the subpages (thumbnails, using autonav plugin) and link it to that page.But that's pretty the best i can do with php (and it might even be totally a "crappy" way of coding… sorry sir!)

    I tried it with different permalink types but nothing i tried worked…
    Can anyone maybe help me? Thanks a lot.

    here under is the code of the plugin…

    <?php
    /*
     * Plugin Name: DG Random Image
     * Plugin URI: http://www.dutchguys.com/plugins
     * Description: Displays a random image from one of the children of a named page.<br> Read more about in on <a href="http://www.dutchguys.com">dutchguys.com</a>
     * Version: 1.01
     * Author: Menno Dekker
     * Author URI: http://www.dutchguys.com
     * License: GPL
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     */
    
    /**
     * Display a random image attached to a named page or one of it's children.
     *
     * @param array $args key (default value) <b>title</b> (Gallery) <b>class</b> (null) <b>x</b> (125) <b>y</b> (125)
     * @return thumbnail image linked to page
     */
    function DG_Random_Image($args = '') {
    
        static $aImages = array();
        static $sPageTitle = '';
    
        if (is_array($args)) {
            $r = $args;
        } else {
            parse_str($args, $r);
        }
    
        //Parse the defaults
        $defaults = array('title' => 'Gallery', 'class' => '', 'x' => 125, 'y' => 125);
        $r = array_merge($defaults, $r);
    
        //Now check if we used this page before and if so wether we have images or not
        if (count($aImages) == 0 or $sPageTitle != $r['title']) {
            $sPageTitle = $r['title'];
            $c = array('child_of' => get_page_by_title($r['title'])->ID,
                'name' => 'page_id'
            );
            $pages = get_pages($c);
            //Now loop through all the child pages to find attached pictures
            foreach ($pages as $pagg) {
                $attachments = get_children(array('post_parent' => $pagg->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
                if (!empty($attachments)) {
                    foreach ($attachments as $att) {
                        $aImages[] = array('ID' => $att->ID, 'parent' => $pagg->guid, 'title' => $pagg->post_title);
                    }
                }
            }
        }
        //Okay we have an array containing image id, page url and page title
        $rId = rand(0, count($aImages) - 1);
        $aImg = wp_get_attachment_image_src($aImages[$rId]['ID'], 'thumbnail', false);
        if ($r[''] !== '')
            $sClass = ' class="' . $r['class'] . '"';
        $alt = ' alt="' . $aImages[$rId]['title'] . '"';
        $title = ' title="' . $aImages[$rId]['title'] . '"';
        $sReturn = '<a href="' . $aImages[$rId]['parent'] . '"><img src="' . $aImg[0] . '" width="' . $r['x'] . '" height="' . $r['y'] . '"' . $sClass . $alt . $title . '></a>';
        return $sReturn;
    }
    
    /**
     * Add function to widgets_init that'll load our widget.
     * @since 1.01
     */
    add_action('widgets_init', 'dg_random_image_load_widgets');
    
    /**
     * Register our widget.
     * 'DG_Random_Image_Widget' is the widget class used below.
     *
     * @since 1.01
     */
    function dg_random_image_load_widgets() {
        register_widget('DG_Random_Image_Widget');
    }
    
    /**
     * DG Random Image Widget class.
     * This class handles everything that needs to be handled with the widget:
     * the settings, form, display, and update.  Nice!
     *
     * @since 1.01
     */
    class DG_Random_Image_Widget extends WP_Widget {
    
        /**
         * Widget setup.
         */
        function __construct() {
    
            /* Widget settings. */
            $widget_ops = array('title' => 'DG Random Image', 'description' => __('Displays a random image from your galleries', 'dg-random-image'));
    
            /* Widget control settings. */
            $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'dg-random-image');
    
            /* Create the widget. */
            parent::__construct('dg-random-image', __('DG Random Image', 'dg-random-image'), $widget_ops, $control_ops);
        }
    
        /**
         * How to display the widget on the screen.
         */
        function widget($args, $instance) {
            extract($args);
    
            /* Our variables from the widget settings. */
            $title = apply_filters('widget_title', $instance['title']);
            $pagetitle = $instance['ptitle'];
            $x = $instance['x'];
            $y = $instance['y'];
            $class = $instance['class'];
    
            /* Before widget (defined by themes). */
            echo $before_widget;
    
            /* Display the widget title if one was input (before and after defined by themes). */
            if ($title)
                echo $before_title . $title . $after_title;
    
            echo DG_Random_Image(array('title'=>$pagetitle,
                                       'class'=>$class,
                                       'x'=>$x,
                                       'y'=>$y));
    
            /* After widget (defined by themes). */
            echo $after_widget;
        }
    
        /**
         * Update the widget settings.
         */
        function update($new_instance, $old_instance) {
            $instance = $old_instance;
    
            /* Strip tags for title and name to remove HTML (important for text inputs). */
            $instance['title'] = strip_tags($new_instance['title']);
            $instance['ptitle'] = strip_tags($new_instance['ptitle']);
            $instance['class'] = strip_tags($new_instance['class']);
            $instance['x'] = strip_tags((int) $new_instance['x']);
            $instance['y'] = strip_tags((int) $new_instance['y']);
    
            return $instance;
        }
    
        /**
         * Displays the widget settings controls on the widget panel.
         * Make use of the get_field_id() and get_field_name() function
         * when creating your form elements. This handles the confusing stuff.
         */
        function form($instance) {
    
            /* Set up some default widget settings. */
            $defaults = array('title'=>'Widget title',
                              'ptitle'=>'Gallery',
                              'class'=>'gallery-class',
                              'x'=>125,
                              'y'=>125);
            $instance = wp_parse_args((array) $instance, $defaults);
    ?>
    
            <!-- Widget Title: Text Input -->
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'hybrid'); ?></label>
                <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
            </p>
    
            <!-- Page title : Text Input -->
            <p>
                <label for="<?php echo $this->get_field_id('ptitle'); ?>"><?php _e('Top gallery title:', 'dg-random-image'); ?></label>
                <input id="<?php echo $this->get_field_id('ptitle'); ?>" name="<?php echo $this->get_field_name('ptitle'); ?>" value="<?php echo $instance['ptitle']; ?>" style="width:90%;" />
            </p>
    
            <!-- Class : Text Input -->
            <p>
                <label for="<?php echo $this->get_field_id('class'); ?>"><?php _e('Image class:', 'dg-random-image'); ?></label>
                <input id="<?php echo $this->get_field_id('class'); ?>" name="<?php echo $this->get_field_name('class'); ?>" value="<?php echo $instance['class']; ?>" style="width:90%;" />
            </p>
    
            <!-- Image width : Text Input -->
            <p>
                <label for="<?php echo $this->get_field_id('x'); ?>"><?php _e('Image width:', 'dg-random-image'); ?></label>
                <input id="<?php echo $this->get_field_id('x'); ?>" name="<?php echo $this->get_field_name('x'); ?>" value="<?php echo $instance['x']; ?>" style="width:90%;" />
            </p>
    
            <!-- Image height : Text Input -->
            <p>
                <label for="<?php echo $this->get_field_id('y'); ?>"><?php _e('Image height:', 'dg-random-image'); ?></label>
                <input id="<?php echo $this->get_field_id('y'); ?>" name="<?php echo $this->get_field_name('y'); ?>" value="<?php echo $instance['y']; ?>" style="width:90%;" />
            </p>
    
    <?php
        }
    
    }
    ?>
  2. now
    Member
    Posted 1 year ago #

    no one?

  3. now
    Member
    Posted 1 year ago #

    Ok,
    I solved my problem using another plugin Gallery Widget
    I needed to change the link instead of

    <a href="' .
                                    $this->getImageLink($_post->ID, $linktype, $_post->post_parent) .
                                    '"' . $_addcss . $_addrel . '><img src="' .
                                    wp_get_attachment_thumb_url($_post->ID) . '" alt="' .
                                    $_imgtitle . '" title="' .
                                    $_imgtitle . '" /></a>

    This is on line 221 and I changed the $_post->ID…
    so it is now:

    <a href="' .
                                    $this->getImageLink($_post->post_parent, $linktype, $_post->post_parent) .
                                    '"' . $_addcss . $_addrel . '><img src="' .
                                    wp_get_attachment_thumb_url($_post->ID) . '" alt="' .
                                    $_imgtitle . '" title="' .
                                    $_imgtitle . '" /></a>

    OK, it's not such a great deal but i confess, php is something i don't know enough to realize this is a very minor change…
    I guess i'm not the only one. It might help other people in my situation.
    Hasta la vista!

Topic Closed

This topic has been closed to new replies.

About this Topic