我使用此代码在我的主题中的小部件中显示信息帖子。这可以很容易地适应您想要做的事情。我只是想赞扬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并保存它。现在,您应该可以在页脚中看到包含图像的帖子。
这是一个屏幕截图
您只需对样式进行排序