PHP WordPress小部件:显示单个帖子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP WordPress小部件:显示单个帖子相关的知识,希望对你有一定的参考价值。

<?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()
  {
    $widget_ops = array('classname' => 'singlePostWidget', 'description' => 'Displays a single post' );
    $this->WP_Widget('singlePostWidget', 'Single Post', $widget_ops);
  }
 
  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'pid' => '' ) );
    $pid = $instance['pid'];
    if($pid == '') {

    $args = array(
	    '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)
  {
    extract($args, EXTR_SKIP);
 
    $post = query_posts('p=' . $instance['pid']);
    while ( have_posts() ) : the_post();

      /*=======================================================
           add template code to display your post here
      =======================================================*/

    endwhile;


  }
 
}
add_action( 'widgets_init', create_function('', 'return register_widget("singlePostWidget");') );?>

以上是关于PHP WordPress小部件:显示单个帖子的主要内容,如果未能解决你的问题,请参考以下文章

PHP 将帖子计数移动到WordPress的存档和类别小部件的链接中

Wordpress 自定义演示侧边栏未出现在帖子上

php EMPTY WIDGET用于在wordpress中创建单个小部件的插件代码。

php 在单个帖子#wordpress上显示自定义类别

PHP 单个Wordpress循环显示最新的夫妇帖子和其余的标题

jQuery Pagination Wordpress 小部件后 Dom Css 丢失