最新/最近发布的帖子小工具标题链接

时间:2012-09-26 作者:Kat

我已经为此挣扎了几个小时了。。。

我想添加一个链接到我最近发布的小部件标题。

我尝试了论坛上给出的许多选项,但似乎没有任何效果。

尝试在周围和中间添加链接标记echo $before_title . $title . $after_title; 但肯定是哪里出了问题。

请看一下小部件代码,并告诉我是否可以添加小部件标题的链接?

<?php
/**
 * Widget Name: Recent Posts with Excerpts Widget
 * Description: A widget that allows to display a recent posts with excerpts and date and author info (optional).
 * Version: 0.1
 *
 */

/**
 * Add function to widgets_init that\'ll load our widget.
 * @since 0.1
 */
add_action( \'widgets_init\', \'latest_posts_load_widgets\' );

/**
 * Register our widget.
 * \'Latest_Posts_Widget\' is the widget class used below.
 *
 * @since 0.1
 */
function latest_posts_load_widgets() {
    register_widget( \'Latest_Posts_Widget\' );
}

/**
 * Custom Category Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Latest_Posts_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function Latest_Posts_Widget() {
        /* Widget settings. */
        $widget_ops = array( \'classname\' => \'widget-latest-posts\', \'description\' => esc_html__(\'The most recent posts with teaser text\', \'purevision\') );

        /* Widget control settings. */
        $control_ops = array( \'width\' => 150, \'height\' => 350, \'id_base\' => \'latest-posts-widget\' );

        /* Create the widget. */
        $this->WP_Widget( \'latest-posts-widget\', esc_html__(\'Digi - Recent Posts\', \'purevision\'), $widget_ops, $control_ops );
}

/**
 * How to display the widget on the screen.
 */
function widget( $args, $instance ) {
    extract( $args );

    /* Our variables from the widget settings. */
    $title = apply_filters(\'widget_title\', $instance[\'title\'] );
    $category_id = $instance[\'category_id\'];
    $num_posts = absint( $instance[\'num_posts\'] );
    $post_offset =  absint( $instance[\'post_offset\'] );

    /* Before widget (defined by themes). */
    echo $before_widget;

    /* Display the widget title if one was input (before and after defined by themes). */

    if ( $title ) echo $before_title . $title . $after_title;

    /* Display the Latest Posts accordinly... */
    echo \'<div id="widget-latest-posts">\';
    $cats_to_include = ( $category_id ) ? "cat={$category_id}&": \'\';
    $num_posts_query = new WP_Query( "{$cats_to_include}showposts={$num_posts}&offset={$post_offset}" );
    if( $num_posts_query->have_posts()) : ?>
        <div class="latest_posts">
        <ul>
                    <?php while( $num_posts_query->have_posts()) : $num_posts_query->the_post(); update_post_caches($posts); ?>
    <?php
    $thumb = get_post_thumbnail_id(); 
    $postimage = vt_resize($thumb, \'\', 58, 58, true );
    ?>
            <li>
            <div class="pop-wrap">

            <h3><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>

            </div>
            </li>
    <?php endwhile; ?>
        </ul>
        </div><!-- end widget_recent_posts -->
    <?php endif;
    echo \'</div><!-- end widget-latest-posts -->\';

    /* After widget (defined by themes). */
    echo $after_widget;
}

/**
 * Update the widget settings.
 */
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    /* Strip tags for title and name to remove HTML (important for text inputs). */
    $instance[\'title\'] = $new_instance[\'title\'];
    $instance[\'num_posts\'] = strip_tags( $new_instance[\'num_posts\'] );
    $instance[\'post_offset\'] = strip_tags( $new_instance[\'post_offset\'] );
    /* No need to strip tags for dropdowns and checkboxes. */
    $instance[\'category_id\'] = $new_instance[\'category_id\'];

    return $instance;
}

/**
 * Displays the widget settings controls on the widget panel.
 * Make use of the get_field_id() and get_field_name() function
 * when creating your form elements. This handles the confusing stuff.
 */
function form( $instance ) {

    /* Set up some default widget settings. */
    $defaults = array( \'title\' => esc_html__(\'Latest Posts\', \'purevision\'), \'category_id\' => \'\', \'num_posts\' => 3, \'post_offset\' => 0);
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>

    <!-- Widget Title: Text Input -->
    <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php esc_html_e(\'Title:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'title\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" class="widefat" />
    </p>

    <!-- Show Categories -->
    <p>
        <label for="<?php echo $this->get_field_id( \'category_id\' ); ?>"><?php esc_html_e(\'Pick a specific category:\', \'purevision\'); ?></label>
        <?php wp_dropdown_categories(\'show_option_all=All&hierarchical=1&orderby=name&selected=\'.$instance[\'category_id\'].\'&name=\'.$this->get_field_name( \'category_id\' ).\'&class=widefat\'); ?>
    </p>

    <!-- Number of Posts -->
    <p>
        <label for="<?php echo $this->get_field_id( \'num_posts\' ); ?>"><?php esc_html_e(\'Number of posts to show:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'num_posts\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'num_posts\' ); ?>" value="<?php echo $instance[\'num_posts\']; ?>" size="2" maxlength="2" />
        <br />
        <small><?php esc_html_e(\'(at most 15)\', \'purevision\'); ?></small>
    </p>

    <!-- Post Offset -->
    <p>
        <label for="<?php echo $this->get_field_id( \'post_offset\' ); ?>"><?php esc_html_e(\'Number of posts to skip:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'post_offset\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'post_offset\' ); ?>" value="<?php echo $instance[\'post_offset\']; ?>" size="2" maxlength="2" />
        <br />
        <small><?php esc_html_e(\'(offset from latest)\', \'purevision\'); ?></small>
    </p>

1 个回复
SO网友:Dave Hunt

我已经更新了您的小部件代码,以包含一个名为“标题链接”的新字段。这与一些附加标记一起,将围绕小部件标题创建一个链接。您可能需要向标题链接字段添加更好的验证。

<?php
/**
 * Widget Name: Recent Posts with Excerpts Widget
 * Description: A widget that allows to display a recent posts with excerpts and date and author info (optional).
 * Version: 0.1
 *
 */

/**
 * Add function to widgets_init that\'ll load our widget.
 * @since 0.1
 */
add_action( \'widgets_init\', \'latest_posts_load_widgets\' );

/**
 * Register our widget.
 * \'Latest_Posts_Widget\' is the widget class used below.
 *
 * @since 0.1
 */
function latest_posts_load_widgets() {
    register_widget( \'Latest_Posts_Widget\' );
}

/**
 * Custom Category Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Latest_Posts_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function Latest_Posts_Widget() {
        /* Widget settings. */
        $widget_ops = array( \'classname\' => \'widget-latest-posts\', \'description\' => esc_html__(\'The most recent posts with teaser text\', \'purevision\') );

        /* Widget control settings. */
        $control_ops = array( \'width\' => 150, \'height\' => 350, \'id_base\' => \'latest-posts-widget\' );

        /* Create the widget. */
        $this->WP_Widget( \'latest-posts-widget\', esc_html__(\'Digi - Recent Posts\', \'purevision\'), $widget_ops, $control_ops );
}

/**
 * How to display the widget on the screen.
 */
function widget( $args, $instance ) {
    extract( $args );

    /* Our variables from the widget settings. */
    $title = apply_filters(\'widget_title\', $instance[\'title\'] );
    $titlelink = $instance[\'titlelink\'];
    $category_id = $instance[\'category_id\'];
    $num_posts = absint( $instance[\'num_posts\'] );
    $post_offset =  absint( $instance[\'post_offset\'] );

    /* Before widget (defined by themes). */
    echo $before_widget;

    /* Display the widget title if one was input (before and after defined by themes). */

    if ( $title ) {
          echo $before_title;
          if ( $titlelink ) { 
                echo \'<a href="\' . $titlelink . \'">\';
          }
          echo $title;
          if ( $titlelink ) {
                echo \'</a>\';
          }
          echo $after_title;
    }

    /* Display the Latest Posts accordinly... */
    echo \'<div id="widget-latest-posts">\';
    $cats_to_include = ( $category_id ) ? "cat={$category_id}&": \'\';
    $num_posts_query = new WP_Query( "{$cats_to_include}showposts={$num_posts}&offset={$post_offset}" );
    if( $num_posts_query->have_posts()) : ?>
        <div class="latest_posts">
        <ul>
                    <?php while( $num_posts_query->have_posts()) : $num_posts_query->the_post(); update_post_caches($posts); ?>
    <?php
    $thumb = get_post_thumbnail_id(); 
    $postimage = vt_resize($thumb, \'\', 58, 58, true );
    ?>
            <li>
            <div class="pop-wrap">

            <h3><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>

            </div>
            </li>
    <?php endwhile; ?>
        </ul>
        </div><!-- end widget_recent_posts -->
    <?php endif;
    echo \'</div><!-- end widget-latest-posts -->\';

    /* After widget (defined by themes). */
    echo $after_widget;
}

/**
 * Update the widget settings.
 */
function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    /* Strip tags for title and name to remove HTML (important for text inputs). */
    $instance[\'title\'] = $new_instance[\'title\'];
    $instance[\'titlelink\'] = $new_instance[\'titlelink\'];
    $instance[\'num_posts\'] = strip_tags( $new_instance[\'num_posts\'] );
    $instance[\'post_offset\'] = strip_tags( $new_instance[\'post_offset\'] );
    /* No need to strip tags for dropdowns and checkboxes. */
    $instance[\'category_id\'] = $new_instance[\'category_id\'];

    return $instance;
}

/**
 * Displays the widget settings controls on the widget panel.
 * Make use of the get_field_id() and get_field_name() function
 * when creating your form elements. This handles the confusing stuff.
 */
function form( $instance ) {

    /* Set up some default widget settings. */
    $defaults = array( \'title\' => esc_html__(\'Latest Posts\', \'purevision\'), \'category_id\' => \'\', \'num_posts\' => 3, \'post_offset\' => 0);
    $instance = wp_parse_args( (array) $instance, $defaults ); ?>

    <!-- Widget Title: Text Input -->
    <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php esc_html_e(\'Title:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'title\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" class="widefat" />
    </p>

     <!-- Widget Title Link: Text Input -->
    <p>
        <label for="<?php echo $this->get_field_id( \'titlelink ); ?>"><?php esc_html_e(\'Title Link:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'titlelink\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'titlelink\' ); ?>" value="<?php echo $instance[\'titlelink\']; ?>" class="widefat" />
    </p>

    <!-- Show Categories -->
    <p>
        <label for="<?php echo $this->get_field_id( \'category_id\' ); ?>"><?php esc_html_e(\'Pick a specific category:\', \'purevision\'); ?></label>
        <?php wp_dropdown_categories(\'show_option_all=All&hierarchical=1&orderby=name&selected=\'.$instance[\'category_id\'].\'&name=\'.$this->get_field_name( \'category_id\' ).\'&class=widefat\'); ?>
    </p>

    <!-- Number of Posts -->
    <p>
        <label for="<?php echo $this->get_field_id( \'num_posts\' ); ?>"><?php esc_html_e(\'Number of posts to show:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'num_posts\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'num_posts\' ); ?>" value="<?php echo $instance[\'num_posts\']; ?>" size="2" maxlength="2" />
        <br />
        <small><?php esc_html_e(\'(at most 15)\', \'purevision\'); ?></small>
    </p>

    <!-- Post Offset -->
    <p>
        <label for="<?php echo $this->get_field_id( \'post_offset\' ); ?>"><?php esc_html_e(\'Number of posts to skip:\', \'purevision\'); ?></label>
        <input id="<?php echo $this->get_field_id( \'post_offset\' ); ?>" type="text" name="<?php echo $this->get_field_name( \'post_offset\' ); ?>" value="<?php echo $instance[\'post_offset\']; ?>" size="2" maxlength="2" />
        <br />
        <small><?php esc_html_e(\'(offset from latest)\', \'purevision\'); ?></small>
    </p>
由于您现在正在标题标记中使用链接,因此必须添加其他样式:

  .widget h3 a {
        // Same colour and style as h3 //
  }

结束

相关推荐

Widgets in PHP files?

有没有可能让Wordpress上的每个小部件都以不同的方式运行。php文件我有一个包含12个元素的页面,我想让每个元素都成为一个小部件,以便以后更容易管理/编辑它们,但如果它来自php页面,而不是来自管理面板上的代码块,则会更好。