WordPress纯代码实现多分类筛选(复选框方式)

WordPress纯代码实现多分类筛选(复选框方式)

方法一:使用分类ID控制选择显示的分类

要使用复选框方式让用户选择分类,‌你可以修改HTML表单部分,‌将<select>标签替换为<input type="checkbox">。‌以下是修改后的代码示例:‌

<!-- HTML表单部分(‌前端显示)‌-->
<form method="post" action="">
    <!-- 使用复选框方式选择分类 -->
    <?php
    // 获取所有分类
    $categories = get_categories();
    $allowed_categories = [1, 2, 3]; // 允许显示的分类ID
    foreach ($categories as $category) {
        if (in_array($category->term_id, $allowed_categories)) {
            echo '<label for="category_' . $category->term_id . '">' . $category->name . '</label>';
            echo '<input type="checkbox" id="category_' . $category->term_id . '" name="category_ids[]" value="' . $category->term_id . '"> ';
        }
    }
    ?>
    <input type="submit" value="筛选文章">
</form>
<!-- PHP处理逻辑部分(‌后端处理)‌‌-->
<?php  
// 处理表单提交并查询文章
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['category_ids'])) {
    // 从表单中获取分类ID数组
    $category_ids = $_POST['category_ids'];
    // 如果没有选择任何分类,‌则不显示任何内容
    if (empty($category_ids)) {
        echo '<p>未选择任何分类。‌</p>';
    } else {
        // 创建一个新的WP_Query对象来执行自定义查询
        $query = new WP_Query(array(
            'category__in' => $category_ids, // 使用'category__in'来指定文章必须属于这些分类中的任何一个
        ));

        // 循环遍历查询到的文章并显示
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                echo '<h2>' . get_the_title() . '</h2>';
                echo '<p>' . get_the_content() . '</p>';
            }
        } else {
            echo '<p>没有找到符合条件的文章。‌</p>';
        }

        // 重置文章数据
        wp_reset_postdata();
    }
}
?>

在这个示例中,‌每个分类都有一个对应的复选框,‌用户可以选择多个分类。‌表单提交后,‌PHP代码会处理这些分类ID,‌并使用WP_Query'category__in'参数来查询属于这些分类中的任何一个的文章。‌注意,‌这里使用了'category__in'而不是'category__and',‌因为用户可能选择多个分类,‌而我们想要显示属于这些分类中任何一个的文章。‌

方法二:使用分类ID控制选择显示的分类(显示摘要以避免内容过长)

以下是使用复选框方式让用户选择分类的完整代码示例:‌

<form method="post" action="">
    <!-- 使用复选框方式选择分类 -->
    <?php
    // 获取所有分类
    $categories = get_categories();
    $allowed_categories = [1, 2, 3]; // 允许显示的分类ID
    foreach ($categories as $category) {
        if (in_array($category->term_id, $allowed_categories)) {
            echo '<label for="category_' . $category->term_id . '">' . $category->name . '</label>';
            echo '<input type="checkbox" id="category_' . $category->term_id . '" name="category_ids[]" value="' . $category->term_id . '"> ';
        }
    }
    ?>
    <input type="submit" value="筛选文章">
</form>

<?php
// 处理表单提交并查询文章
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['category_ids'])) {
    // 从表单中获取分类ID数组
    $category_ids = $_POST['category_ids'];
    // 如果没有选择任何分类,‌则不显示任何内容
    if (empty($category_ids)) {
        echo '<p>未选择任何分类。‌</p>';
    } else {
        // 创建一个新的WP_Query对象来执行自定义查询
        $query = new WP_Query(array(
            'category__in' => $category_ids, // 使用'category__in'来指定文章必须属于这些分类中的任何一个
        ));

        // 循环遍历查询到的文章并显示
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                echo '<h2>' . get_the_title() . '</h2>';
                echo '<p>' . get_the_excerpt() . '</p>'; // 显示摘要以避免内容过长
            }
        } else {
            echo '<p>没有找到符合条件的文章。‌</p>';
        }

        // 重置文章数据
        wp_reset_postdata();
    }
}
?>

