在页面中,我需要显示指定类别的所有子类别的列表。
例如,在“运动”类别中,有6个子类别:
游泳、足球、篮球、滑雪、曲棍球每个子类别都有我想展示的特色图片、标题和描述。
我使用以下代码(在functions.php中)添加了特色图像:
add_action(\'init\', \'my_category_module\');
function my_category_module() {
add_action ( \'edit_category_form_fields\', \'add_image_cat\');
add_action ( \'edited_category\', \'save_image\');
}
function add_image_cat($tag){
$category_images = get_option( \'category_images\' );
$category_image = \'\';
if ( is_array( $category_images ) && array_key_exists( $tag->term_id, $category_images ) ) {
$category_image = $category_images[$tag->term_id] ;
}
?>
<tr>
<th scope="row" valign="top"><label for="auteur_revue_image">Image</label></th>
<td>
<?php
if ($category_image !=""){
?>
<img src="<?php echo $category_image;?>" alt="" title=""/>
<?php
}
?>
<br/>
<input type="text" name="category_image" id="category_image" value="<?php echo $category_image; ?>"/><br />
<span>This field allows you to add a picture to illustrate the category. Upload the image from the media tab WordPress and paste its URL here.</span>
</td>
</tr>
<?php
}
function save_image($term_id){
if ( isset( $_POST[\'category_image\'] ) ) {
//load existing category featured option
$category_images = get_option( \'category_images\' );
//set featured post ID to proper category ID in options array
$category_images[$term_id] = $_POST[\'category_image\'];
//save the option array
update_option( \'category_images\', $category_images );
}
}
category.php
<?php
if(is_category()){
$category_images = get_option( \'category_images\' );
$category_image = \'\';
if ( is_array( $category_images ) && array_key_exists( get_query_var(\'cat\'), $category_images ) ){
$category_image = $category_images[get_query_var(\'cat\')] ;
?>
<img src="<?php echo $category_image;?>"/>
<?php
}
}
?>
我需要一个这样的网格(这是最近的帖子)
有了它,我可以显示所有类别及其描述,但如何添加特色图像并仅显示某个类别父级的子类别?
<?php
$categories = get_categories( array(
\'orderby\' => \'name\',
\'order\' => \'ASC\'
) );
foreach( $categories as $category ) {
$category_link = sprintf(
\'<a href="%1$s" alt="%2$s">%3$s</a>\',
esc_url( get_category_link( $category->term_id ) ),
esc_attr( sprintf( __( \'View all posts in %s\', \'textdomain\' ), $category->name ) ),
esc_html( $category->name )
);
echo \'<p>\' . sprintf( esc_html__( \'Category: %s\', \'textdomain\' ), $category_link ) . \'</p> \';
echo \'<p>\' . sprintf( esc_html__( \'Description: %s\', \'textdomain\' ), $category->description ) . \'</p>\';}
是否也可以显示为网格?
谢谢
SO网友:ricotheque
How can I add the featured image?
从代码中可以看出,您已将所有类别特征图像存储在
category_images
选项所以你可以使用
get_option
要检索每个类别的特征图像,请执行以下操作:
$image_urls = get_option( \'category_images\', array() );
foreach ( $categories as $category ) {
$cat_id = $category->term_id;
$image_url = isset( $image_urls[$cat_id ) ? $image_urls[$cat_id] : \'\';
...
}
然后您可以使用
$image_url
作为
src
的属性
<img>
.
How can I display only the subcategories of a certain category parent?
喜欢
get_terms
,
get_categories
具有“child\\u of”参数。再一次,在你的
foreach
回路:
$sub_categories = get_categories( array(
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'child_of\' => $cat_id,
) );
foreach ( $sub_categories as $sub_category ) {
// Process the sub-categories here.
}
Is it possible to display these as a grid?
是的,你可以!但这是一个
HTML/CSS question 我认为这超出了这个问题的范围。继续,在该主题下提出一个新问题。你不能要求太多。:)
希望这有帮助!