• Resolved sroskylos

    (@sroskylo1)


    Hello

    How can i print the current taxonomy page brand name on wp_querry?

    'terms' => array( 'brand-name?????' )

    • This topic was modified 5 years, 11 months ago by sroskylos.
    • This topic was modified 5 years, 11 months ago by sroskylos.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor titodevera

    (@titodevera)

    Hi sotos.

    Here are two helpful code snippets for you::

    
    /*
     *	Get brand name in brand page
     */
    add_action( 'woocommerce_before_shop_loop', function(){
    
    	if( is_tax('pwb-brand') ){
    		$current_brand = get_queried_object();
    		echo '<h1>'.$current_brand->name.'</h1>';
    	}
    
    } );
    
    /*
     *	Get brands into a wp_query loop
     */
    add_action( 'init', function(){
    
    	$the_query = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => 1 ) );
    
    	if( $the_query->have_posts() ){
    	 while( $the_query->have_posts() ){
    		 $the_query->the_post();
    
    		 $brands = wp_get_object_terms( get_the_ID(), 'pwb-brand', array( 'fields' => 'names' ) );
    		 foreach( $brands as $brand ){
    			 echo '<h1>'.$brand.'</h1>';
    		 }
    	 }
    	 wp_reset_postdata();
    	}
    
    } );
    

    ??

    Thread Starter sroskylos

    (@sroskylo1)

    I try this code for display all products from this brand taxonomy page in one page. I dont know what to put on 'terms' => array( 'print-the-current-brand-name-from-page' ) //brand name here

    The full code is

    <?php
        // get products
        $args = array(
      'post_type'      => 'product',
      'posts_per_page' => -1,
      'tax_query'      => array(
        array(
          'taxonomy' => 'pwb-brand', //brands are terms of 'pwb-brand' taxonomy
          'field'    => 'name', //search by term name
          'terms'    => array( '' ) //brand names here
        )
      )
    );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
          while ( $loop->have_posts() ) : $loop->the_post();
            wc_get_template_part( 'content', 'product' );
          endwhile;
        } else {
          echo __( 'not found anyhting.' );
        }
        wp_reset_postdata();
      ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get brand name’ is closed to new replies.