ViewFromTheBox Posted June 12, 2012 Share Posted June 12, 2012 I'm creating a custom widget for my wordpress theme that displays posts from a category that the user chooses. When I try to run it though, I get the following error: "Parse error: syntax error, unexpected ';', expecting T_FUNCTION in C:\xampp\htdocs\wordpress\wp-content\themes\sports\tools\categorywidget.php on line 70". Here's the code: <?php /* Plugin Name: Sports Theme Category Widget Description: This widget allows you to display posts from a category in any of the sidebar areas. Author: Jacob Martella Author URI: Version: 1.1 */ add_action('widgets_init', create_function('', "register_widget('sports_category');")); class sports_category extends WP_Widget { function sports_category() { $widget_ops = array( 'classname' => 'Sports Category Widget', 'description' => 'Use this widget to display posts from a category in any of your sidebars.' ); $control_ops = array( 'width' => 500, 'height' => 250, 'id_base' => 'category' ); $this->WP_Widget( 'category', 'sports_category', $widget_ops, $control_ops ); } function widget($args, $instance) { extract($args); $categoryslug = cat_id_to_slug($instance['category']); $categoryname = cat_id_to_name($instance['category']); ?> <div id="widget-wrap"> <?php query_posts('cat=' . $instance['category'] . 'showposts=1&orderby=date' ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div id="category-widget-featured-photo"> <?php the_post_thumbnail( 'cat-widget' ); ?> </div> <h3 class="category-widget-main-headline"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h3> <p class="postmetadata"> Written By: <?php the_author_posts_link(); ?>, <?php $key="jobtitle"; get_post_meta($post->ID, $key, true); ?> </p> <p class="story"> <?php the_content(); ?> </p> <p class="postmetadata"> <?php the_time('F j, Y'); ?> • <?php comments_popup_link('0 comments', '1 comment', '% comments'); ?><?php edit_post_link('Edit', ' • ', ''); ?> </p> <?php endwhile; ?> <?php endif; ?> <?php rewind_query(); ?> <p class="category-head"> <a href="/<?php echo $categoryslug; ?>">Recent <?php echo $categoryname; ?> Stories</a> </p> <?php query_posts('cat=' . $instance['category'] . 'showposts=3&orderby=date&offset=0' ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <p class="category-widget-headlines"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php the_time('F j, Y'); ?> </p> <p class="viewall"> <a href="<?php echo cat_id_to_slug($instance['category']); ?>">View All</a> </p> <?php endwhile; ?> <?php endif; ?> <?php } ?> <!--End Category Widget Display--> <?php function form($instance) { $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'category' => '' ) ); $title = $instance['title']; $category = $instance['category']; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = $new_instance['title']; $instance['category'] = $new_instance['category']; return $instance; } ?> <div style="float:left;width:230px;margin-right:20px;border-right:1px solid #aaaaaa;padding-right:10px;"> <p style="font-weight:bold;text-decoration:underline;">Widget Content</p> <p>Select your category<br /> <?php wp_dropdown_categories(array('selected' => $instance['category'], 'name' => $this->get_field_name( 'category' ), 'orderby' => 'Name', 'hierarchical' => 1, 'show_option_none' => __("None", 'studiopress'), 'hide_empty' => '0' )); ?> </p> <?php $categorytitle = cat_id_to_name($instance['category']); ?><input type="hidden" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $categorytitle; ?>" /> <?php } ?> Thanks in advance for any help offered. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 Look at the lines around that one and make sure syntax is correct. In the future, post just the relevant section, not all the code. While this is not WP specific, you might have better luck in the third party forum because this is so heavy on WP code. Did you make this widget? Quote Link to comment Share on other sites More sharing options...
ViewFromTheBox Posted June 12, 2012 Author Share Posted June 12, 2012 I'm fairly confident that the syntax is correct; however, I am really new to web development and this is part of my first ever theme, so I may be overlooking something. Yes, I did make this widget, but it's based off of a similar widget which is why I'm puzzled by it. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2012 Share Posted June 12, 2012 Edit: never mind what I posted here, let me continue to investigate... Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 I thought your answer was correct PFMaBiSmAd Quote Link to comment Share on other sites More sharing options...
ViewFromTheBox Posted June 12, 2012 Author Share Posted June 12, 2012 Okay, that seems to make sense to me. So should I use the echo to display the html tags? I'm also using this tutorial as well: http://www.makeuseof.com/tag/how-to-create-wordpress-widgets/ By the way, you did seem to be making sense. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 12, 2012 Share Posted June 12, 2012 You can't just echo something within a class either. IE, this won't work Class Bob{ var $name = 'bob'; function sayHi(){ echo 'hi'; } echo 'this is bob'; } Everything inside it needs to either be a function or a property definition, maybe you meant to put that stuff in the constructor? Or maybe just at the end of widget() looking at the link. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2012 Share Posted June 12, 2012 The error seems to have something to do with all the opening and closing php tags that are at the start and end of the function definitions and at the closing } for the class definition. It's probably something like having white-space between one closing ?> tag and an opening <?php tag right before the closing } of a function and of the class definition itself. I would personally eliminate all those excessive opening and closing php tags. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2012 Share Posted June 12, 2012 The short-answer seems to be - after the closing } of a class function definition, don't put a closing ?> tag (or any html comments or in-line html/php.) The only thing that should be after the closing } of a class function definition are php comments, another class function definition, or the final } brace of the class definition. I'm thinking this is because you cannot break up the class definition into parts. The first closing ?> tag that is in the 'body/root' of the class definition, closes the class definition. Quote Link to comment Share on other sites More sharing options...
ViewFromTheBox Posted June 12, 2012 Author Share Posted June 12, 2012 I deleted the closing ?> tag but I get the same message just a few lines further down to the next ?>. I also went back to the tutorial, and I'm thinking that I should flip widget options (form($instance)) and put that in the top possibly. At this point, I'm willing to try anything. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 12, 2012 Share Posted June 12, 2012 The error seems to have something to do with all the opening and closing php tags that are at the start and end of the function definitions And at the end of a function definition it goes back into the class definition. And like you said, you cannot break up the class definition into parts. Summarized the code is /* comment */ add_action(); class sports_category { function sports_category() { } function widget() { ?> html } ?> html function form() { } function update() { } ?> html } ?> Quote Link to comment Share on other sites More sharing options...
ViewFromTheBox Posted June 12, 2012 Author Share Posted June 12, 2012 So I guess the question then becomes where do I put the html stuff? Both the widget I'm basing this off of and the tutorial I'm using have some html tags outside of the php. Quote Link to comment Share on other sites More sharing options...
ViewFromTheBox Posted June 13, 2012 Author Share Posted June 13, 2012 So, I looked up another tutorial (http://www.doitwithwp.com/how-to-create-wordpress-widget-step-by-step/) and followed it basically verbatim and got it to work perfectly. Thanks for your help though. I learned a lot about PHP today. Quote Link to comment Share on other sites More sharing options...
antmeeks Posted June 13, 2012 Share Posted June 13, 2012 This is why I hate Wordpress... it's promotion of spaghetti-style coding. Ugh, it hurts my eyes to see all that php inline with html. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.