Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Need code turned to into widget WordPress

  • SOLVED

Hello,
I just need the code below turned into a widget that users can drag into their sidebar in the Widgets area. The widget should be available when users activate the theme. I would also like the user to be able to enter their own title where it currently says "Company News".



<div class="widget">
<h2>Company News</h2>
<ul>
<?php query_posts('showposts=4&category_name='.get_option('freshstart_blog_cat')); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span class="date">Posted on <?php the_date(); ?> by <?php the_author(); ?></span>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--widget-->

Answers (1)

2010-12-05

Nick Parsons answers:

Add this to functions.php:

wp_register_sidebar_widget('news','Company News','show_news');
function show_news(){

<div class="widget">
<h2>Company News</h2>
<ul>
<?php query_posts('showposts=4&category_name='.get_option('freshstart_blog_cat')); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span class="date">Posted on <?php the_date(); ?> by <?php the_author(); ?></span>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--widget-->
}


Nick Parsons comments:

Oops, that should be:

<?php
wp_register_sidebar_widget('news','Company News','show_news');
function show_news(){
?>

<div class="widget">
<h2>Company News</h2>
<ul>
<?php query_posts('showposts=4&category_name='.get_option('freshstart_blog_cat')); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span class="date">Posted on <?php the_date(); ?> by <?php the_author(); ?></span>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--widget-->
<?php }?>


Mike McAlister comments:

Hi Nick,
See the updated description. I left one request out.


Nick Parsons comments:

No problem. Here's the modified code:



<?php
wp_register_sidebar_widget('news','News Widget','show_news');
register_widget_control('news','news_control');
function news_control(){
echo "<strong>Title:</strong><input type='text' name='news-title' value='" .get_option('news_title'). "'/>";
if(isset($_POST['news-title'])) update_option('news_title',$_POST['news-title']);
}
function show_news(){ ?>
<div class="widget">
<h2><?php echo get_option('news_title'); ?></h2>
<ul>
<?php query_posts('showposts=4&category_name='.get_option('freshstart_blog_cat')); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span class="date">Posted on <?php the_date(); ?> by <?php the_author(); ?></span>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--widget-->
<?php } ?>


Mike McAlister comments:

Perfect. Thank you!