-1

I need to add sponsor images, thumbnails, in the footer of a Wordpress website. I'd like the possibility to just upload images through the native Wordpress media gallery and then having a way to select those pictures. Is there a way to do this? A way to mark some uploaded pictures so that I can select them for the footer?

Right now I'm using the image widget. It works, but I'm not totally satisfied. I would prefer to get images from the php code and customize them as I want and possibly not having to use a plugin.

  • I added some more information explaining what I'm doing right now – Carlo Mar 30 '14 at 16:05
1

In order of perferance:

Option A : Use the plugin that you mention because the footer can have it's own custom sidebar, this makes it easy to manage. If you don't like that plugin it still makes sense to use a custom sidebar for your footer, there nothing stopping you from writing your own widget or shortcode.

Option B : Create a CPT (or page), attach the images to it and then use WP Query with the paramater set to 'post_type' => 'attachment'. Please refer to http://codex.wordpress.org/Class_Reference/WP_Query for the parameters.

Option C : Query the image directly using wp_get_attachment_image_src($attachmentID, $imageSizeName);

1

I use this code to display information posts in widgets in my theme. This can easily be adapted to what you want to do. I just want to give credit to digitalraindrops, as this code was adapted from their twenty eleven business theme

First, you need to register a sidebar and the widget you will add later.

function pietergoosen_widgets_init() {
register_widget( 'pietergoosen_information_widget' );

    register_sidebar( array(
        'name'          => __( 'footersidebar', 'pietergoosen' ),
        'id'            => 'sidebar-10',
        'description'   => __( 'Footer sidebar.', 'pietergoosen' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h1 class="widget-title">',
        'after_title'   => '</h1>',
    ) );
}

add_action( 'widgets_init', 'pietergoosen_widgets_init' );

Call the widget where you want to display it in your footer

<?php if ( is_active_sidebar( 'sidebar-10' ) ) : ?>

        <?php dynamic_sidebar( 'sidebar-10' ); ?>

    <?php endif; ?> 

Now, create a file in your root and call it information-widget.php, open it and add the following code. I have removed everything that is not needed.

<?php

class pietergoosen_information_widget extends WP_Widget {

    function pietergoosen_information_widget() {
        $widget_ops = array( 'classname' => 'pietergoosen_information_widget', 'description' => __( 'Widget to display Information Posts', 'pietergoosen' ) );
        $this->WP_Widget( 'pietergoosen_information_widget', __( 'Information Posts', 'pietergoosen' ), $widget_ops );
        $this->alt_option_name = 'pietergoosen_information_widget';

        add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
        add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
        add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
    }

    function widget( $args, $instance ) {
        $cache = wp_cache_get( 'pietergoosen_information_widget', 'widget' );

        if ( !is_array( $cache ) )
            $cache = array();

        if ( ! isset( $args['widget_id'] ) )
            $args['widget_id'] = null;

        if ( isset( $cache[$args['widget_id']] ) ) {
            echo $cache[$args['widget_id']];
            return;
        }

        ob_start();
        extract( $args, EXTR_SKIP );

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base);

        if ( ! isset( $instance['postid'] ) ) 
            $postid = 0;

        if ( ! $postid = absint( $instance['postid'] ) )
            $postid = 0;

        $thumbnail = ( isset( $instance['thumbnail'] ) ) ? $instance['thumbnail'] : false; 
        $excerpt = ( isset( $instance['excerpt'] ) ) ? $instance['excerpt'] : false; 
        // Set the global arguments
        global $widget_args; 
        $widget_args=array(); 
        $widget_args['postid'] = $postid;
        $widget_args['uid'] = '-' .$args['widget_id'];

        // Display the infomation post
        echo $before_widget;
        echo $before_title;
        echo $title; // Can set this with a widget option, or omit altogether
        echo $after_title;      
        get_template_part('content','information');
        echo $after_widget;

        // Reset the post globals as this query will have stomped on it
        wp_reset_postdata();

        $cache[$args['widget_id']] = ob_get_flush();

        wp_cache_set( 'pietergoosen_information_widget', $cache, 'widget' );
    }

    /**
     * Deals with the settings when they are saved by the admin. Here is
     * where any validation should be dealt with.
     **/
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['postid'] = (int) $new_instance['postid'];
        $this->flush_widget_cache();
        $alloptions = wp_cache_get( 'alloptions', 'options' );
        if ( isset( $alloptions['pietergoosen_information_widget'] ) )
            delete_option( 'pietergoosen_information_widget' );

        return $instance;
    }

    function flush_widget_cache() {
        wp_cache_delete( 'pietergoosen_information_widget', 'widget' );
    }

    /**
     * Displays the form for this widget on the Widgets page of the WP Admin area.
     **/
    function form( $instance ) {
        $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
        $postid = isset( $instance['postid'] ) ? absint( $instance['postid'] ) : 0;
        $postlist = pietergoosen_info_post_template();
    ?>
        <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Information Post title', 'pietergoosen' ); ?></label>
        <br/>
        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>

        <p><label for="<?php echo $this->get_field_id('postid'); ?>"><?php _e( 'Information Post title', 'pietergoosen' ); ?></label>
        <br/>
        <select id="<?php echo $this->get_field_id('postid'); ?>" name="<?php echo $this->get_field_name('postid'); ?>">
    <?php 
        foreach ($postlist as $output) :
            $selected = ( $output['value'] == esc_attr($postid) ) ? ' selected = "selected" ' : '';
            $option = '<option '.$selected .'value="' . $output['value'];
            $option = $option .'">';
            $option = $option .$output['label'];
            $option = $option .'</option>';
            echo $option;
        endforeach;
    ?>
        </select></p>       


        <?php
    }
}

