我为自定义帖子类型创建了一个小部件,它将在小部件区域中显示帖子列表。它工作正常,我可以选择多个帖子并保存小部件,但我无法让它显示在前端。UPDATE因此,基本上,我试图实现的是用一个复选框列出小部件区域中的所有帖子(效果很好),当选中复选框时,它将在前端显示该帖子,如果不选中复选框,它将不会显示在前端。这是不起作用的部分,我在前端没有内容输出HERE IS MY CODE
<?php class mm_location_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
\'mm_location_widget\',
// Widget name will appear in UI
__(\'Locations By Marvil Media Widget\', \'mm_location_widget_domain\'),
// Widget description
array( \'description\' => __( \'Locations Widget By Marvil Media\', \'mm_location_widget_domain\' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$your_checkbox_var = $instance[ \'your_checkbox_var\' ] ? \'true\' : \'false\';
// before and after widget arguments are defined by themes
echo $args[\'before_widget\'];
if ( ! empty( $title ) )
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
// This is where you run the code and display the output
//echo __( \'Hello, World!\', \'mm_location_widget_domain\' );
// Retrieve the checkbox
if ( $your_checkbox_var == \'checked\' ) {
$loc_id = $post->ID;
$loc_title = get_the_title($post->ID);
$loc_link = get_permalink($post->ID);
$loc_address = get_post_meta( get_the_ID(), \'_marvilmedia_address\', true );
$loc_city = get_post_meta( get_the_ID(), \'_marvilmedia_city\', true );
$loc_state = get_post_meta( get_the_ID(), \'_marvilmedia_state\', true );
$loc_zipcode = get_post_meta( get_the_ID(), \'_marvilmedia_zipcode\', true );
$loc_phone = get_post_meta( get_the_ID(), \'_marvilmedia_phone\', true );
$loc_direction = get_post_meta( get_the_ID(), \'_marvilmedia_direction\', true );
// echo "<ul>";
foreach ($posts as $post) {
$loc_info .= \'<div class="col-xs-12">\';
$loc_info .= \'<h5><a target="_blank" href="\'.get_the_permalink().\'">\' . get_the_title() . \'</a></h5>\';
$loc_info .= \'<p>\';
$loc_info .= $loc_address.\'<br/>\';
$loc_info .= $loc_city.\', \'.$loc_state.\' \'.$loc_zipcode.\'\';
$loc_info .= $loc_phone.\'<br/>\';
$loc_info .= \'</p>\';
$loc_info .= \'</div>\';
echo $loc_info;
}
// echo "</ul>";
};
?>
<?php // Retrieve the checkbox
if( \'checked\' == $instance[ \'your_checkbox_var\' ] ) :
$loc_id = $post->ID;
$loc_title = get_the_title($post->ID);
$loc_link = get_permalink($post->ID);
$loc_address = get_post_meta( get_the_ID(), \'_marvilmedia_address\', true );
$loc_city = get_post_meta( get_the_ID(), \'_marvilmedia_city\', true );
$loc_state = get_post_meta( get_the_ID(), \'_marvilmedia_state\', true );
$loc_zipcode = get_post_meta( get_the_ID(), \'_marvilmedia_zipcode\', true );
$loc_phone = get_post_meta( get_the_ID(), \'_marvilmedia_phone\', true );
$loc_direction = get_post_meta( get_the_ID(), \'_marvilmedia_direction\', true );
// echo "<ul>";
foreach ($posts as $post) {
$loc_info .= \'<div class="col-xs-12">\';
$loc_info .= \'<h5><a target="_blank" href="\'.get_the_permalink().\'">\' . get_the_title() . \'</a></h5>\';
$loc_info .= \'<p>\';
$loc_info .= $loc_address.\'<br/>\';
$loc_info .= $loc_city.\', \'.$loc_state.\' \'.$loc_zipcode.\'\';
$loc_info .= $loc_phone.\'<br/>\';
$loc_info .= \'</p>\';
$loc_info .= \'</div>\';
echo $loc_info;
}
// echo "</ul>";
endif;
echo $args[\'after_widget\'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ \'title\' ] ) ) {
$title = $instance[ \'title\' ];
}
else {
$title = __( \'Locations\', \'mm_location_widget_domain\' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( \'your_checkbox_var\' ); ?>"><?php _e( \'Locations:\' ); ?></label><br/>
<?php
$args = array( \'post_type\' => \'mm_location\', \'posts_per_page\' => -1, \'post_status\' =>\'any\', \'post_parent\' => $post->ID );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) {
$option=\'<input type="checkbox" id="\'. $this->get_field_id( \'your_checkbox_var\' ) .\'[]" name="\'. $this->get_field_name( \'your_checkbox_var\' ) .\'[]"\';
//if (is_array($instance[\'your_checkbox_var\'])) {
foreach ($instance[\'your_checkbox_var\'] as $posts) {
if($posts==$post->ID) {
$option=$option.=\' checked="checked"\';
}
}
//}
$option .= \' value="\'.$post->ID.\'" />\';
$option .= get_the_title($post->ID);
$option .= \'<br />\';
echo $option;
} ?>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
$instance[ \'your_checkbox_var\' ] = $new_instance[ \'your_checkbox_var\' ];
return $instance;
}
} // Class mm_location_widget ends here
// Register and load the widget
function marvilmedia_loc_load_widget() {
register_widget( \'mm_location_widget\' );
}
add_action( \'widgets_init\', \'marvilmedia_loc_load_widget\' );
?>
SO网友:sacgro
以下是您正在寻找的内容:
<?php
class mm_location_widget extends WP_Widget
{
function __construct()
{
parent::__construct(
// Base ID of your widget
\'mm_location_widget\',
// Widget name will appear in UI
__(\'Locations By Marvil Media Widget\', \'mm_location_widget_domain\'),
// Widget description
array(
\'description\' => __(\'Locations Widget By Marvil Media\', \'mm_location_widget_domain\')
));
}
// Creating widget front-end
// This is where the action happens
public function widget($args, $instance)
{
$title = apply_filters(\'widget_title\', $instance[\'title\']);
// $your_checkbox_var = $instance[ \'your_checkbox_var\' ] ? \'true\' : \'false\';
// before and after widget arguments are defined by themes
echo $args[\'before_widget\'];
if (!empty($title))
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
// This is where you run the code and display the output
//echo __( \'Hello, World!\', \'mm_location_widget_domain\' );
// Retrieve the checkbox
$args1 = array(
\'post_type\' => \'mm_location\',
\'posts_per_page\' => -1,
\'post_status\' => \'publish\'
);
$myposts = get_posts($args1);
foreach ($myposts as $post) {
$loc_info = "";
$option = false;
foreach ($instance[\'checkbox\'] as $checks) {
if ($checks == $post->ID) {
$option = true;
}
}
if ($option) {
$loc_id = $post->ID;
$loc_title = get_the_title($post->ID);
$loc_link = get_permalink($post->ID);
$loc_address = get_post_meta($loc_id, \'_marvilmedia_address\', true);
$loc_city = get_post_meta($loc_id, \'_marvilmedia_city\', true);
$loc_state = get_post_meta($loc_id, \'_marvilmedia_state\', true);
$loc_zipcode = get_post_meta($loc_id, \'_marvilmedia_zipcode\', true);
$loc_phone = get_post_meta($loc_id, \'_marvilmedia_phone\', true);
$loc_direction = get_post_meta($loc_id, \'_marvilmedia_direction\', true);
$loc_info .= \'<div class="col-xs-12">\';
$loc_info .= \'<h5><a target="_blank" href="\' . $loc_link . \'">\' . $loc_title . \'</a></h5>\';
$loc_info .= \'<p>\';
$loc_info .= $loc_address . \'<br/>\';
$loc_info .= $loc_city . \', \' . $loc_state . \' \' . $loc_zipcode . \'\';
$loc_info .= $loc_phone . \'<br/>\';
$loc_info .= \'</p>\';
$loc_info .= \'</div>\';
echo $loc_info;
// echo "</ul>";
}
;
}
echo $args[\'after_widget\'];
}
// Widget Backend
public function form($instance)
{
if (isset($instance[\'title\'])) {
$title = $instance[\'title\'];
} else {
$title = __(\'Locations\', \'mm_location_widget_domain\');
}
// Widget admin form
?>
<p>
<label for="<?php
echo $this->get_field_id(\'title\');
?>"><?php
_e(\'Title:\');
?></label>
<input class="widefat" id="<?php
echo $this->get_field_id(\'title\');
?>" name="<?php
echo $this->get_field_name(\'title\');
?>" type="text" value="<?php
echo esc_attr($title);
?>" />
</p>
<p>
<label for="<?php
echo $this->get_field_id(\'checkbox\');
?>"><?php
_e(\'Locations:\');
?></label><br/>
<?php
$args = array(
\'post_type\' => \'mm_location\',
\'posts_per_page\' => -1,
\'post_status\' => \'publish\'
);
$myposts = get_posts($args);
foreach ($myposts as $post) {
$option = \'<input type="checkbox" id="\' . $this->get_field_id(\'checkbox\') . \'[]" name="\' . $this->get_field_name(\'checkbox\') . \'[]"\';
$selected = $instance[\'checkbox\'];
$arrlength = count($selected);
for ($x = 0; $x < $arrlength; $x++) {
if ($selected[$x] == $post->ID) {
$option = $option .= \' checked="checked"\';
}
}
//}
$option .= \' value="\' . $post->ID . \'" />\';
$option .= get_the_title($post->ID);
$option .= \'<br />\';
echo $option;
}
?>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update($new_instance, $old_instance)
{
$instance = array();
$instance[\'title\'] = (!empty($new_instance[\'title\'])) ? strip_tags($new_instance[\'title\']) : \'\';
$instance[\'checkbox\'] = $new_instance[\'checkbox\'];
return $instance;
}
} // Class mm_location_widget ends here
// Register and load the widget
function marvilmedia_loc_load_widget()
{
register_widget(\'mm_location_widget\');
}
add_action(\'widgets_init\', \'marvilmedia_loc_load_widget\');