我在函数中制作了一个小部件。php,如下所示:
class Categories_Widget extends WP_Widget
{
function __construct() {
parent::__construct(
\'categories_widget\', // Base ID
(\'Categoriën tonen\'), // Name
array( \'description\' => ( \'Toon categoriën\' ), ) // Args
);
}
function widget($args, $instance)
{
extract($args, EXTR_SKIP);
echo $before_widget;
//echo "<h1>This is my new widget!</h1>";
include \'inc/categories.php\';
echo $after_widget;
}
}
add_action( \'widgets_init\', create_function(\'\', \'return register_widget("Categories_Widget");\') );
这完全有效。在这个小部件中,我包括
categories.php
文件,如下所示:
<?php
$categories = get_categories();
foreach ( $categories as $category ) {
?>
<div class="categories-block category-<?php echo $category->name ?>">
<h4><?php echo $category->name ?></h4>
<ul>
<?php
$catId = $category->term_id;
global $post;
$args = array( \'category\' => $catId );
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata($post);
?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php
}
wp_reset_postdata();
?>
</ul>
</div>
<?php
}
出于某种原因,它显示类别名称,但不显示我要创建的查询。