After upgrading to WP 3.0 and Arras 1.4.3, I also re-arranged some of its layout – most notably, I stopped displaying the Featured posts in the Tabbed widget and replaced it with the Most Popular. There was no sense of having the featured posts displayed yet again since it was already on the front page.
Unfortunately, by doing so two things happened that broke the site in IE or made it look not as good as I wanted it to. The thing that broke is that Arras had a minor bug in the code for the Tabbed widget which ended up making the sidebar look rendered partially and in the wrong order.
The offending code was found in arras-theme/library/widgets.php:
echo '<div id="s-popular" class="widgetcontainer clearfix">';
if ( function_exists('akpc_most_popular') ) akpc_most_popular();
echo '</div><!-- #s-popular -->';
The problem is that the function akpc_most_popular() generates a list of links which are surrounded by the tags <li> and </li>. However, as you can see in the code snippet above, the tags <ul> and </ul> are missing. This was causing IE to render the list in the wrong place on the page. The fix was very simple – add the missing tags:
echo '<div id="s-popular" class="widgetcontainer clearfix"><ul>';
if ( function_exists('akpc_most_popular') ) akpc_most_popular();
echo '</ul></div><!-- #s-popular -->';
Now, that this was fixed, I had the chance to look at the content of the tab. What I realized was that I didn’t like how the information was presented at all – It was just a list of links with no other information at all.
So, I set out to find a way to fix this. After a little digging through the Arras and Popularity Contest plugin, I found out that changing the look of the tab would be relatively easy. It also turned out that the changes need to be made to only one file – arras-theme/library/widgets.php.
Even though, I didn’t want to have the Featured Post tab present in the Multi-Tabbed Widget, I did like how it look and how it presented its content, so I wanted to replicate that look for the Popular posts, as well.
However, the first step was to figure out how to actually get the raw data. The call that Arras currently used (akpc_most_popular()) output the list of popular posts directly to the browser. Looking through the Popularity Contest plugin code, I found that the akpc_most_popular() call, in turn, called a class method which did all the “dirty” work – $akpc->get_top_ranked_posts().
What I needed was an API call that would let me work with the raw data. Further inspection of the Popularity Contest (PC) code showed that there was a call that appeared might work – akpc_get_popular_posts_array(). However, that wasn’t the case. The PC code had a bug in that function.
In the body of akpc_get_popular_posts_array(), there was a call to $akpc->get_popular_posts(). The prototype/signature of that method is:
function get_popular_posts($type = 'popular', $limit, $exclude_pages = 'yes', $custom = array())
Below is the body of the akpc_get_popular_posts_array() API function.
function akpc_get_popular_posts_array($type, $limit, $custom = array()) {
global $akpc;
return $akpc->get_popular_posts($type, $limit, $custom);
}
You can see that there is a discrepancy between the method signature and the way it was being called, which resulted in it [the method] not returning any results.
The solution was simple enough – call the $akpc->get_popular_posts() method directly from arras-theme/library/widgets.php. I removed the code below:
echo '<div id="s-popular" class="widgetcontainer clearfix"><ul>';
if ( function_exists('akpc_most_popular') ) akpc_most_popular();
echo '</ul></div><!-- #s-popular -->';
and replaced with the following:
echo '<div id="s-popular" class="widgetcontainer clearfix"><ul>';
if ( function_exists('akpc_most_popular') ) {
global $akpc;
$popular = $akpc->get_popular_posts('most', 10, 'yes', array('column' => 'total'));
}
echo '</ul></div><!-- #s-popular -->';
This allowed me to get an array of the raw data that I needed. Each of the elements in that array was an object referring to a post from the 10 most viewed posts.
Now, that I had the information that I needed, it was type to display it. Because I wanted the look of the Popular tab to mimic the look of the Featured Post tab, I looked through the arras-theme/library/widgets.php file and found the code that dealt with the display of the Featured Posts:
if (is_numeric($featured)) {
echo '<div id="s-featured" class="widgetcontainer clearfix">';
$f = new WP_Query('showposts=8&cat=' . $featured);
if (!$f->have_posts()) {
echo '<span class="textCenter sub">' . __('No posts at the moment. Check back again later!', 'arras') . '</span>';
} else {
echo '<ul>';
while ($f->have_posts()) {
$f->the_post();
?>
<li class="clearfix">
<?php if (function_exists('the_post_thumbnail')) the_post_thumbnail('sidebar-thumb') ?>
<a href="<?php the_permalink() ?>"><?php the_title() ?></a><br />
<span class="sub"><?php the_time( __('d F Y g:i A', 'arras') ); ?> |
<?php comments_number( __('No Comments', 'arras'), __('1 Comment', 'arras'), __('% Comments', 'arras') ); ?></span>
</li>
<?php
}
echo '</ul>';
}
echo '</div><!-- #s-featured -->';
}
I used the above section as the template for the code which displayed the Popular posts and I ended up with this:
echo '<div id="s-popular" class="widgetcontainer clearfix"><ul>';
if ( function_exists('akpc_most_popular') ) {
global $akpc;
$popular = $akpc->get_popular_posts('most', 10, 'yes', array('column' => 'total'));
foreach ($popular as $post) {
?>
<li class="clearfix">
<?php if ( function_exists('get_the_post_thumbnail') )
echo get_the_post_thumbnail($post->ID, 'sidebar-thumb'); ?>
<a href="<?php echo get_permalink($post->ID) ?>"><?php echo $post->post_title ?></a><br />
<span class="sub"><?php echo get_the_time(__('d F Y g:i A', 'arras'), $post->ID); ?></span>
</li>
<?php
}
}
echo '</ul></div><!-- #s-popular -->';
The screenshot shows how the front page looks after making the changes above.
All the changes that are needed to fix the IE issue and change the look of the Popular tab are contained in the arras-theme/library/widgets.php file from the Arras theme. This has the benefit of retaining these changes if the PC plugin releases a new version and gets updated.
However, be careful when updating your Arras theme as the update process will replace your modified files. In order to get around that, I alway keep a copy of all the diffs that I’ve made to the theme against the original files. Since most of you (including me) have made changes to the stylesheet(s) (probably through arras-theme/user.css), most likely you are already keeping a list of all the changes. Just as this to that list.




