Adding Widgets In or Between Posts

Dec 132013
 
 Posted by on 13/12/2013 How-To's Tagged with: , , , ,  Add comments
Ads at the end of single post
Ads at the end of single post

For several times more questions on Suffusion forum addressed 2 issues: how can add codes (like ads) between a specific number of posts and how can add programmatically widget areas inside the content of posts or pages.

Today, Miguel, a friend from Spain, ask me an interesting question. On aggregate posts pages (category, tag etc.) he need to have one banner after first post, another ad after second post and yet another one after the third post, but all those 3 ads at the end of each post when is viewed in full. And all these ads need to be easily accessible to be changed as needed.

So, Suffusion don’t provide a tool for placing ads, but provide a very powerful feature – Ad-Hoc Widget Areas. These areas can be added in posts/pages using a shortcode, and here can use any of the widgets from Appearance -> Widgets.

Note. 1. In theme are bundled 5 Ad-Hoc areas, but you will have to install Suffusion Shortcodes plugin for activating these areas. After installing the plugin can create as many Ad-Hoc areas you wish, and these areas (and all Suffusion shortcodes) will be available for other themes too, so you will preserve their content if you switch the theme.

Note.2. In the next steps will go to alter layout-blog.php template and add a new function in functions.php. Better in this case is to use a child theme for preserving your changes when Suffusion will be updated.

Step 1. Let’s start to add the widget areas where is needed. First will add our widget areas at the end of full post. Open functions.php from your child theme folder and add a new function:

 This code will add 3 different Ad-Hoc widget areas at the end of full posts. Of course you can add as many you wish. For changing their position use other hook than “suffusion_before_end_post” (a complete list of action hooks was published by Sayontan on Aquoid site. For having the widget areas on all pages just remove the “if {}” condition, so the code will contain only “echo” functions.

Step. 2. Let’s add the widgets on aggregate pages. For this part I adapted the solution proposed at Lancelhoff.com as it was the most used solution on Suffusion forum. Start by copying layout-blog.php from /suffusion/layouts/ folder into a /layouts/ subfolder created in your child theme folder.

Edit the copy and look for the line if (have_posts()) near to beginning of file. Immediately above this line can see the declaration: global $post, $suffusion_cpt_post_id;. Complete the declaration with a new variable: global $post, $suffusion_cpt_post_id, $suffusion_count_index;. This variable is a counter which will be incremented at each post. Immediatelly below “if” line define the first value of the counter: $suffusion_count_index = 1;. The entire code around the “if” line will look like: 

 We finished to define our counter, now let’s add the Ad-Hoc areas where is needed. Look for the line suffusion_before_end_post(); almost at the end of file. Immediately after that line add the code:

 Save the file and you’re set. Can go at Appearance -> Widgets and add your widgets in the Ad-Hoc Widget Areas. Anything added in those areas will be shown now between posts when posts on aggregate pages are displayed in full or excerpts but also at the end of single posts.

Of course if you need ads after posts 5, 6 or 9 need to change appropriately the value of $suffusion_count_index. If you need another position for ads, change the position of last code before/after different hooks contained in the file.

Can download the changed layout-blog.php from here: Download changed layout-blog.php

https://drafie-design.nl/adding-widgets-in-or-between-posts/trackback/Trackback URI:  

Ciprian

Experienced web developer and graphic designer. Former consultant for Suffusion theme and former moderator on Suffusion Support forum. I teach others while learning myself.

Google+ Profile

    Google+ Page

 Leave a Reply

(Your comment will be published. To contact me privately, use the contact form.)

This site uses Akismet to reduce spam. Learn how your comment data is processed.