Woocommerce sub categories - php

I am making a custom loop for my site using Woocommerce, I am using sub categories as a additional text field for the products but I can't find a function to just show sub categories, all are showing main and sub categories.
Can anyone help ?
<?php echo $product->get_categories(); ?>
Much appreciated :)
Here is the full code
<ul class="row-fluid">
<?php
$args = array ( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'product_cat' => 'news', 'orderby' => 'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="span">
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
<?php echo $product->get_categories(); ?>
<p class="price"><?php echo $product->get_price_html();?></p>
<a class="button add_to_cart_button product_type_simple"href="<?php the_permalink(); ?>">More...</a>
</a>
<div class="wer"><?php the_excerpt(); ?></div>
<span class="Cart"><?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?></span>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>

Related

How to display each category most recent post and change the order of categories whenever there is a new post in each category?

I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();

Wordpress loop showing blank page

I'm using the code below to display an Owl Carousel of pages that have the same parent page as the current page, it works but the first item in the loop is blank.
Any Ideas? Thanks
<?php
// [service_page_carousel]
function service_page_carousel_func() {
global $post;
$direct_parent = $post->post_parent;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $direct_parent,
'order' => 'ASC',
'orderby' => 'menu_order',
'post__not_in' => array( $post->ID ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<div class="service-page-owl-carousel owl-carousel owl-theme owl-loaded owl-drag">
<?php while ( $query->have_posts() ) : $query->the_post();
// Featured image
$feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full", true);
?>
<div class="item">
<div class="service">
<a href="<?php the_permalink(); ?>">
<div class="featured-img" style="background-image: url(<?php echo $feat_image[0]; ?>);"></div>
</a>
<a href="<?php the_permalink(); ?>">
<h3 class="brand-secondary"><?php the_title(); ?></h3>
</a>
<div class="text"><?php the_excerpt(); ?></div>
<p class="learn-more"><a class="btn primary" href="<?php the_permalink(); ?>">Learn More</a></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
<?php endif;
}
add_shortcode( 'service_page_carousel', 'service_page_carousel_func' );
i think you missed to add active class if only one page/post in owl carousel
<?php
// [service_page_carousel]
function service_page_carousel_func() {
global $post;
$direct_parent = $post->post_parent;
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $direct_parent,
'order' => 'ASC',
'orderby' => 'menu_order',
'post__not_in' => array( $post->ID ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<div class="service-page-owl-carousel owl-carousel owl-theme owl-loaded owl-drag">
<?php
$index = 0;
while ( $query->have_posts() ) : $query->the_post();
// Featured image
$feat_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "full", true);
?>
<div class="item <?php echo ($index == 0) ? 'active' : ''; ?>">
<div class="service">
<a href="<?php the_permalink(); ?>">
<div class="featured-img" style="background-image: url(<?php echo $feat_image[0]; ?>);"></div>
</a>
<a href="<?php the_permalink(); ?>">
<h3 class="brand-secondary"><?php the_title(); ?></h3>
</a>
<div class="text"><?php the_excerpt(); ?></div>
<p class="learn-more"><a class="btn primary" href="<?php the_permalink(); ?>">Learn More</a></p>
</div>
</div>
<?php endwhile;
$index++;
wp_reset_query(); ?>
</div>
<?php endif;
}
add_shortcode( 'service_page_carousel', 'service_page_carousel_func' );

the_post_thumbnail(); not working when calling post based on category in WordPress

<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC' ,
'cat' => '3',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="post-thumbnail" style="width: 250px;height: 147px;">
<?php
$attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) {
?>
<img src="<?php echo wp_get_attachment_url($image->ID);?>" > <?php
}
?> </a>
<div class="entry-header">
<h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3></br>
<div class="entry-content">
<?php the_excerpt(); ?>
<a href="<?php the_permalink();
?>">Read More</a>
</div>
</div>
</li>
</br></ul>
</article>
<?php
endwhile;
I am trying to display all post from category 3 to a particular page.
I am getting the title and excerpt correctly.But I am not getting the image correctly.
I first Used :
the_post_thumbnail();
But it didnt worked.
Then I used
$attimages = get_attached_media('image', $post->ID);
Then For I post,I got image so I printed $attimages and found empty array.
Any help
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC' ,
'cat' => '3',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a class="post-thumbnail" style="width: 250px;height: 147px;">
<?php $attimages = get_attached_media('image', $post->ID);
foreach ($attimages as $image) { ?>
<img src="<?php echo wp_get_attachment_url($image->ID);?>" >
<?php } ?>
</a>
<div class="entry-header">
<h3 class="entry-title"><a target="_self" href=<?php the_permalink(); ?>><?php the_title(); ?></a></h3>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</div>
</article>
<?php
endwhile;
endif;
?>

Wordpress show featured image sorted by subtitle

I try to sort this array by the subtitle. I want to show only featured images, where the subtitle is "anwesend". I tried different things, but nothing is working. Could you please support me in this task?
here is the code of this array.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
);
$pages = get_pages($args); ?>
<div style="max-width:1240px;">
<?php foreach( $pages as $page ) { ?>
<div class="box">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<span class="tagesplan_title"><?php echo $page->post_title; ?></span>
<center><span class="thumbnail_box"><?php echo get_the_post_thumbnail($page->ID, 'small-thumb'); ?></span></center>
<span class="status2"><?php echo get_the_subtitle($page); ?></span>
<span class="desc"><?php echo get_post_meta($page->ID, 'desc', true); ?></span>
</a>
</div>
<?php } ?>

wordpress - custom homepage with products in carousel

I'm using WooCommerce to build a webshop.
I made a custom homepage. On this homepage, I want to display the products per category in a carousel like structure(3 per view). But how do I get the data(products per category) into my own custom homepage? To be clear, this homepage comes before the product overview(or: 'archive-product.php').
Thanks!
Modified slightly from source: https://wordpress.stackexchange.com/a/67266/16086
You'll want to use something like this:
<ul class="products">
<?php
// Change product_cat to the slug of the category you want
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</h2>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
You can just change the product_cat item to the category you want and set posts_per_page to 3.

Categories