WordPress.org

Ready to get started?Download WordPress

Forums

problem creating widget (2 posts)

  1. ergate
    Member
    Posted 8 years ago #

    Hi, I am trying to create a widget that will let me display the recent posts. I know this is already built in, but I want to customize what is displayed. So I thought I would make a widget that would display recent posts the way I want to see them. I have made the widget, but it does not show up in sidebar widgets. (Yes I did upload the widget, activated them etc etc) There is something wrong with my code but I dont know what to look for to fix it. I based this on the recent entries code in the widgets plugin.
    My code is as follows:

    <?php

    // This gets called at the plugins_loaded action
    function widget_recentposts_init() {

    // Check for the required API functions
    if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
    return;

    // This saves options and prints the widget's config form.
    function widget_recentposts_control() {
    $options = $newoptions = get_option('widget_recentposts');
    if ( $_POST['recentposts-submit'] ) {
    $newoptions['title'] = strip_tags(stripslashes($_POST['recentposts-title']));
    }
    if ( $options != $newoptions ) {
    $options = $newoptions;
    update_option('widget_recentposts', $options);
    }
    ?>
    <div style="text-align:right">
    <label for="recentposts-title" style="line-height:35px;display:block;">Widget title: <input type="text" id="recentposts-title" name="recentposts-title" value="<?php echo htmlspecialchars($options['title']); ?>" /></label>
    <input type="hidden" name="recentposts-submit" id="recentposts-submit" value="1" />
    </div>
    <?php
    }

    // This prints the widget
    function widget_recentposts($args) {
    extract($args);
    $title = __('Recent Posts');
    $r = new WP_Query('showposts=10');
    if ($r->have_posts()) :
    ?>
    <?php echo $before_widget; ?>
    <?php echo $before_title . $title . $after_title; ?>

    <?php while ($r->have_posts()) : $r->the_post(); ?>
    <?php the_excerpt(''); ?>
    <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>

    <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
    <?php endwhile; ?>

    <?php echo $after_widget; ?>
    <?php
    }

    // Tell Dynamic Sidebar about our new widget and its control
    register_sidebar_widget('Recent Posts', 'widget_recentposts');
    register_widget_control('Recent Posts', 'widge_recentposts_control');

    }

    // Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
    add_action('plugins_loaded', 'widget_recentposts_init');

    ?>

  2. ergate
    Member
    Posted 8 years ago #

    Ok, I forgot one thing, to activate this widget in the plugin menu. That being done, I am now recieving this error
    Parse error: parse error, unexpected '}' in /home/httpd/xxx/xxx/httpdocs/alog/wp-content/plugins/widgets/recent_posts.php on line 57

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.