如何在wordpress中显示自定义帖子分类中列出的类别中的所有类别名称和图像?
我创建了一个名为Products的自定义post类型和一个名为productcategories的自定义分类法。我在分类法产品类别中列出了许多产品类别名称及其图像。我想显示所有产品类别名称及其各自上传的图像(使用类别图像插件上传)。图像应上传到如何在wordpress中显示自定义帖子分类中列出的类别中的所有类别名称和图像?,wordpress,Wordpress,我创建了一个名为Products的自定义post类型和一个名为productcategories的自定义分类法。我在分类法产品类别中列出了许多产品类别名称及其图像。我想显示所有产品类别名称及其各自上传的图像(使用类别图像插件上传)。图像应上传到 有人能帮我吗 我附上了产品类别的截图 如果我理解正确,这是正确的方法, 首先,您需要在word中创建菜单按管理菜单空白菜单。现在转到function.php文件(主题文件),在其中添加以下代码 您可以通过此函数获取产品类别属性列表 function g
有人能帮我吗
我附上了产品类别的截图
如果我理解正确,这是正确的方法, 首先,您需要在word中创建菜单按管理菜单空白菜单。现在转到function.php文件(主题文件),在其中添加以下代码 您可以通过此函数获取产品类别属性列表
function get_product_terms( $term_id ) {
$html = '';
$args = array( 'hide_empty' => 0, 'parent' => $term_id );
$terms = get_terms('product_cat', $args);
foreach ($terms as $term) {
$html .= '<li';
if( $term_id == 0 ) {
$html .= ' class="top_li"';
}
$html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';
if( $list = get_product_terms( $term->term_id )) {
$html .= '<ul class="second_level">'.$list.'</ul>';
}
$html .= '</li>';
}
return $html;
}
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
// Woo function
/*//product_cat
$terms = get_terms( 'product_cat', $args );
print_r($terms);*/
if( $list = get_product_terms( 0 )) {
$menu1link = '<li class="home"><a href="' . home_url( '/' ) . '">' . __($list) . '</a></li>';
$homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
// add the home link to the end of the menu
$items = $items . $homelink;
$items = $items .$menu1link;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
函数获取产品术语($term\u id){
$html='';
$args=array('hide\u empty'=>0,'parent'=>$term\u id);
$terms=获取条款('product_cat',$args);
foreach($terms作为$term){
$html.='';
如果($list=get_product_terms($term->term_id)){
$html.='。$list.
;
}
$html.='';
}
返回$html;
}
您可以使用此功能将产品类别添加到菜单
function get_product_terms( $term_id ) {
$html = '';
$args = array( 'hide_empty' => 0, 'parent' => $term_id );
$terms = get_terms('product_cat', $args);
foreach ($terms as $term) {
$html .= '<li';
if( $term_id == 0 ) {
$html .= ' class="top_li"';
}
$html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';
if( $list = get_product_terms( $term->term_id )) {
$html .= '<ul class="second_level">'.$list.'</ul>';
}
$html .= '</li>';
}
return $html;
}
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
// Woo function
/*//product_cat
$terms = get_terms( 'product_cat', $args );
print_r($terms);*/
if( $list = get_product_terms( 0 )) {
$menu1link = '<li class="home"><a href="' . home_url( '/' ) . '">' . __($list) . '</a></li>';
$homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
// add the home link to the end of the menu
$items = $items . $homelink;
$items = $items .$menu1link;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
//筛选wp\u nav\u menu()以添加附加链接和其他输出
功能新建导航菜单项($items){
//宇函数
/*//产品目录
$terms=获取术语('product\u cat',$args);
打印(条款)*/
如果($list=获取产品条款(0)){
$menu1link=';
$homelink=';
//将主页链接添加到菜单的末尾
$items=$items.$homelink;
$items=$items.$menu1link;
}
退回$items;
}
添加过滤器(“wp导航菜单项”、“新建导航菜单项”);
如果我理解正确,这是正确的方法,
首先,您需要在word中创建菜单按管理菜单空白菜单。现在转到function.php文件(主题文件),在其中添加以下代码
您可以通过此函数获取产品类别属性列表
function get_product_terms( $term_id ) {
$html = '';
$args = array( 'hide_empty' => 0, 'parent' => $term_id );
$terms = get_terms('product_cat', $args);
foreach ($terms as $term) {
$html .= '<li';
if( $term_id == 0 ) {
$html .= ' class="top_li"';
}
$html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';
if( $list = get_product_terms( $term->term_id )) {
$html .= '<ul class="second_level">'.$list.'</ul>';
}
$html .= '</li>';
}
return $html;
}
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
// Woo function
/*//product_cat
$terms = get_terms( 'product_cat', $args );
print_r($terms);*/
if( $list = get_product_terms( 0 )) {
$menu1link = '<li class="home"><a href="' . home_url( '/' ) . '">' . __($list) . '</a></li>';
$homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
// add the home link to the end of the menu
$items = $items . $homelink;
$items = $items .$menu1link;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
函数获取产品术语($term\u id){
$html='';
$args=array('hide\u empty'=>0,'parent'=>$term\u id);
$terms=获取条款('product_cat',$args);
foreach($terms作为$term){
$html.='';
如果($list=get_product_terms($term->term_id)){
$html.='。$list.
;
}
$html.='';
}
返回$html;
}
您可以使用此功能将产品类别添加到菜单
function get_product_terms( $term_id ) {
$html = '';
$args = array( 'hide_empty' => 0, 'parent' => $term_id );
$terms = get_terms('product_cat', $args);
foreach ($terms as $term) {
$html .= '<li';
if( $term_id == 0 ) {
$html .= ' class="top_li"';
}
$html .= '><a href="'.get_term_link($term->slug, 'product_cat').'">' . $term->name . '</a>';
if( $list = get_product_terms( $term->term_id )) {
$html .= '<ul class="second_level">'.$list.'</ul>';
}
$html .= '</li>';
}
return $html;
}
// Filter wp_nav_menu() to add additional links and other output
function new_nav_menu_items($items) {
// Woo function
/*//product_cat
$terms = get_terms( 'product_cat', $args );
print_r($terms);*/
if( $list = get_product_terms( 0 )) {
$menu1link = '<li class="home"><a href="' . home_url( '/' ) . '">' . __($list) . '</a></li>';
$homelink = '<li class="home"><a href="' . home_url( '/' ) . '">' . __('Home') . '</a></li>';
// add the home link to the end of the menu
$items = $items . $homelink;
$items = $items .$menu1link;
}
return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );
//筛选wp\u nav\u menu()以添加附加链接和其他输出
功能新建导航菜单项($items){
//宇函数
/*//产品目录
$terms=获取术语('product\u cat',$args);
打印(条款)*/
如果($list=获取产品条款(0)){
$menu1link=';
$homelink=';
//将主页链接添加到菜单的末尾
$items=$items.$homelink;
$items=$items.$menu1link;
}
退回$items;
}
添加过滤器(“wp导航菜单项”、“新建导航菜单项”);
在单个产品页面上显示类别图像
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="absolute category-image" src="'.$image.'">';
}
$terms=get_the_terms($post->ID,'product_cat');
foreach($terms作为$term){
$category_name=$term->name;
$category\u thumbnail=get\u-woocmerce\u-term\u-meta($term->term\u-id,'thumbnail\u-id',true);
$image=wp\u get\u attachment\u url($category\u缩略图);
回声';
}
在单个产品页面上显示类别图像
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail);
echo '<img class="absolute category-image" src="'.$image.'">';
}
$terms=get_the_terms($post->ID,'product_cat');
foreach($terms作为$term){
$category_name=$term->name;
$category\u thumbnail=get\u-woocmerce\u-term\u-meta($term->term\u-id,'thumbnail\u-id',true);
$image=wp\u get\u attachment\u url($category\u缩略图);
回声';
}
我添加了这段代码,但不起作用。它显示的错误如下:“警告:为C:\xampp\htdocs\working files\rajab\wp content\themes\rajab\page templates\building.php中的foreach()提供的参数无效”
我添加了这段代码,但不起作用。它显示的错误如下:“警告:为C:\xampp\htdocs\working files\rajab\wp content\themes\rajab\page templates\building.php中的foreach()提供的参数无效”第二个功能从product category为菜单编写。
$list
已使用types插件创建了您想要的所有类别的自定义post类型“Products”,并将其分类为“product Categories”。然后我在分类法中的“产品类别”中添加了一组类别名称和使用类别图像插件的类别图像。我不知道如何在wordpress中显示它们的图像和类别名称?第二个功能从product category为菜单编写。$list
已经使用类型插件创建了您想要的所有类别自定义发布类型“Products”,并将其分类为“product categories”。然后,我在分类法中添加了一组类别名称,即“产品类别”和使用类别图像插件的类别图像。我不知道如何在wordpress中显示它们的图像和类别名称?请告诉我警告:为foreach()提供的参数无效在C:\xampp\htdocs\Working files\rajab\wp content\themes\rajab\page templates\building.phpits php foreach error中,您可以检查函数输出,只需print_r();请告诉我警告:为C:\xampp\htdocs\Working files\rajab\wp content\themes\rajab\page templates\building.phpits php foreach()中的foreach()提供的参数无效错误,您可以检查函数输出,只需打印即可;