在这个示例中,‌每个允许显示的分类都有一个对应的复选框,‌用户可以选择多个分类。‌表单提交后,‌PHP代码会处理这些分类ID,‌并使用WP_Query'category__in'参数来查询属于这些分类中任何一个的文章。‌如果查询到了文章,‌它们会被循环遍历并显示出来。‌如果没有选择任何分类或没有查询到文章,‌会相应地显示提示信息。‌

方法三:使用分类ID控制选择显示的分类(显示子分类)

要修改代码以显示子分类,‌你需要使用get_categories()函数的参数来获取特定分类的子分类。‌你可以使用child_of参数来指定你想要获取其子分类的父分类ID。‌如果你想显示多个父分类下的子分类,‌你可以对每个父分类分别调用get_categories()。‌

以下是一个修改后的代码示例,‌它将显示指定父分类下的所有子分类,‌并使用复选框让用户选择:‌

<form method="post" action="">
    <!-- 使用复选框方式选择分类 -->
    <?php
    // 指定要显示子分类的父分类ID
    $parent_category_ids = [1, 2]; // 你可以根据需要修改这些ID

    foreach ($parent_category_ids as $parent_id) {
        // 获取指定父分类下的所有子分类
        $categories = get_categories(array(
            'child_of' => $parent_id,
            'hide_empty' => 0, // 即使分类下没有文章也显示
        ));

        if (!empty($categories)) {
            echo '<p>父分类ID: ' . $parent_id . ' 下的子分类:</p>';
            foreach ($categories as $category) {
                echo '<label for="category_' . $category->term_id . '">' . $category->name . '</label>';
                echo '<input type="checkbox" id="category_' . $category->term_id . '" name="category_ids[]" value="' . $category->term_id . '"> ';
            }
        }
    }
    ?>
    <input type="submit" value="筛选文章">
</form>

<?php
// 处理表单提交并查询文章
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['category_ids'])) {
    // 从表单中获取分类ID数组
    $category_ids = $_POST['category_ids'];
    // 如果没有选择任何分类,‌则不显示任何内容
    if (empty($category_ids)) {
        echo '<p>未选择任何分类。‌</p>';
    } else {
        // 创建一个新的WP_Query对象来执行自定义查询
        $query = new WP_Query(array(
            'category__in' => $category_ids, // 使用'category__in'来指定文章必须属于这些分类中的任何一个
        ));

        // 循环遍历查询到的文章并显示
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                echo '<h2>' . get_the_title() . '</h2>';
                echo '<p>' . get_the_excerpt() . '</p>'; // 显示摘要以避免内容过长
            }
        } else {
            echo '<p>没有找到符合条件的文章。‌</p>';
        }

        // 重置文章数据
        wp_reset_postdata();
    }
}
?>

在这个示例中,‌我们首先定义了一个包含父分类ID的数组$parent_category_ids。‌然后,‌我们遍历这个数组,‌对每个父分类ID调用get_categories()来获取其子分类。‌子分类会以复选框的形式显示出来,‌用户可以从中选择。‌表单提交后,‌PHP代码会处理这些分类ID,‌并查询属于这些分类中任何一个的文章。‌

方法四:在方法三的基础上修改成显示父分类的名称

<form method="post" action="">
    <!-- 使用复选框方式选择分类 -->
    <?php
    // 指定要显示子分类的父分类ID
    $parent_category_ids = [6, 28]; // 你可以根据需要修改这些ID
    foreach ($parent_category_ids as $parent_id) {
        // 获取父分类的名称
        $parent_category_name = get_cat_name($parent_id);
        
        // 获取指定父分类下的所有子分类
        $categories = get_categories(array(
            'child_of' => $parent_id,
            'hide_empty' => 0, // 即使分类下没有文章也显示
        ));
        
        if (!empty($categories)) {
            // 显示父分类名称和“下的子分类:”文本
            echo '<p>' . $parent_category_name . ':</p>';
            foreach ($categories as $category) {
                echo '<label for="category_' . $category->term_id . '">' . $category->name . '</label>';
                echo '<input type="checkbox" id="category_' . $category->term_id . '" name="category_ids[]" value="' . $category->term_id . '"> ';
            }
        }
    }
    ?>
    <input type="submit" value="筛选文章">
