-1
<?php
$args = array(
    'post_type' => 'product',
    'stock' => 1,
    'posts_per_page' => 4,
    'product_cat' => '<?php echo get_theme_mod('vatname', 'Clothing'); ?>',
    'orderby' =>'date',
    'order' => 'ASC'
);

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post();
global $product;
?>

<p><?php the_title(); ?></p>

<?php endwhile; wp_reset_query(); ?>

I'm trying to echo the product category name. The custom loop works fine but when I echo the function:

<?php echo get_theme_mod( 'vatname', 'Clothing' ); ?>

The loop stops. Can anyone help me? im trying to use wordpress cutomization api to enter the category name in loop examole 'product_cat' => 'clothing',

so i made text function im trying to eco the value in product_cat buy i give sybtax error

  • Please write your question in human terms. I've made your code readable, so you can see a syntax error there. – Max Yudin Aug 17 '16 at 14:43
  • 1
    'product_cat' => '<?php echo get_theme_mod('vatname', 'Clothing'); ?>', you're already in PHP... try 'product_cat' => get_theme_mod('vatname', 'Clothing'), – jgraup Aug 17 '16 at 14:48
  • @jgraup You could formulate that into a nice answer :) – Howdy_McGee Aug 17 '16 at 14:53
  • thank you but still syntax error (T_STRING), expecting – Scarlet Soza Aug 17 '16 at 14:56
  • im sorry for bad format in the post im just start working soon – Scarlet Soza Aug 17 '16 at 14:59
0

The function get_theme_mod() returns information. Since you want to assign it and not echo it to the screen, we can remove the echo and PHP tags:

$args = array(
    'post_type'      => 'product',
    'stock'          => 1,
    'posts_per_page' => 4,
    'product_cat'    => get_theme_mod( 'vatname', 'Clothing' ),
    'orderby'        =>'date',
    'order'          => 'ASC'
);

That should fix your query. Now the query is going to return Posts ( products in this case ) so if we want the categories we need to use a function like wp_get_post_terms() which returns an array of post ( product ) categories.

<?php
    while ( $loop->have_posts() ) :
        $loop->the_post();
        global $product;
        $category_array = wp_get_post_terms( $post->ID, 'product_cat' );
?>

        <p><?php the_title(); ?></p>
        <?php print_r( $category_array ); ?>

<?php
    endwhile;
wp_reset_query();
?>
  • @ScarletSoza You're welcome! If any of the posts here solved your issue, ensure to click the "checkmark" to show future readers which solution worked for you! – Howdy_McGee Aug 17 '16 at 15:14
  • Ha! missed my answer window – jgraup Aug 17 '16 at 15:43
0

Are the opening and closing php tags for the product_cat arg killing the statement? Would this work:

$clothing = get_theme_mod('vatname', 'Clothing');
$args = array(
    'post_type' => 'product',
    'stock' => 1,
    'posts_per_page' => 4,
    'product_cat' => $clothing,
    'orderby' =>'date',
    'order' => 'ASC'
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.