WooCommerce产品类别链接

时间:2015-08-20 作者:Irina

我正在为存档产品创建模板。WooCommerce中的php页面。

我想有3个盒子,并将每个盒子链接到不同的产品类别。

How do I get dynamic link to each product category in <a> tag? 现在我放静态链接,但我确信有一种方法可以使它们在WordPress中保持动态

下面是我的代码:

    <ul class="shop-items">
      <li class="fine-art">
        <a href="http://url.com/product-category/categories/fine-art/">
         <div>Fine Art
          <span>Description</span>
          </div>
        </a>
       </li>
       <li class="dance-art">
        <a href="http://url.com/product-category/categories/dance-art/">
         <div>Dance Art
          <span>Description</span>
          </div>
        </a>
       </li>
     <li class="history-art">
        <a href="http://url.com/product-category/categories/history-art/">
         <div>History Art
          <span>Description</span>
          </div>
        </a>
       </li>
 </ul>

2 个回复
最合适的回答,由SO网友:Marek 整理而成

为此,有get_term_link 功能(documentation).

<a href="<?php echo get_term_link( 42 ,\'product_cat\') ?>">Fine Art ... etc.</a>
产品类别只是WP分类法,所以有很多功能可以使用。在这种情况下,您必须知道产品类别ID(实际上是分类术语ID)。编辑类别时,您将在URL中找到它:/编辑标记。php?操作=编辑(&A);分类法=product\\u目录(&U);tag\\u ID=42&安培;post\\u类型=产品

SO网友:R VC

我也在试着这么做

<?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\', \'customizer section title\', \'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__( \'Slider posts category\', \'olsen-light-child\' ),
        \'description\'   => esc_html__( \'Select the category that the slider will show posts from. If no category is selected, the slider will be disabled.\', \'olsen-light-child\' ),
    ) ) );
}

add_action( \'customize_register\', \'olsen_light_child_customize_register\' );
但是,当我在头版回显我的设置时,我网站的许多元素都消失了。。。,有什么线索吗,谢谢

<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>

结束

相关推荐

通过主题的函数激活插件。php?

是否可以激活每个主题的插件?我有一个多站点,我知道每个站点都可以激活插件。但我需要为特定主题启用插件。有人问了一个类似的问题-如果有可能通过其他插件激活插件:How To Activate Plugins via Code?这是一个公认的答案: //Activate a plugin programmatically - Akismet example function run_activate_plugin( $plugin ) { $current =