</form>

<?php
// 处理表单提交并查询文章
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['category_ids'])) {
    // 从表单中获取分类ID数组
    $category_ids = $_POST['category_ids'];
    // 如果没有选择任何分类,‌则不显示任何内容
    if (empty($category_ids)) {
        echo '<p>未选择任何分类。‌</p>';
    } else {
        // 创建一个新的WP_Query对象来执行自定义查询
        $query = new WP_Query(array(
            'category__in' => $category_ids, // 使用'category__in'来指定文章必须属于这些分类中的任何一个
        ));

        // 循环遍历查询到的文章并显示
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                echo '<h2>' . get_the_title() . '</h2>';
                echo '<p>' . get_the_excerpt() . '</p>'; // 显示摘要以避免内容过长
            }
        } else {
            echo '<p>没有找到符合条件的文章。‌</p>';
        }

        // 重置文章数据
        wp_reset_postdata();
    }
}
?>

方法五:在方法四的基础上修改下半部分代码显示:特色图片、发布时间、作者、摘要、标签、分类和添加链接

<?php
// 指定要显示子分类的父分类ID
$parent_category_ids = [6, 15, 28, 245]; // 你可以根据需要修改这些ID
?>

<form method="post" action="">
    <!-- 使用复选框方式选择分类 -->
    <?php foreach ($parent_category_ids as $parent_id) {
        // 获取父分类的名称
        $parent_category_name = get_cat_name($parent_id);
        // 获取指定父分类下的所有子分类
        $categories = get_categories(array(
            'child_of' => $parent_id,
            'hide_empty' => 0, // 即使分类下没有文章也显示
        ));
        if (!empty($categories)) {
            // 显示父分类名称和“下的子分类:”文本
            echo '<p>' . $parent_category_name . ':</p>';
            foreach ($categories as $category) {
                echo '<label for="category_' . $category->term_id . '">' . $category->name . '</label>';
                echo '<input type="checkbox" id="category_' . $category->term_id . '" name="category_ids[]" value="' . $category->term_id . '"> ';
            }
        }
    } ?>
    <input type="submit" value="筛选文章">
</form>

<?php
// 处理表单提交并查询文章
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['category_ids'])) {
    // 从表单中获取分类ID数组
    $category_ids = $_POST['category_ids'];
    // 如果没有选择任何分类,‌则不显示任何内容
    if (empty($category_ids)) {
        echo '<p>未选择任何分类。‌</p>';
    } else {
        // 创建一个新的WP_Query对象来执行自定义查询
        $query = new WP_Query(array(
            'category__in' => $category_ids, // 使用'category__in'来指定文章必须属于这些分类中的任何一个
        ));
        // 循环遍历查询到的文章并显示
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                // 显示特色图片
                if (has_post_thumbnail()) {
					// 使用默认的图片尺寸
					//the_post_thumbnail('thumbnail'); // 显示缩略图尺寸的图片
					//the_post_thumbnail('medium');    // 显示中等尺寸的图片
					//the_post_thumbnail('large');     // 显示大尺寸的图片
					//the_post_thumbnail('full');      // 显示原始尺寸的图片
					// 使用数组指定图片宽度和高度
					//$size = array(1500, 100); // 这里的100是示例,‌你可以根据需要设置具体的宽度和高度
					//the_post_thumbnail($size);
                }
                // 显示标题并添加链接
                echo '<h2><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h2>';
                // 显示发布时间和作者
                echo '<p>' . get_the_date() . ' 由 ' . get_the_author() . '</p>';
                // 显示摘要以避免内容过长
                echo '<p>' . get_the_excerpt() . '</p>';
				
				// 显示标签
                echo '<p>标签: ' . get_the_tag_list('', ', ') . '</p>'; 
				// 显示分类
                echo '<p>分类: ' . get_the_category_list(', ') . '</p>'; 
				
            }
        } else {
            echo '<p>没有找到符合条件的文章。‌</p>';
        }
        // 重置文章数据
        wp_reset_postdata();
    }
}
?>