Link către Categoria de Produse în WooCommerce
Creez un template pentru pagina archive-product.php în WooCommerce.
Aș dori să am 3 box-uri și să leg fiecare box de o categorie diferită de produse.
Cum pot obține un link dinamic către fiecare categorie de produse în tag-ul <a>
?
Momentan am pus link-uri statice, dar sunt sigur că există o modalitate de a le face dinamice în WordPress
Iată cum arată codul meu:
<ul class="shop-items">
<li class="fine-art">
<a href="http://url.com/product-category/categories/fine-art/">
<div>Artă Fină
<span>Descriere</span>
</div>
</a>
</li>
<li class="dance-art">
<a href="http://url.com/product-category/categories/dance-art/">
<div>Artă Dance
<span>Descriere</span>
</div>
</a>
</li>
<li class="history-art">
<a href="http://url.com/product-category/categories/history-art/">
<div>Artă Istorică
<span>Descriere</span>
</div>
</a>
</li>
</ul>

Pentru acest scop există funcția get_term_link
(documentația).
<a href="<?php echo get_term_link( 42 ,'product_cat') ?>">Arta Plastica ... etc.</a>
Categoria de produse este doar o taxonomie WordPress, așa că există o mulțime de funcții pentru a lucra cu ea. În acest caz, trebuie să cunoașteți ID-ul categoriei de produse (de fapt, ID-ul termenului de taxonomie). Când editați o categorie, îl veți găsi în URL: .../edit-tags.php?action=edit&taxonomy=product_cat&tag_ID=42&post_type=product

Încerc să fac același lucru
<?php
class My_Dropdown_Category_Control extends WP_Customize_Control {
public $type = 'dropdown-category';
protected $dropdown_args = false;
protected function render_content() {
?><label><?php
if ( ! empty( $this->label ) ) :
?><span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span><?php
endif;
if ( ! empty( $this->description ) ) :
?><span class="description customize-control-description"><?php echo $this->description; ?></span><?php
endif;
$dropdown_args = wp_parse_args( $this->dropdown_args, array(
'taxonomy' => 'product_cat',
'show_option_none' => ' ',
'selected' => $this->value(),
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'show_count' => 1,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'hierarchical' => 1,
'depth' => 0,
'tab_index' => 0,
'hide_if_empty' => false,
'option_none_value' => 0,
'value_field' => 'term_id',
) );
$dropdown_args['echo'] = false;
$dropdown = wp_dropdown_categories( $dropdown_args );
$dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
echo $dropdown;
?></label><?php
}
}
function olsen_light_child_customize_register( WP_Customize_Manager $wp_customize ) {
require_once get_stylesheet_directory() . '/inc/dropdown-category.php';
$wp_customize->add_section( 'homepage', array(
'title' => esc_html_x( 'Test-Link', 'titlu secțiune customizer', 'olsen-light-child' ),
) );
$wp_customize->add_setting( 'home_slider_category', array(
'default' => 0,
'sanitize_callback' => 'absint',
) );
$wp_customize->add_control( new My_Dropdown_Category_Control( $wp_customize, 'home_slider_category', array(
'section' => 'homepage',
'label' => esc_html__( 'Categoria postărilor pentru slider', 'olsen-light-child' ),
'description' => esc_html__( 'Selectați categoria din care slider-ul va afișa postări. Dacă nu este selectată nicio categorie, slider-ul va fi dezactivat.', 'olsen-light-child' ),
) ) );
}
add_action( 'customize_register', 'olsen_light_child_customize_register' );
dar când afișez setările pe pagina principală, multe elemente de pe site dispar... Aveți vreo idee, mulțumesc
<a href="<?php echo get_term_link(get_theme_mod('home_slider_category'))?>"><span><?php echo get_theme_mod('gs_slider_one_txt')?></span></a>
