如何添加和显示类别图像

时间:2016-06-06 作者:Jahangir Khattak

我创建了一个WordPress插件来创建一个小部件区域来显示类别名称(帖子的总nmbr)和描述。现在,我想在类别名称之后显示类别图像,在我想显示类别描述的图像之后显示类别图像。虽然类别名称和描述显示成功,但我只想了解如何将功能图像添加到类别以及如何在类别描述小部件区域显示的完整代码。

以下是我的完整Category Description Widget Area 密码

<?php
/*
Plugin Name: My Category Description Widget Plugin
Plugin URI: http://mkrdip.me/category-posts-widget
Description: Adds a widget that shows all categories along with their description.
Author: Muhammad Jahangir
Version: 1.0
Author URI: http://facebook.com/jahangirKhattakPk
*/
class categories_description extends WP_Widget
{
    function __construct()
    {
        $widget_ops = array( 
            \'classname\' => \'categoryDescription widget\',
            \'description\' => \'Show all categories with their description.\',
        );
        parent::__construct( \'categories_description\', \'My Category Description\', $widget_ops );
    }
    // end __construct()

    public function form( $instance ) {
        $title = ! empty( $instance[\'title\'] ) ? $instance[\'title\'] : __( \'New title\', \'text_domain\' );
        ?>
        <p>
        <label for="<?php echo esc_attr( $this->get_field_id( \'title\' ) ); ?>"><?php _e( esc_attr( \'Title:\' ) ); ?></label> 
        <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>
        <?php 
    }
    // End form function

// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
    return $instance;
}
// End update function
    function widget($args,$instance){
           echo $args[\'before_widget\'];
            if ( ! empty( $instance[\'title\'] ) )
            {


        ?>
        <div class="categoriesDescription bgWhite">
                <div class="container innerContainer">
                    <h2 class="widgetTitle"><?php echo apply_filters( \'widget_title\', $instance[\'title\'] ); } ?></h2>
                        <?php $categories = get_categories();
                        foreach ($categories as $cat)
                            { 

                                $cat_name = $cat->name;
                                $CategoryID = get_cat_ID( $cat_name );
                                $totalPosts = $cat->count;
                                $currentcatname = $cat_name;



                                ?>

                              <div class="category-image">

                              </div>

                                <div class="singleCategory col-lg-4">
                                    <a href="<?php echo get_category_link( $cat ); ?>" class="uppercase categoryTitle">
                                        <?php echo $cat_name; ?></a>
                                        <span class="numberOfPosts">
                                        <?php 
                                            echo \'( \'.$totalPosts.\' )\'; 
                                        ?>
                                </span>                         
                                <div class="CategoryImg"><?php echo $cat->term_icon; ?>
                                    // here I want to show category feature image...
                                <div class="my-image">
                                <?php

 ?>
                                <header class="archive-header <?php if ($category_image==true) echo \'category-image\'; ?>">

                                 </div>

                                </div>
                                <p class="CategoryDescription"><?php echo $cat->description; ?></p>
                                <p><?php echo $CategoryID; ?></p>
                               </div>
                            <?php 
                            } ?>
                    <div class="clearfix"></div>
                </div>
        </div>
    </div>
</div>
        <?php

    }
    // End widget function
}
//end categories_description class
    add_action(\'widgets_init\', function() {
        register_widget(\'categories_description\');
    })
?>

2 个回复
SO网友:Mark Kaplun

一般来说,对于类别或其他类型的术语,没有“特色图片”这样的东西。最简单的方法是与支持此类功能的现有插件和主题集成,并/或提供一个易于使用的过滤器,供网站所有者自定义从何处获取信息。

SO网友:majick

我会说注册一个自定义的帖子类型,例如。cat_image, 因此,可以使用现有的特色图像元盒将图像上载到帖子。

您可以使用同步数据wp_insert_post 将小部件ID与自定义post slug匹配。。?

// to match widget to an existing custom post
// similar to get_post but can use slug (if no post parents)
$slug = $instance[\'id\']; // not sure on this?
$post = get_page_by_path($slug,OBJECT,\'cat_image\');

if (!$post) {
    // create fresh post from widget instance data
    $args = array(\'post_type\' => \'cat_image\'); // ...and all the rest
    $post = wp_insert_post($args);
}

$thumbnail_id = get_post_meta( $post->ID, \'_thumbnail_id\', true );  
$postthumbnailhtml = _wp_post_thumbnail_html( $thumbnail_id, $post->ID );
这将为您提供链接到帖子ID的upload元素。。。(此外,您可能需要确保wp_enqueue_media 是否已在widget页面排队?)

无论如何,这是一个建议,您仍然需要围绕它构建一个表单元素HTML元盒,然后看看会发生什么。。。还没有时间测试它,希望输出足够容易访问并作为链接自定义帖子的附件插入。