Widget commands
-
I have been tweaking a theme that I like, but I have come to a point that I need someone’s help. Currently, my sidebar is custom made by the original designer and shows three boxes: tags, recent comments, and “Popular” posts (popular is defined by how many comments are on a given post).
I want to change the “Popular” box to show the most recent posts published in order. In the theme editor, the sidebar file has a div id – “Popular”, which I believe is getting instructions from the themes function file. From the themes function file, I found this:
<?php
function popularPosts($num) {
global $wpdb;$posts = $wpdb->get_results(“SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num”);
foreach ($posts as $post) {
setup_postdata($post);
$id = $post->ID;
$title = $post->post_title;
$count = $post->comment_count;if ($count != 0) {
$popular .= ‘- ‘;
$popular .= ‘‘ . $title . ‘ ‘;
$popular .= ‘
‘;
}
}
return $popular;Can anyone tell me what I need to change to make this function collect and display the most recent posts? Thanks!
- ‘;
-
This is UNTESTED, but should be close:
<?php function popularPosts($num) { global $wpdb; $posts = $wpdb->get_results("SELECT ID, post_title, post_date FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND post_date <= NOW() ORDER BY post_date DESC LIMIT 0 , $num"); foreach ($posts as $post) { setup_postdata($post); $id = $post->ID; $title = $post->post_title; $popular .= ' '; $popular .= '' . $title . ' '; $popular .= ' '; } return $popular;It will need some modification if you want to make the titles a link to the post.
Unfortunately, I get a:
Parse error: syntax error, unexpected T_STRING in /home/content/17/5686317/html/wp-content/themes/aparatus/functions.php on line 244
When I try that. Any other ideas? Thanks again for providing your thoughts.
Tyler
You posted a partial listing of the popularPosts function. Did you replace the part you posted with what I posted, or did you replace the whole function?
Probably only part… it’s tricky because it is part of a much larger function. The created (who is rather busy) of the theme suggested this, but it doesn’t work either:
I wouldn’t mess with the popularPosts function – since that was built for specifically that. If you want to pull recent posts – go in the tabbed-container.php and take out
- <?php echo popularPosts(10); ?>
replace with:
- <?php global $post; $myposts = get_posts(‘numberposts=10′); foreach($myposts as $post) : setup_postdata($post); ?>
- “><?php the_title(); ?>
<?php endforeach; ?>
He is talking about putting it here:
<div id=”tabbed-container”>
<div id=”myTabs” class=”mootabs”>
<ul class=”mootabs_title”>
<li title=”Popular”>Popular
<li title=”RecentComments”>Recent Comments
<li title=”Tags”>Tags<div id=”Popular” class=”mootabs_panel”>
-
<?php echo popularPosts(10); ?>
</div><!–popular–>
<div id=”RecentComments” class=”mootabs_panel”>
<?php my_rec_comments(7); ?>
</div>
<div id=”Tags” class=”mootabs_panel”><?php wp_tag_cloud(); ?>
</div>
</div>
</div><!–tabbed-container–>If its useful to you, the site I’m working on is: http://seattlemiscellany.com/
And it is the “Popular” tab on the right I want to be most recent. Thanks again for following up with me.
Looks like your developer’s suggestion is correct. Did you get it fixed?
Unfortunately not. I get a
Parse error: syntax error, unexpected ‘=’ in /home/content/17/5686317/html/wp-content/themes/aparatus/tabbed-container.php on line 12
message then preview box stops working.
Why don’t you paste the entire tabbed-container.php in wordpress.pastebin.ca, and post the link to it here so we can see all of the code.
With the suggested change: http://wordpress.pastebin.ca/1821597
OK, two little problems:
- There is an extra opening
<ul>right after<div id="Popular" class="mootabs_panel">. Just delete it. - The first tick mark after get_posts is not an apostrophe, it is a backtick. Change to an apostrophe.
That should fix it.
That did the trick! Thanks again for your help, I really appreciate it. It’s been a long time since I’ve been comfortable with HTML and all its changes.
Glad it worked! Now, please use the dropdown at top right to mark this topic ‘Resolved’.
The topic ‘Widget commands’ is closed to new replies.