Modify a wordpress widget programatically from the template file?

Viewed 93

In my WordPress site, I would like to have a menu for each Month Archives page that lists all of the posts published that month in the sidebar. I would like the menu to update automatically depending on which month page the user is on.

I created a custom widget that lists all of the pages in a certain month and year. But I can't figure out how to update the widget to display the month and year for each month's page. The best idea I have is to pass the month/year parameter from the template.php file to the widget class somehow, but I'm not sure how to do that.

So, is there any way to change widget variables in a sidebar from the template file?

Here is my widget function in the widget class extended from WP_Widget, in functions.php. It contains the variables I would like to change for each archive page.

public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );

    // before and after widget arguments are defined by themes
    echo $args['before_widget'];
    if ( ! empty( $title ) ) {
        echo $args['before_title'] . $title . $args['after_title'];
    }
    // Output of Widget
    $query = array(
        'numberposts' => -1,
        'monthnum' => 6, //I would like to change this dynamically
        'year' => 2020, //I would like to change this dynamically
        'orderby' => !empty($instance['orderby']) ? $instance['orderby'] : 'name',
        'order' => !empty($instance['order']) ? $instance['order'] : 'asc'
    );
    $posts = get_posts( $query );
    if (!empty($posts)) {
        echo '<ul>';
        foreach( $posts as $post) {
            echo '<li>';
            echo '<a href="#before-post-' . $post->ID . '">'
                . $post->post_title . '</a></li>';
        }
        echo '</ul>';
    } else {
        echo 'Sorry. No articles have been added to this month\'s newsletter yet.';
    }
    echo $args['after_widget'];
}

I add it to a sidebar called sidebar-month from the front-end, and then I call the sidebar from archives.php using get_sidebar('month'). Here is my archives.php file:

<div class="wrapper u-t-margin">
    <div class="columns columns--gutters">

        <div id="primary" class="content-area columns__md-8 u-b-margin">
            <main id="main" class="site-main" role="main">

                <?php
                if ( have_posts() ) {
                    ?>

                    <header class="page-header gx-card-content gx-card-content--same-md-y u-b-margin">
                        <?php
                        the_archive_title( '<h1 class="page-title archive-title">', ' Newsletter</h1>' );
                        the_archive_description( '<div class="archive-description">', '</div>' );
                        ?>
                    </header>

                    <?php
                    while ( have_posts() ) {
                        the_post();
                    ?><div id="before-post-<?php the_ID() ?>"></div>
                    <?php
                        get_template_part( 'template-parts/content', get_post_type() );
                    }

                    the_posts_pagination();
                } else {
                    get_template_part( 'template-parts/content', 'none' );
                }
                ?>

            </main>
        </div>

        <?php get_sidebar(); ?>

    </div>
</div>
0 Answers
Related