Php 从模板文件以编程方式修改wordpress小部件?
在我的WordPress网站上,我想为每个月的档案页面提供一个菜单,在侧边栏中列出当月发布的所有帖子。我想菜单自动更新取决于哪个月的页面上的用户 我创建了一个自定义小部件,它列出了特定月份和年份的所有页面。但是我不知道如何更新小部件来显示每个月页面的月份和年份。我最好的办法是以某种方式将month/year参数从template.php文件传递给widget类,但我不确定如何做到这一点 那么,有没有办法从模板文件更改侧栏中的小部件变量 下面是我的widget函数,它是从WP_widget扩展而来的widget类,位于functions.php中。它包含我想为每个归档页面更改的变量Php 从模板文件以编程方式修改wordpress小部件?,php,wordpress,widget,custom-wordpress-pages,Php,Wordpress,Widget,Custom Wordpress Pages,在我的WordPress网站上,我想为每个月的档案页面提供一个菜单,在侧边栏中列出当月发布的所有帖子。我想菜单自动更新取决于哪个月的页面上的用户 我创建了一个自定义小部件,它列出了特定月份和年份的所有页面。但是我不知道如何更新小部件来显示每个月页面的月份和年份。我最好的办法是以某种方式将month/year参数从template.php文件传递给widget类,但我不确定如何做到这一点 那么,有没有办法从模板文件更改侧栏中的小部件变量 下面是我的widget函数,它是从WP_widget扩展而来
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'];
}
<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>