inFocus WP Theme: Customising WP-PostViews and Efficient Related Posts Plugins

i

5. Including the Plugins in the inFocus Theme

The code block that controls the display of the inFocus Popular and Related Posts widget is in lib/includes/template-single.php:

<?php if (!$related_popular_posts) { ?>
<div id="popular_related_posts">
	<div class="one_half"><?php webtreats_popular_post(); ?></div>
	<div class="one_half last"><?php webtreats_related_post(); ?></div>
	<div class="clearboth"></div>
</div>
<?php } ?>

We need to replace the entire code block with:

<?php if (!$related_popular_posts) { // Use WebTreats inFocus theme widgets ?>
	<div id="popular_related_posts">
		<div class="one_half"><?php webtreats_popular_post(); ?></div>
		<div class="one_half last"><?php webtreats_related_post(); ?></div>
		<div class="clearboth"></div>
	</div>
<?php } else { // Use WP-PostViews & Efficient Related Posts plugins ?>
	<?php if ( function_exists('get_most_viewed') || function_exists('wp_get_related_posts') ): // Check if either plugin is active ?>
		<div id="popular_related_posts">
			<?php if (function_exists('get_most_viewed')): // Check if WP-PostViews is active ?>
				<div class="one_half">
					<h3 class='related_post_title'>Popular Posts</h3>
					<ul class='thumbnail_list'>
						<?php get_most_viewed('post', 5); ?>
						<?php wp_reset_query(); ?>
					</ul>
				</div>
			<?php endif; ?>
			<?php if (function_exists('wp_get_related_posts')): // Check if Efficient Related Posts is active ?>
				<div class="one_half last">
					<?php echo wp_get_related_posts(array('title'=>'Related Posts', 'num_to_display'=>5, 'no_rp_text'=>'No Related Posts', 'use_infocus_theme_style'=>true)); ?>
					<?php wp_reset_query(); ?>
				</div>
			<?php endif; ?>
			<div class="clearboth"></div>
		</div>
	<?php endif; ?>
<?php } ?>

The first part of the new code block is the same. It checks to see if, in the inFocus admin panel, we’ve chosen to display the inFocus widgets. If we haven’t, the subsequent new code block will determine what to do next.

If the WP-PostViews plugin is active it will call get_most_viewed(). Similarly, if the Efficient Related Posts plugin is active it will call wp_get_related_posts(). Notice that after each of these functions are executed I’ve included a call to wp_reset_query() which destroys any previous queries.

This is necessary as I noticed that after one or perhaps both of the functions executed subsequent queries were not behaving as expected. For example have_comments() was returning false when I knew the post had comments. You can read more about wp_reset_query() here.

Further reading on using the get_most_viewed() function can be found here under the Usage tab. For more information on the wp_get_related_posts() function check the FAQ section here.

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of fish & chips and writing the occasional blog post he hopes others will find helpful.

22 responses

Leave a Reply to dev Cancel reply

21 Comments

    • The number is formatted using number_format_i18n on line 235 of wp-content/plugins/wp-postviews/wp-postviews.php like so:

      	$temp = str_replace("%VIEW_COUNT%", number_format_i18n($post_views), $temp);
      

      You could change it using a function similar to the one described here.

      Regards, Steve

  • Hi Steve, my question is not related to this post but since you have a lot of experience in this field I wanted to ask, have you come across any wordpress plugin that can bookmark to various sites from within the worpress admin panel rather then from the site itself. I mean say there are buttons like the bold, italics, etc. for various social sites and you can bookmark the post while writing the post itself. Please help I require it urgently. If not what is the way I can proceed I mean coding one myself with your assistance if you would not mind.

    • dev,

      ….i can provide snapshots if required

      Sure. These will help, but without seeing the code you’re using it may be difficult to give a definitive answer.

      Regards, Steve.

  • Also the sliding Popular Post widget is affected. It too shows the image icons which doesnt look good. Sorry I am new to web development so the questions may be lame but please help me out.

  • After going through the steps you have mentioned the popular posts widget in my footer shows both the infocus style and modified footer template. First the infocus style on the top followed by the modified style in the bottom thus showing posts two times. Any idea why this is happening?

  • Thanks…I was able to find the discuss area and posted the question…so hopefully I’ll be able to figure it out. thanks!

  • Thanks Steve. I’ve scoured that page many times and cannot for the life of me figure out how to leave a comment. Nor can I seem to search the comments there for my issues. Quite frustrating. I have not altered anything in the theme, so…… And yes, to answer your questions, I’ve never experienced this before and I run quite a few wordpress sites. If you know of a way to actually contact the developer and I am totally missing it, please let me know. Thanks so much for your time!

    • Hi Charissa,

      Yeah, not being able to search the discussions is frustrating. At the bottom of the page though there’s a ‘Discuss This Item’ box would should allow you to post a comment. Failing that, you could try ‘Email Webtreats’ at the bottom of this page.

      Sorry I can’t be of more help. Steve.

  • I am banging my head against the wall trying to figure out why category links for my inFocus theme return ALL POSTS. Tag links work just fine. Cannot figure out what the heck is going on?
    Any insights would be smashing. Thanks!
    http://epoxy-surfboard-reviews.com/surfer-feedback
    Try clicking on a category link and you’ll get every post returned.
    Try clicking on a tag and you’ll get only the posts tagged with that particular name.
    ?????????????

    • Hi Charissa,

      I’ve not seen this before and without access to your WP install I’m afraid I can’t be of much of help. Does this only happen with the inFocus theme? If it does, you may do better contacting the inFocus developers.

      Good luck, Steve.

  • Neat! curious though, do you know if it is possible to filter the results of $output from your theme without modifying the code of the plugin itself? once you mod the plugin doesn’t that pretty much mean you can never upgrade? now that you can easily attach a featured image to a post (without a custom field) i hope the ERP author will include that as part of the array that is stored w/ each post.

    • Hi Kathy,

      …do you know if it is possible to filter the results of $output from your theme without modifying the code of the plugin itself?

      Not sure about this. You may want to ask the developers.

      …once you mod the plugin doesn’t that pretty much mean you can never upgrade?

      True to some extent, but it should be possible to apply the changes to the upgraded plugin. Just makes it not so simple to upgrade.

      Regards, Steve.

  • Tolu

    …how did you get to style the popular posts widget in your footer to look so much like the built-in widget? I’m getting some space after the h3 header before the first list item…

    Add this to the bottom of style.css:

    .widget_execphp h3.widgettitle {
    	margin: 0 !important;
    }
    

    Regards, Steve

  • By the way, how did you get to style the popular posts widget in your footer to look so much like the built-in widget? I’m getting some space after the h3 header before the first list item, can’t figure out why!

  • In your own words:
    “In conclusion then, this is a fairly lengthy customisation for something that’s entirely cosmetic and perhaps quite trivial. Whether it’s worth the effort or not, I’ll leave up to you.”

    I tell you what? It was really worth it!

1 Pingback

Steve

Recent Comments

Recent Posts