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:
1 2 3 4 5 6 7 8 |
add_action( 'suffusion_before_end_post', 'suf_perm_widget' ); function suf_perm_widget() { if (is_single()) { echo do_shortcode('[suffusion-widgets id="1"]'); echo do_shortcode('[suffusion-widgets id="2"]'); echo do_shortcode('[suffusion-widgets id="3"]'); } } |
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:
1 2 3 4 5 |
global $post, $suffusion_cpt_post_id, $suffusion_count_index; if (have_posts()) { $suffusion_count_index = 1; |
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:
1 2 3 4 5 6 7 8 9 10 |
if ($suffusion_count_index == 1) { echo do_shortcode('[suffusion-widgets id="1"]'); } elseif ($suffusion_count_index == 2) { echo do_shortcode('[suffusion-widgets id="2"]'); } elseif ($suffusion_count_index == 3) { echo do_shortcode('[suffusion-widgets id="3"]'); } $suffusion_count_index++; |
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