widget after post in functions.php multisite

hi, i insert this in my theme function.php file

add_action( 'frontier_after_single_post_content', 'ad_after_post' );

function ad_after_post() {
?>
<div style="text-align: center; padding-top: 24px;">
ADS CODE
 </div>
<?php
}

and now i can place my ads on all sites on my subdmains, but i want to insert "Recent global posts widget" i tried with

<div class="widget-sidebar frontier-widget" >
   <?php echo do_shortcode('[do_widget "recent global posts" ]'); ?>
</div>

insted ads code but it doesent work, how can i put widget using functions.php file, i need that because if i put in sidebar admins can remove it.

  • Sajid

    Hi @Nikola

    Hope you are doing good today :slight_smile:

    To add widget dynamically in template via functions.php file. You have to do add it using the_widget function.

    So your code will look like this instead.

    <div class="widget-sidebar frontier-widget" >
       <?php the_widget( 'Recent_Global_Posts_Widget', $instance, $args ); ?>
    </div>

    For arguments and instance please check WordPress codex page.
    https://codex.wordpress.org/Function_Reference/the_widget

    Hope it helps :slight_smile:

    Cheers, Sajid

  • Sajid

    Hi @Nikola

    Hope you are doing good today :slight_smile:

    I am sorry to be not clear enough. Yes you can use it as it is with default settings.

    If you want to change the settings/options then you can use it like this one.

    <div class="widget-sidebar frontier-widget" >
       <?php 
    
    	$instance = array(
    			'recentglobalpoststitle'             => 'Your Tile Here',
    			'recentglobalpostsdisplay'           => 'title_content|title_blog_content|title|title_blog|content|blog_content', //Select one of from given options
    			'recentglobalpostsnumber'            => '5', //number of posts to show
    			'recentglobalpoststitlecharacters'   => '30', //number of charcters of a post title
    			'recentglobalpostscontentcharacters' => '100', //nubmer chracters for post contnet
    			'recentglobalpostsavatars'           => 'show|hide', // Show or hide avatars
    			'recentglobalpostsavatarsize'        => '16',//Avatar size in pixels
    			'recentglobalpoststype'              => 'post', //What post type you want to display post/products etc..
    			'exclude_blogs'                      => '', //Blog IDs, separated by commas from which you dont want to display posts
    		);
    
       	the_widget( 'Recent_Global_Posts_Widget', $instance, $args ); ?>
    </div>

    You can ignore the $args and leave to default.

    Hope it helps :slight_smile:

    Cheers, Sajid

  • Nikola

    @Sajid this other code is better i see now that i can set up different options than is on main site, i am right?

    i place this code
    <div class="widget-sidebar frontier-widget" >
    `<?php

    $instance = array(
    'recentglobalpoststitle' => 'Your Tile Here',
    'recentglobalpostsdisplay' => 'title_content',
    'recentglobalpostsnumber' => '5',
    'recentglobalpoststitlecharacters' => '30',
    'recentglobalpostscontentcharacters' => '100',
    'recentglobalpostsavatars' => 'hide',
    'recentglobalpostsavatarsize' => '16',
    'recentglobalpoststype' => 'post',
    'exclude_blogs' => '',
    );

    the_widget( 'Recent_Global_Posts_Widget', $instance, $args ); ?>
    </div>`

    between

    function ad_after_post() {
    ?>
    <div style="text-align: center; padding-top: 24px;">

    and above this

    </div>
    <?php
    }

    but i dont show anything..? i tried and with this two codes that you give me i previus posts but nothing? can you please help me with that..

  • Sajid

    Hi @Nikola

    Hope you are doing good today :slight_smile:

    By default recent global post widget only work on main site. If you want to make it work for entire network then add following define in wp-config.php file.

    Enabling Widget for all sites

    By default the Recent Global Posts widget is only enabled for use by the man site.

    You can enable it for all sites on your network as follows:

    1. Open up the wp-config.php in the root of your WordPress install

    2. Just before the line that says That’s all, stop editing!, add the following:

    define('RECENT_GLOBAL_POSTS_WIDGET_MAIN_BLOG_ONLY', false);
    3. Save and re-upload your amended wp-config.php file

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.