?>

You need a template to display these posts/images. In the widget we just created we called a template to use, get_template_part('content','information');, so create a file in your root, and call it content-information.php.

The following code need to go in there. Again I stripped what is not needed.

    <?php
    /* Widget template*/

    global $widget_args;
    $postid = $widget_args['postid'];
    $uid = $widget_args['uid'];

    $post_args = array(
        'p' => $postid,
        'post_type' => 'information',
        'post_status' => 'publish',
    );

    $post_query = new WP_Query( $post_args );

    if ( $post_query->have_posts() ) : ?>
        <div class="infopost">
        <?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>

            <article id="post-<?php the_ID(); ?><?php echo $uid; ?>" <?php post_class(); ?>>

                    <?php the_content(); ?>             

            </article>
        </div>  
        <div class="cleared"></div>

        <?php endwhile; ?>

    <?php endif; ?>

The last thing to do is to register the post type, and a way to display the available info posts to display

    function pietergoosen_info_post_widget_init() {
        $labels = array(
            'name'                  => __( 'Information Posts', 'pietergoosen' ),
            'singular_name'         => __( 'Information Post', 'pietergoosen' ),
            'add_new'               => __( 'Add new Information Post', 'pietergoosen' ),
            'add_new_item'          => __( 'Add new Information Post', 'pietergoosen' ),
            'edit_item'             => __( 'Edit Information Post', 'pietergoosen' ),
            'new_item'              => __( 'New Information Post', 'pietergoosen' ),
            'view_item'             => __( 'Check Information Posts', 'pietergoosen' ),
            'search_items'          => __( 'Searh for Information Post', 'pietergoosen' ),
            'not_found'             => __( 'No Information Posts found', 'pietergoosen' ),
            'not_found_in_trash'    => __( 'No Information Posts found in the thrash', 'pietergoosen' ),
            'parent_item_colon'     => ''
        );

        $args = array( 
            'label'                 => __( 'Information Posts', 'pietergoosen' ),
            'description'           => __( 'Information Posts for use in widgets', 'pietergoosen' ),
            'labels'                => $labels,
            'public'                => true,
            'publicly_queryable'    => true,
            'show_ui'               => true,
            'query_var'             => true,
            'rewrite'               => true,
            'capability_type'       => 'post',
            'hierarchical'          => false,
            'has_archive'           => true,
            'menu_position'         => null,
            'taxonomies'            => array( '' ),
        ); 

        register_post_type( 'information', $args );
    }

    add_action( 'init', 'pietergoosen_info_post_widget_init', 0 );

    // Display available information posts for use
    function pietergoosen_info_post_template() {

        $postlist = array();
        array_push( $postlist ,array( "value" => 0,"label" => '') );

        $args = array('post_type' => 'information','numberposts' => -1,'post_status' => 'publish' );
        $myposts = get_posts( $args );
        foreach( $myposts as $post ) {
            array_push( $postlist ,array( "value" => $post->ID,"label" => $post->post_title) );
        }
        return $postlist;
    }

You should now be able to create a CPT with the images that you need to display. As soon as that is done, you can go to the Widgets screen, drag the info post widget to your footer widget, choose the newly created info post and save it. You should now see the post with your images been displayed in your footer.

Here is a screenshot

Screenshot

You just have to sort the styling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.