Great tutorial! Exactly what I was looking for!
Thank you very much!
Hi, I tried to add the comments as well, but it doesnt quite work.
I added this:
|
It shows right, but the number of comments is wrong. All posts have the same number of comments as the page that they are on. For example, if i`m in a post with 3 comments, in the widget popular tab all the posts will appear to have 3 comments. Which is wrong. But for the Latest and Featured, the comments seem to be calculated right. I only get this in the popular tab.
To see for youself: http://www.wubber.net
Thanks for the hel
Oops, it didnt show the code:
I added it after the time, as it is in the featured.
?php comments_number( __(‘No Comments’, ‘arras’), __(’1 Comment’, ‘arras’), __(‘% Comments’, ‘arras’) ); ?
Hope it appear now :/
The ‘comments_number()’ call expects that the ‘the_post()’ has been called, which in the case of my suggested changes is not.
Try using ‘get_comments_number ($post->ID)’ instead of ‘comments_number()’. This call will return the number of comments, which you’ll have to format yourself. Somthing like this should work:
$num = get_comments_number($post->ID); switch ($num) { case 0: $comments = "No Comments"; break; case 1: $comments = "1 Comment"; break; default: $comments = str ($num) . " Comments"; }Please, be advised that I haven’t tested the code above.
It didn’t work… I inserted the code but nothing appeared, it was just blank. Is there a way to call ‘the_post()’ in the popular posts code?
Thanks for the help!
The fix was to echo the $comments variable after it has been set by the switch statement.
Hi! How did you solve the dup problem in the tabbed sidebar? If I’m on my homepage it looks ok but if I go to a post there is a bug. Do you have a tip?
I haven’t figured it out yet. If you notice, I have the same problem on my site. Once, I figure it out, I’ll post the fix.
Maybe there is a wp_reset_query somewhere in the theme-code. I notice that the comment count is correct in the tabbed list.
OK, I figured out my problem. The problem is the SexyBookmarks plugin. They have the following code in multiple functions:
What that ends up doing is resetting the value of the $post global variable, which carries through to other plugins and themes.
The solution is to replace every occurance of $post (except the one on the line starting with ‘global’) with a different variable in these two functions: get_sexy() and shrsb_get_fetch_url(). Both of these functions are located in sexybookmarks/include/public.php.
@Mitko Thanks for discovering this! The fix will be included in the main SB releases very soon.
http://sexybookmarks.shareaholic.com
Hi, using your new code from the update I get the following error:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/internub/online/totalgadgets.net/wp-content/themes/arras/library/widgets.php on line 365There was a small quoting error that I found thanks to your report but it wasn’t anywhere near line 365.
However, since quoting errors can manifest themselves elsewhere, please try the corrected code (I’ve edited the original code segment in the first post update) and see if it fixes your problem.
All better now, thanks very much.
Ok, another problem, it seem to be multiplying the number of visits by 10 for me: http://totalgadgets.net.
I don’t know if this is a problem with your code or the Popularity Contest plugin :/
Olly,
I don’t think that this is a problem with my changes but I’ll look into it. The call to get_popular_posts() just uses the code and logic that is from the Popularity Contest plugin to get the stats.
I’d also like to replace my featured posts in the tabbed sidebar widget to popular posts. Unfortunately, when I install the popularity contest plugin it won’t activate because it causes a fatal error “Incorrect table name ” on line: 255.
Any thoughts on what code I need to change to fix this??
The error that you are getting seems to be from a failure while trying to create the Popularity MySQL tables. More specifically, it seem that the table name is incorrect (as the error indicates).
One thing that comes to mind when looking at the plugin code is that it uses the WordPress setting for the MySQL table prefix. This setting is set in your wp-config.php file. Make sure that it is a sane setting and that it is compatible with MySQL.
My widgets.PHP file is apparently structured a bit differently. I’m trying to do the same thing here with my popular tab to get the thumbnails but can’t figure out exactly how to place the code in order to get that.
Here’s the output portion of my PHP file…
function popular_tab() {
if ( function_exists(‘akpc_most_popular’) ) {
echo ”;
akpc_most_popular(10, ”, ”);
echo ”;
}
Please let me know how I should go about doing this. Thank you so much for the help in advance!
Sanglucci ,
What version of the theme are you using?
It looks like the differences are minor (missing ‘<ul>’ from the ‘echo’ lines and ‘<li>’ from the function call). You could just proceed as the instructions outline and replace that same portion of code with the modification and see if it work.
However, if it doesn’t, I’ll need you to tell me the version of your theme so I can look at what is different.
Fatal error: Call to a member function get_popular_posts() on a non-object in /home/content/53/6867153/html/wp-content/themes/arras/library/widgets.php on line 183
I tried to enter all the code you had in your modifications update. but I ended up getting this error above. Any suggestions??? My theme is the latest one 1.5.0.1
Without having access to your WordPress files, my best guess would be that there is something going on with the Popularity Contest plugin. The error that you are getting leads me to believe that this line:
instead of pulling in the $akpc object from the Popularity plugin is creating a new variable. Make sure that these two lines exists in the akpc_init() function in the popularity-contest.php file:
global $wpdb, $akpc; $akpc = new ak_popularity_contest;I reinstalled the new theme and then added the corrections u placed above it seemed to work this time. However, now I’m not getting the thumbnails I’m just getting a small pixel by pixel square. It shows the total post views so the code def. did work to some extent. Any ideas on the thumbnail fix?
I tried using the thumbnail output code in the other tabs but that only pulled one particular thumbnail and didn’t correspond with each posts specific thumbnail.
display_thumbs) : ?>
Are you by any chance using the SexyBookmarks plugin? If so, read the comments above. There is a problem with that plugin that manifests itself with just those symptoms. However, the latest version of the plugin fixes that issue.
If you are not using that plugin, make sure that you are getting the correct post ID when you are calling the the thumbnail functions.
Don’t have the sexy bookmarks. And if your adjustments are calling the correct post titles wouldn’t they naturally pull the thumbnails as well?
This is the function used to call the thumbnails for the other tabs
display_thumbs) : ?>
By the way… haven’t played the new assassins creed how is it??
display_thumbs) : ?>
For some reason I can’t get the code to paste here…
they use if($this display_thumbs)
span class = thumb php echo arras_get_thumbnail(‘sidebar-thumb’, get the ID
Oh, I think I know what’s happening:
The reason why my version of the code did not work is because you are not actually setting thumbnails on your posts. You are using Arras’ feature that allows you to use the first attached image as a thumbnail. The function get_the_post_thumbnail() is a WodPress function and can not do the same thing.
Using the Arras function arras_get_thumbnail() should allow you to do what you want. Try replacing what you currently have with this:
<span class="thumb">echo arras_get_thumbnail('sidebar-thumb', $post->ID,...NOTE: The important part is replacing the part ‘get_the_ID()’ with $post->ID
Assassins Creed Brotherhood is awesome. All those games keep getting better with every new one.