来自短代码变量的 Wordpress 小部件 wp_query 参数
Wordpress widget wp_query params from shortcode variable
我有一个小部件可以在边栏中显示相关的 post。
我想在页面中使用短代码来操纵小部件的 wp_query 参数。
我想这样做的原因是它是一个多页 post(带有分页符)并且每个页面都与不同的类别相关。
我希望小部件根据特定页面显示推荐的 posts。
我不确定如何让小部件在 运行 和 wp_query 之前评估简码 ($a["cat"])
。
简码函数:
//[bartag cat="computers"]
function bartag_func( $atts ) {
$a = shortcode_atts( array('cat' => ''), $atts );
return $a['cat'];
}
add_shortcode( 'bartag', 'bartag_func' );
插件功能:
class related_articles_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'related_articles_Widget', // Base ID
esc_html__( 'related_articles_Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( '', 'text_domain' ), ) // Args
);
}
public function widget( $args, $instance ) {
echo $args['before_widget'];
$args = array( 'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => $a['cat']
)
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'template-parts/content', 'small-article-template');
endwhile;
wp_reset_query();
}
}
function register_related_articles_Widget() {
register_widget( 'related_articles_Widget' );
}
add_action( 'widgets_init', 'register_related_articles_Widget' );
在您的 class 中声明简码。我前几天刚做了这个,并使用本教程来帮助:https://code.tutsplus.com/articles/create-wordpress-plugins-with-oop-techniques--net-20153
编辑:
我会添加一个名为 get_widget_contents($cat);
的单独函数
function get_widget_contents($cat) {
$args = array( 'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => $cat
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'template-parts/content', 'small-article-template');
endwhile;
wp_reset_query();
}
然后在您的短代码函数中执行此操作:
function bartag_func( $atts ) {
$a = shortcode_atts( array('cat' => ''), $atts );
return get_widget_contents($a['cat']);
}
add_shortcode( 'bartag', 'bartag_func' );
您的小部件函数也将调用单独的函数:
public function widget( $args, $instance ) {
echo $args['before_widget'];
echo get_widget_contents();
}
我还没有测试过,但这应该可以。不确定您的 get_template_part 是做什么的。对于短代码,您 可能 需要 return 一串 HTML 以便将其放在正确的位置。如果是这种情况,只需将整个内容包装在 ob_start() 和 ob_get_clean()
中
我有一个小部件可以在边栏中显示相关的 post。 我想在页面中使用短代码来操纵小部件的 wp_query 参数。
我想这样做的原因是它是一个多页 post(带有分页符)并且每个页面都与不同的类别相关。 我希望小部件根据特定页面显示推荐的 posts。
我不确定如何让小部件在 运行 和 wp_query 之前评估简码 ($a["cat"])
。
简码函数:
//[bartag cat="computers"]
function bartag_func( $atts ) {
$a = shortcode_atts( array('cat' => ''), $atts );
return $a['cat'];
}
add_shortcode( 'bartag', 'bartag_func' );
插件功能:
class related_articles_Widget extends WP_Widget {
/**
* Register widget with WordPress.
*/
function __construct() {
parent::__construct(
'related_articles_Widget', // Base ID
esc_html__( 'related_articles_Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( '', 'text_domain' ), ) // Args
);
}
public function widget( $args, $instance ) {
echo $args['before_widget'];
$args = array( 'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => $a['cat']
)
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'template-parts/content', 'small-article-template');
endwhile;
wp_reset_query();
}
}
function register_related_articles_Widget() {
register_widget( 'related_articles_Widget' );
}
add_action( 'widgets_init', 'register_related_articles_Widget' );
在您的 class 中声明简码。我前几天刚做了这个,并使用本教程来帮助:https://code.tutsplus.com/articles/create-wordpress-plugins-with-oop-techniques--net-20153
编辑: 我会添加一个名为 get_widget_contents($cat);
的单独函数function get_widget_contents($cat) {
$args = array( 'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => $cat
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'template-parts/content', 'small-article-template');
endwhile;
wp_reset_query();
}
然后在您的短代码函数中执行此操作:
function bartag_func( $atts ) {
$a = shortcode_atts( array('cat' => ''), $atts );
return get_widget_contents($a['cat']);
}
add_shortcode( 'bartag', 'bartag_func' );
您的小部件函数也将调用单独的函数:
public function widget( $args, $instance ) {
echo $args['before_widget'];
echo get_widget_contents();
}
我还没有测试过,但这应该可以。不确定您的 get_template_part 是做什么的。对于短代码,您 可能 需要 return 一串 HTML 以便将其放在正确的位置。如果是这种情况,只需将整个内容包装在 ob_start() 和 ob_get_clean()
中