如何在页脚创建自定义图片库?

时间:2014-03-30 作者:Carlo

我需要添加赞助商图片,缩略图,在一个Wordpress网站的页脚。我想通过本地Wordpress媒体库上传图片,然后有办法选择这些图片。有没有办法做到这一点?一种方法来标记一些上传的图片,以便我可以选择它们作为页脚?

现在我正在使用image widget. 这是可行的,但我并不完全满意。我更喜欢从php代码中获取图像,并根据需要对其进行自定义,可能不必使用插件。

2 个回复
SO网友:Wyck

按性能顺序:

Option A : 使用您提到的插件,因为页脚可以有自己的自定义侧栏,这使其易于管理。如果你不喜欢这个插件,那么为你的页脚使用一个自定义侧边栏还是有意义的,没有什么可以阻止你编写自己的小部件或短代码。

Option B : 创建一个CPT(或页面),将图像附加到其中,然后使用WP Query 参数设置为\'post_type\' => \'attachment\'. 请参阅http://codex.wordpress.org/Class_Reference/WP_Query 对于参数。

Option C : 使用直接查询图像wp_get_attachment_image_src($attachmentID, $imageSizeName);

SO网友:Pieter Goosen

我使用此代码在我的主题中的小部件中显示信息帖子。这可以很容易地适应您想要做的事情。我只是想赞扬digitalraindrops, 因为此代码是根据其2011年商业主题改编的

首先,您需要注册一个侧栏和稍后添加的小部件。

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\' );
调用要在页脚中显示它的小部件

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

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

    <?php endif; ?> 
现在,在根目录中创建一个文件并调用它information-widget.php, 打开它并添加以下代码。我已经移除了所有不需要的东西。

<?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
    }
}

?>
您需要一个模板来显示这些帖子/图像。在我们刚刚创建的小部件中,我们调用了一个模板来使用,get_template_part(\'content\',\'information\');, 因此,在根目录中创建一个文件,并将其称为内容信息。php。

下面的代码需要放入其中。我再次剥离了不需要的东西。

    <?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;
    }
现在,您应该能够使用需要显示的图像创建CPT。完成后,您可以转到Widgets屏幕,将info post小部件拖动到页脚小部件,选择新创建的info post并保存它。现在,您应该可以在页脚中看到包含图像的帖子。

这是一个屏幕截图

Screenshot

您只需对样式进行排序

结束

相关推荐

根据URL更改sidebar.php和footer.php

几天前,我提出了一个类似的问题,但也许我的问题有点不正确。我想要我的页脚。php和侧栏。php将根据URL进行更改。如果URL包含/ru,则显示ru页脚。php else显示默认页脚(footer.php)。侧边栏也应该如此。如果可能的话,你能一步一步地引导我吗。非常感谢。