我有一个小部件/插件,它列出了所选product\\u cat的可选产品数量。我正在努力将所选product\\u cat的关联URL传递给位于任何给定product\\u cat的产品列表上方的product\\u cat标题。我可以选择product\\u cat并呈现大量相关产品,但我无法提供包含在正确product\\u cat链接中的标题。这是我的密码。我真的很感谢你的帮助。我正在努力使用get\\u cat\\u ID和get\\u category\\u链接。我做错了什么事。我已经尝试了各种解决方案,但我不明白。
// Register widget
add_action( \'widgets_init\', \'redding420_register_widget_cat_recent_posts\' );
function redding420_register_widget_cat_recent_posts() {
register_widget( \'redding420_widget_cat_recent_posts\' );
}
class redding420_widget_cat_recent_posts extends WP_Widget {
public function __construct() {
parent::__construct(
\'redding420_widget_cat_recent_posts\',
__( \'Recent Posts by Category\', \'recent-posts-by-category-widget\' ),
array(
\'classname\' => \'redding420_widget_cat_recent_posts widget_recent_entries\',
\'description\' => __( \'Display recent blog posts from a specific category\', \'recent-posts-by-category-widget\' )
)
);
}
// Build the widget settings form
function form( $instance ) {
$defaults = array( \'title\' => \'\', \'category\' => \'\', \'number\' => 5, \'show_date\' => \'\' );
$instance = wp_parse_args( ( array ) $instance, $defaults );
$title = $instance[\'title\'];
$category = $instance[\'category\'];
$number = $instance[\'number\'];
$show_date = $instance[\'show_date\'];
?>
<p>
<label for="redding420_widget_cat_recent_posts_title"><?php _e( \'Title\' ); ?>:</label>
<input type="text" class="widefat" id="redding420_widget_cat_recent_posts_title" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="redding420_widget_cat_recent_posts_category"><?php _e( \'Product Category\' ); ?>:</label>
<?php
wp_dropdown_categories( array(
\'taxonomy\' => \'product_cat\',
\'orderby\' => \'title\',
\'hide_empty\' => false,
\'name\' => $this->get_field_name( \'category\' ),
\'id\' => \'redding420_widget_cat_recent_posts_category\',
\'class\' => \'widefat\',
\'value_field\'=> \'slug\',
\'selected\' => $category
) );
?>
</p>
<p>
<label for="redding420_widget_cat_recent_posts_number"><?php _e( \'Number of posts to show\' ); ?>: </label>
<input type="text" id="redding420_widget_cat_recent_posts_number" name="<?php echo $this->get_field_name( \'number\' ); ?>" value="<?php echo esc_attr( $number ); ?>" size="3" />
</p>
<p>
<input type="checkbox" id="redding420_widget_cat_recent_posts_show_date" class="checkbox" name="<?php echo $this->get_field_name( \'show_date\' ); ?>" <?php checked( $show_date, 1 ); ?> />
<label for="redding420_widget_cat_recent_posts_show_date"><?php _e( \'Display post date?\' ); ?></label>
</p>
<?php
}
// Save widget settings
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[\'title\'] = wp_strip_all_tags( $new_instance[\'title\'] );
$instance[\'category\'] = wp_strip_all_tags( $new_instance[\'category\'] );
$instance[\'number\'] = is_numeric( $new_instance[\'number\'] ) ? intval( $new_instance[\'number\'] ) : 5;
$instance[\'show_date\'] = isset( $new_instance[\'show_date\'] ) ? 1 : 0;
return $instance;
}
// Display widget
function widget( $args, $instance ) {
extract( $args );
echo $before_widget;
$title = apply_filters( \'widget_title\', $instance[\'title\'], $instance, $this->id_base );
$category = $instance[\'category\'];
$number = $instance[\'number\'];
$show_date = ( $instance[\'show_date\'] === 1 ) ? true : false;
// *** THIS IS THE CODE I\'M STRUGGLING WITH ***
// Get the ID of a given category
$category_id = get_cat_ID( $title );
// Get the URL of this category
$category_link = get_category_link( $category_id );
if ( !empty( $title ) ) echo $before_title . \'<a href="\' . $category_link . \'">\' . $title . \'</a>\' . $after_title;
$cat_recent_posts = new WP_Query( array(
\'post_type\' => \'product\',
\'posts_per_page\' => $number,
\'product_cat\' => $category
) );
if ( $cat_recent_posts->have_posts() ) {
while ( $cat_recent_posts->have_posts() ) {
$cat_recent_posts->the_post();
echo
\'<div class="card clickme cont-shadow">\',
\'<div class="card-block">\',
\'<div class="row">\';
echo \'<div class="col-sm-2">\',
\'<a href="#myModal-\' . get_the_ID() . \'"\' . \'data-toggle="modal"\' . \'data-target="#myModal-\' . get_the_ID() . \'">\';
if ( has_post_thumbnail() ) {
echo the_post_thumbnail( \'full\', array( \'class\' => \'img-fluid\') );
}
echo \'</a>\',
\'</div>\';
echo \'<div class="col-sm-10">\',
\'<a href="#myModal-\' . get_the_ID() . \'"\' . \'data-toggle="modal"\' . \'data-target="#myModal-\' . get_the_ID() . \'"\' . \'class="link-color"\' . \'>\' . \'<h2>\' . get_the_title() . \'</h2>\' . \'</a>\';
echo \'<span>\' . get_the_excerpt() . \'</span>\';
if ( $show_date ) echo \'<span class="post-date">\' . get_the_time( get_option( \'date_format\' ) ) . \'</span>\';
echo \'</div>\';
// Modal
echo \'<div class="modal fade" id="myModal-\' . get_the_ID() . \'"\' . \'tabindex="-1"\' . \'role="dialog"\' . \'aria-labelledby="myModal-\' . get_the_ID() . \'Title\' . \'"\' . \'aria-hidden="true">\',
\'<div class="modal-dialog" role="document">\',
\'<div class="modal-content">\',
\'<div class="modal-header">\',
\'<a href="\' . get_the_permalink() . \'"\' . \'>\',
\'<h2 class="modal-title" id="myModal-\' . get_the_ID() . \'Title"\' . \'>\' . get_the_title() . \'</h2>\',
\'</a>\',
\'<button type="button" class="close" data-dismiss="modal" aria-label="Close">\',
\'<span aria-hidden="true">×</span>\',
\'</button>\',
\'</div>\';
echo \'<div class="modal-body">\',
the_content(),
\'</div>\';
echo \'<div class="modal-footer">\',
\'<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>\',
\'</div>\',
\'</div>\',
\'</div>\',
\'</div>\',
\'</div>\',
\'</div>\',
\'</div>\';
}
} else {
_e( \'<h2>\' . \'Nothing yet.\' . \'</h2>\', \'recent-posts-by-category-widget\' );
}
wp_reset_postdata();
echo \'<a href="\' . $category_link . \'"\' . \'title="Category Name"\' . \'class="cat-fpmore"\' . \'>\' . \'Read More From This Category: \' . $title . \'</a>\',
\'<hr>\';
echo $after_widget;
}
}