【发布时间】:2019-11-26 10:38:06
【问题描述】:
我在 Wordpress 中工作并实现了一些 PHP 来显示页面上的所有类别(通过简码)。通过单击一个类别,它会链接到一个显示该类别所有帖子的新页面。
如何仅显示某些类别,例如按类别的 id 和/或名称?
一篇文章有两个类别(a:A & B 或 A & C)。所以一篇帖子a总是有一个类别A,一个类别B或C。
这是我的代码:
function swerft_categories( ){
ob_start();
$categories = get_categories();
echo '<div class="swerft_cat">';
foreach($categories as $category) {
echo '<div class="swerft_cat_single col-lg-3 col-md-3 col-sm-6 col-xs-12">';
echo '<div class="swerft_cat_single_inner">';
$thim_group_custom_title_bg_img = get_term_meta( $category->term_id, 'thim_group_custom_title_bg_img', true );
if ($thim_group_custom_title_bg_img) {
$image_id = $thim_group_custom_title_bg_img['id'];
if ($image_id) {
$post_thumbnail_img = wp_get_attachment_image_src( $image_id, 'full' );
echo '<a href="' . get_category_link($category->term_id) . '"><img src="' . $post_thumbnail_img[0] . '" alt="' . $category->name . '" /></a>';
}
}
echo '<a href="' . get_category_link($category->term_id) . '"><h5>'. $category->name .'</h5></a>';
echo '<p>'. $category->description . $category->count . '<span> Seminare </span>' . '</p>';
echo '</div>';
echo '</div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'swerft_categories', 'swerft_categories' );
例如,我在前几行中尝试过,但没有成功:
function swerft_categories($args){
ob_start();
$args = array('hide_empty'=> 1,
'name' => 'B');
$categories = get_categories($args);
1) 我只想显示一个特定的关系。假设:只有 a 的关系:A & B 2)我希望计数仅显示基于上述关系的帖子数量。 3)通过点击基于这种关系的类别,我当然希望只显示那些帖子。
【问题讨论】:
-
嘿,非常感谢您的链接。同时我找到了解决方案;)
标签: php wordpress get categories