我在为WP主题编码的自定义小部件上遇到了障碍。小部件从自定义帖子类型检索帖子列表,并使用“高级自定义字段”来执行此操作。然后,我调用get_template在小部件中显示帖子。
ACF要求窗口小部件ID包含在字段请求中以检索字段数据。我要完成的工作是使用从ACF请求中检索的帖子名称,将小部件标题更改为WP后端中的帖子名称。不幸的是,我似乎无法使其工作,因为我无法将ID变量传递到小部件表单部分。我已经阅读了食典,搜索了堆栈并搜索了reddit。这是我找到的最接近答案的地方:https://wordpress.stackexchange.com/questions/42955/how-can-i-get-id-variable-in-widgets-form-function
这是我的代码:
<?php
// Creating the widget
class theme_form_widget extends WP_Widget
{
//Register widget with WordPress
function __construct()
{
parent::__construct('theme_form_widget', // Base ID
esc_html__('Theme Form Widget', 'text_domain'), // Name
array(
'description' => esc_html__('A Theme Form Widget', 'text_domain')
) // Args
);
}
// Creating widget front-end
// This is where the action happens
public function widget($args, $instance)
{
//store the options in variables
$option1 = $instance['option1']; // Option1 centers widget text
$option2 = $instance['option2']; // Option2 enables widget drop shadow
if (!empty($instance['option1']) && $instance['option2'] == null) {
// class that centers widget text
$class_to_add = 'widget theme-form-widget-center';
$class_to_add = 'class="' . $class_to_add . '"';
$args['before_widget'] = str_replace('class="widget widget_theme_form_widget"', $class_to_add, $args['before_widget']);
} elseif ($instance['option1'] == null && !empty($instance['option2'])) {
// class that enables widget drop shadow
$class_to_add = 'widget theme-form-widget-shadow';
$class_to_add = 'class="' . $class_to_add . '"';
$args['before_widget'] = str_replace('class="widget widget_theme_form_widget"', $class_to_add, $args['before_widget']);
} elseif (!empty($instance['option1']) && !empty($instance['option2'])) {
// class that enables both centered text and drop shadow
$class_to_add = 'widget theme-form-widget-centershadow';
$class_to_add = 'class="' . $class_to_add . '"';
$args['before_widget'] = str_replace('class="widget widget_theme_form_widget"', $class_to_add, $args['before_widget']);
} else {
// class for widget default style
$class_to_add = 'widget theme-form-widget-default';
$class_to_add = 'class="' . $class_to_add . '"';
$args['before_widget'] = str_replace('class="widget widget_theme_form_widget"', $class_to_add, $args['before_widget']);
}
// before widget (defined by theme)
echo $args['before_widget'];
// declare variables for advanced custom fields
$post_object = get_field('widget_form_type', 'widget_' . $this->id); // gets array of post data
$instance['post_object_id'] = $post_object->ID; // assigns post id to variable
$instance['title'] = $post_object->post_title; //assigns post title to variable
$page_template = get_page_template_slug($post_object); // assigns post page template to variable
$template_slug = substr($page_template, 0, -4); // assigns template name to variable after stripping file type from name
$instance['template_slug'] = $template_slug; //assigns stripped template name to variable
if (!empty($instance['title'])) {
echo $args['before_title'] . $instance['title'] . $args['after_title'];
}
get_template_part($instance['template_slug']); //retrieves post template for display in the widget
// after widget (defined by theme)
echo $args['after_widget'];
}
// Widget Backend
public function form($instance)
{
// Option 1 and option 2 are checkboxes to change the state of widget drop shadow and text alignment
//Check if option1 exists, if its null, put "new option1" for use in the form
if (isset($instance['option1'])) {
$option1 = $instance['option1'];
} else {
$option1 = __('new option1', 'text_domain');
}
//Repeat for option2
if (isset($instance['option2'])) {
$option1 = $instance['option2'];
} else {
$option1 = __('new option2', 'text_domain');
}
?>
//Widget checkboxes for text alignment and drop shadow on form
<p>
<input <?php checked((bool) $instance['option1'], true); ?> id="<?php echo esc_attr($this->get_field_id('option1')); ?>" name="<?php echo esc_attr($this->get_field_name('option1')); ?>" type="checkbox" />
<label for="<?php echo esc_attr($this->get_field_id('option1')); ?>">Center Justify Widget Content</label>
</p>
<p>
<input <?php checked((bool) $instance['option2'], true); ?> id="<?php echo esc_attr($this->get_field_id('option2')) ?>" name="<?php echo esc_attr($this->get_field_name('option2')); ?>" type="checkbox" />
<label for="<?php echo esc_attr($this->get_field_id('option2')); ?>">Turn On Widget Drop Shadow</label>
</p>
<?php
if (!empty($instance['title'])) {
$title = $posty_title;
}
?>
//Attempting to assign post name to widget name here. It's not working, though.
<p>
<input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="hidden" value="<?php echo $posty_title; ?>">
</p>
<?php
}
// Updating widget replacing old instances with new
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['option1'] = (!empty($new_instance['option1'])) ? strip_tags($new_instance['option1']) : '';
$instance['option2'] = (!empty($new_instance['option2'])) ? strip_tags($new_instance['option2']) : '';
$instance['post_object_id'] = (!empty($new_instance['post_object_id'])) ? strip_tags($new_instance['post_object_id']) : '';
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}
} // Class theme_form_widget ends here
// Register and load the widget
add_action('widgets_init', function()
{
register_widget('theme_form_widget');
});
?>
我会很高兴有人指出我的错误,因此我可以从中学习。预先感谢。