WordPress小部件:显示单个帖子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WordPress小部件:显示单个帖子相关的知识,希望对你有一定的参考价值。
A simple widget for displaying a single post in a dynamic sidebar. Originally developed to accommodate a WP user who wanted full control of which posts were displayed and what order they were on a certain page.
<?php /* Plugin Name: Single Post Plugin URI: http://ch4ze.com Description: Define a widget for displaying a single post Author: Chase Crawford Author URI: http://ch4ze.com Version: 0.1 */ class singlePostWidget extends WP_Widget { function singlePostWidget() { $this->WP_Widget('singlePostWidget', 'Single Post', $widget_ops); } function form($instance) { $pid = $instance['pid']; if($pid == '') { 'post_type' => 'post' ); $the_query = new WP_Query($args); print '<select name="' . $this->get_field_name('pid') . '"><option value ="">Choose a post to display</option>'; while ( $the_query->have_posts() ) : $the_query->the_post(); print '<option value="' . get_the_ID() . '">' . get_the_title() . '</option>'; endwhile; print '</select>'; } else { $post = query_posts('p=' . $pid); while ( have_posts() ) : the_post(); print "<strong>" . get_the_title() . "</strong>"; endwhile; } } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['pid'] = $new_instance['pid']; return $instance; } function widget($args, $instance) { $post = query_posts('p=' . $instance['pid']); while ( have_posts() ) : the_post(); /*======================================================= add template code to display your post here =======================================================*/ endwhile; } }
以上是关于WordPress小部件:显示单个帖子的主要内容,如果未能解决你的问题,请参考以下文章