在帖子或页面内的WordPress小部件

时间:2014-08-12 08:56:15

标签: wordpress

我已将此小部件安装到我的Wordpress网站,我想将它的组件复制到帖子或页面。例如:我让小部件在侧栏中显示一些文本..我想要那个文本进入帖子或我想要的页面... 谢谢 这是小部件具有的php插件的代码

    <?php

/*
    Plugin Name:  WP What's New
    Plugin URI:   http://zippersoftware.com/wp/free-stuff/wp-whats-new/
    Description:  A widget which displays new and updated WordPress pages.
    Version:      1.5.2
    Author:       Dennis M. Kowallek
    Author URI:   http://zippersoftware.com/
*/

/*
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
    Version 1.5.2
    -----------
    Changed to only include pages with `post_status` = 'publish'.

    Version 1.5.1
    -----------
    Changed to ignore pages not shown in menu.

    Version 1.5
    -----------
    Changed to not display pages scheduled for future publication.

    Version 1.4.1
    -----------
    Changed name to WP What's New

    Version 1.4
    -----------
    Changed as follows:

      1) Don't strip tags from Title.
      2) Make Title field on widget panel larger.
      3) Added class="widget-whats-new-item" to each item <a> tag.

    Version 1.3
    -----------
    Changed to exclude pages with column `redirect_url` (from Page Lists Plus plugin).

    Version 1.2
    -----------
    Changed date display to include timezone.

    Version 1.1
    -----------
    Changed title display to use 'Home' if the `post_title` is blank.

    Version 1.0
    -----------
    Initial public release.
*/

    function zsoft_whats_new($args, $widget_args = 1) {

        global $wpdb;
        extract( $args, EXTR_SKIP );
        if ( is_numeric($widget_args) )
            $widget_args = array( 'number' => $widget_args );
        $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
        extract( $widget_args, EXTR_SKIP );

        $options = get_option('whats_new_widget');
        if ( !isset($options[$number]) )
        return;

        $the_time = current_time("mysql");

        $plp = "";  // for Page Lists Plus plugin

        $sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'wp_posts' AND COLUMN_NAME = 'redirect_url'";
        $result = $wpdb->get_results($sql, ARRAY_A);
        if ( $result ) $plp = "(`link_link` = 1) AND (`redirect_url` IS NULL) AND ";

        $sql = "SELECT `ID`,`post_title`,`guid`,`post_modified_gmt` FROM `wp_posts` WHERE (%s(`post_type` = 'page') AND (`post_status` = 'publish') AND (`post_date` <= '%s') AND (`post_modified` > SUBDATE('%s', %s))) ORDER BY `post_modified` DESC LIMIT %s";

        $title = $options[$number]['title'];
        $days = $options[$number]['days'];
        $items = $options[$number]['items'];
        echo $before_widget; // start widget display code

        $sql = sprintf($sql, $plp, $the_time, $the_time, $days, $items);

        $result = $wpdb->get_results($sql, ARRAY_A);
        if ( !$result ) {
          $result[0]['post_title'] = "No recent activity";
        }

        ?>
<h3 class="widget-title widget-whats-new">
    <?=$title?></h3>
<ul>
    <?php
      $title_is_blank = false;

        foreach($result as $page){
            if ($page['post_title'] == "") {
              $title_is_blank = true;
              $page['post_title'] = "Home";
            }

            $page_title = ($page['post_title'] == 'No recent activity') ? $page['post_title'] : '<a class="widget-whats-new-item" href="' . get_permalink($page['ID']) . '">' . $page['post_title'] . '</a>';

            $page_modified_date = ($page['post_modified_gmt'] == '') ? '' : '(' . date("Y-m-d H:i:s T",strtotime($page['post_modified_gmt'])) . ')';
    ?>
    <li>
        <?=$page_title?><br />
        <?=$page_modified_date?>
    </li>
    <?php } ?>
</ul>
<?php echo $after_widget; // end widget display code

    }

    function zsoft_whats_new_control($widget_args) {

        global $wp_registered_widgets;
        static $updated = false;

        if ( is_numeric($widget_args) )
            $widget_args = array( 'number' => $widget_args );
        $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
        extract( $widget_args, EXTR_SKIP );

        $options = get_option('whats_new_widget');

        if ( !is_array($options) )
            $options = array();

        if ( !$updated && !empty($_POST['sidebar']) ) {

            $sidebar = (string) $_POST['sidebar'];
            $sidebars_widgets = wp_get_sidebars_widgets();

            if ( isset($sidebars_widgets[$sidebar]) )
                $this_sidebar =& $sidebars_widgets[$sidebar];
            else
                $this_sidebar = array();

            foreach ( (array) $this_sidebar as $_widget_id ) {
                if ( 'whats_new_widget' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
                    $widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
                    if ( !in_array( "whats_new_widget-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
                        unset($options[$widget_number]);
                }
            }

            foreach ( (array) $_POST['whats_new_widget'] as $widget_number => $whats_new_widget ) {
                if ( !isset($whats_new_widget['title_value']) && isset($options[$widget_number]) ) // user clicked cancel
                    continue;

//              $title = strip_tags(stripslashes($whats_new_widget['title_value']));
                $title = stripslashes($whats_new_widget['title_value']);
                $days = strip_tags(stripslashes($whats_new_widget['days_value']));
                $items = strip_tags(stripslashes($whats_new_widget['items_value']));
                if ( !(is_numeric($days) && ($days > 0) && ($days < 100000 ))) {
                  $days = 7;
                }
                if ( !(is_numeric($items) && ($items > 0) && ($items < 11 ))) {
                  $items = 5;
                }
                // Pact the values into an array
                $options[$widget_number] = compact( 'title', 'days', 'items' );
            }

            update_option('whats_new_widget', $options);
            $updated = true;
        }

        if ( -1 == $number ) { // if it's the first time and there are no existing values

            $title = '';
            $days = '';
            $items = '';
            $number = '%i%';

        } else { // otherwise get the existing values

            $title = attribute_escape($options[$number]['title']);
            $days = attribute_escape($options[$number]['days']);
            $items = attribute_escape($options[$number]['items']);
        }

//      print_r($options[$number]);
    ?>
<p>
    <label>Title
    </label><br />
    <input id="title_value_<?php echo $number; ?>" name="whats_new_widget[<?php echo $number; ?>][title_value]" type="text" size="64" value="
    <?=$title?>" />
</p>
<p>
    <label>Number of Days
    </label><br />
    <input id="days_value_<?php echo $number; ?>" name="whats_new_widget[<?php echo $number; ?>][days_value]" type="text" size="5" value="
    <?=$days?>" />
</p>
<p>
    <label>Number of Items
    </label><br />
    <input id="items_value_<?php echo $number; ?>" name="whats_new_widget[<?php echo $number; ?>][items_value]" type="text" size="2" value="
    <?=$items?>" />
</p>
<input type="hidden" name="whats_new_widget[<?php echo $number; ?>][submit]" value="1" />
<?php
    }

    function zsoft_whats_new_register() {
        if ( !$options = get_option('whats_new_widget') )
            $options = array();

        $widget_ops = array('classname' => 'whats_new_widget', 'description' => __('Displays new and updated WordPress pages'));
        $control_ops = array('width' => 400, 'height' => 350, 'id_base' => 'whats_new_widget');

        $name = __('WP What\'s New');

        $id = false;

        foreach ( (array) array_keys($options) as $o ) {

            if ( !isset( $options[$o]['title'] ) )
                continue;

            $id = "whats_new_widget-$o";
            wp_register_sidebar_widget($id, $name, 'zsoft_whats_new', $widget_ops, array( 'number' => $o ));
            wp_register_widget_control($id, $name, 'zsoft_whats_new_control', $control_ops, array( 'number' => $o ));
        }

        if ( !$id ) {
            wp_register_sidebar_widget( 'whats_new_widget-1', $name, 'zsoft_whats_new', $widget_ops, array( 'number' => -1 ) );
            wp_register_widget_control( 'whats_new_widget-1', $name, 'zsoft_whats_new_control', $control_ops, array( 'number' => -1 ) );
        }
    }

add_action('init', zsoft_whats_new_register, 1);
?>

1 个答案:

答案 0 :(得分:0)

1)我建议将您希望在帖子或页面中显示的信息传递给函数。 你可以在这样的帖子中使用这个功能

This is an example which mentions code within a paragraph,
namely the functions <code>wp_title()</code>, 
<code>wp_content()</code> and <code>wp_footer()</code>, 
which are very useful in WordPress.

在此处查看完整的详细信息http://codex.wordpress.org/Writing_Code_in_Your_Posts

2)或者您也可以使用iframe方法加载信息。