This a part of a program to display a product list. I created the products as custom posts, and I need to display the posts on the basis of taxonomies.
Who can I provide taxonomies as an argument in post selection arguments?
I created taxonomies and posts using tool set plugin.
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<h1>
<?php echo $taxonomy->name;
//$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
//how can i add taxonomies asan argument here
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>
Here is the working code
<?php $taxonomies = get_terms([
'taxonomy' => 'product_category',
'parent' => 0,
'orderby' => 'name',
'order' => get_query_var('Order', 'ASC'),
'hide_empty' => false,
]);
?><?php
$i=1;
if ($taxonomies) {
foreach ($taxonomies as $taxonomy ) {; ?>
<?php
if(!empty(do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]'))){
$pro_cat_img = do_shortcode('[types term_id="'.$taxonomy->term_id.'" termmeta="secondary-category-image" size="product-main-cat" url="true"][/types]');
}else{
$pro_cat_img = get_template_directory_uri()."/img/product-guitar.png";
} ?>
<div class="product-row clearfix">
<div class="bg-box">
<?php if($i==1){?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-guitar"/>
<?php }else if($i==2){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-speaker"/>
<?php }else if($i==3){ ?>
<img src="<?php echo esc_url($pro_cat_img); ?>" class="product-headphone"/>
<?php } ?>
<a href="<?php echo esc_url( add_query_arg( array('cat'=>$taxonomy->term_id), home_url('/products') ) ); ?>" class="view-more-btn button button--moema ">
READ MORE
</a>
</div>
<div class="items-list-box">
<h1>
<?php echo $taxonomy->name;
$ir=$taxonomy->name;
?>
</h1>
<ul>
<?php
// args
$args = array(
'posts_per_page' => 5,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'product_category',
'field' => 'name',
'terms' => $ir,
)
),
);
// query
$the_query = new WP_Query( $args );
$count = $the_query->post_count;
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) : $the_query->the_post();
if(has_post_thumbnail(get_the_ID())) {
$feature_image_full = simplexml_load_string(get_the_post_thumbnail(get_the_ID(), 'banner_img'));
$pr_img_source = $feature_image_full->attributes()->src;
?>
<li>
<span>
<?php echo the_title(); ?>
</span>
</li>
<!-- <img src="<?php //echo esc_url($pr_img_source); ?>" alt="<?php the_title(); ?>">
-->
<?php } endwhile; endif; wp_reset_query(); ?>
</ul>
Related
I am using get_pages to fetch some data from the children pages of a parent in WordPress (like a custom loop) - but it doesnt work when trying to fetch some data as set by the Advanced Custom Fields plugin for some strange reason... Is there an alternative / better way to acheive what I want? Code below works apart from fetching the ACF field called 'job_title'.
<?php
$args = array(
'parent' => $post->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish',
'sort_order' => 'DESC',
'sort_column" => "post_name',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$pages = get_pages($args); ?>
<div class="childrenFetchedLoopWrapper">
<?php foreach( $pages as $page ) { ?>
<div class="feedItemWrapper wpb_animate_when_almost_visible wpb_fadeInUp fadeInUp" style="background-image: url('<?php echo get_the_post_thumbnail_url($page->ID, 'full'); ?>')">
<a href="<?php echo get_permalink($page->ID); ?>" rel="bookmark" title="<?php echo $page->post_title; ?>">
<img src="/wp-content/themes/salient-child/images/aspectTrans.png" alt="*" title="*" />
<h3><?php echo $page->post_title; ?></h3>
<p><?php the_field('job_title'); ?></p>
</a>
</div>
<?php } ?>
</div>
Replace <?php the_field('job_title'); ?> with <?php the_field('job_title', $page->ID); ?>.
OR
You can use WP_Query for alternative solution.
or you can also get acf value using get_post_meta();
Try Out this code. I hope it helps.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$parent = new WP_Query( $args );
if ( $parent->have_posts() ) : ?>
<?php while ( $parent->have_posts() ) : $parent->the_post();
$id = get_the_ID(); ?>
<p><?php the_field('job_title', $id); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
I created a custom post type called Recipes but my posts are showing as "page not found".
This is a link to one of the posts I created - http://pt1-dev.info/recipes/festive-napa-cabbage/ But it shows as page not found.
Also my theme doesn't show on my page. (It's just a test site to make this work so only the navigation and footer box should show)
Here is my code -
// Register Custom Post Types
add_action('init', 'register_custom_posts_init');
function register_custom_posts_init() {
// Register Recipes
$recipes_labels = array(
'name' => 'Recipes',
'singular_name' => 'Recipe',
'menu_name' => 'Recipes'
);
$recipes_args = array(
'labels' => $recipes_labels,
'public' => true,
'capability_type' => 'post',
'show_in_menu' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions' )
);
register_post_type('recipes', $recipes_args);
}
add_action( 'init', 'custom_post_type', 0 );
function tr_create_my_taxonomy() {
register_taxonomy(
'recipes-category',
'recipes',
array(
'label' => __( 'Recipe Category' ),
'rewrite' => array( 'slug' => 'recipes-category' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'tr_create_my_taxonomy' );
This is what I have the file recipes.php I created -
<?php
// Posts are found
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) :
$posts->the_post();
global $post;
?>
$args = array(
'post_type' => 'recipes',
'post_status' => 'publish',
'posts_per_page' => '10'
);
$recipes_loop = new WP_Query( $args );
if ( $recipes_loop->have_posts() ) :
while ( $recipes_loop->have_posts() ) : $recipes_loop->the_post();
// Set variables
$title = get_the_title();
$description = get_the_content();
$recipeby = get_field('recipe_by');
$category = get_field('category');
$recipe_description = get_field('recipe_description');
$image = get_field('recipe_image');
if (!empty($image)) {
$url = get_field('hlink');
?>
<a href="<?php echo $url; ?>" target="_blank"><img src="<?php
echo $image['url']; ?>" alt="<?php
echo $image['alt']; ?>" /></a>
<?php
}
$yields = get_field('yields');
$prep = get_field('prep_time');
$cook = get_field('cook_time');
$total = get_field('total_time');
$ingredients = get_field('ingredients');
$instructions = get_field('instructions');
endwhile;
wp_reset_postdata();
endif;
Any help would be appreciated!
Thank you!
Updated single-recipe.php file -
<?php
/*
* Template Name: Recipes
*/
get_header(); ?>
<div id="primary" class="clearfix">
<div id="content" role="main" class="clearfix">
<header class="blog-entry-header clearfix">
<?php if ( is_sticky() ) : ?>
<hgroup>
<h2 class="entry-title"><?php the_title(); ?></h2>
<h3 class="entry-format"><?php _e( 'Featured', 'azurebasic' ); ?></h3>
</hgroup>
<?php else : ?>
<h1 class="blog-entry-title clearfix"><?php the_title(); ?></h1>
<?php while ( have_posts() ) : the_post(); ?>
</header><!-- .entry-header -->
$args = array(
'post_type' => 'recipes',
'post_status' => 'publish',
'posts_per_page' => '10'
);
$recipes_loop = new WP_Query( $args );
if ( $recipes_loop->have_posts() ) :
while ( $recipes_loop->have_posts() ) : $recipes_loop->the_post();
// Set variables
$title = get_the_title();
$description = get_the_content();
$recipeby = get_field('recipe_by');
$category = get_field('category');
$recipe_description = get_field('recipe_description');
$image = get_field('recipe_image');
if (!empty($image)) {
$url = get_field('hlink');
?>
<a href="<?php echo $url; ?>" target="_blank"><img src="<?php
echo $image['url']; ?>" alt="<?php
echo $image['alt']; ?>" /></a>
<?php
}
$yields = get_field('yields');
$prep = get_field('prep_time');
$cook = get_field('cook_time');
$total = get_field('total_time');
$ingredients = get_field('ingredients');
$instructions = get_field('instructions');
endwhile;
wp_reset_postdata();
endif;
<nav id="nav-single" class="clearfix">
<h4 class="assistive-text"><?php _e( 'Post Navigation', 'azurebasic' ); ?></h4>
<span class="nav-next"><?php next_post_link( '%link', __( '<span class="meta-nav">←</span> Next', 'azurebasic' ) ); ?></span>
<span class="nav-previous"><?php previous_post_link( '%link', __( 'Previous <span class="meta-nav">→</span>', 'azurebasic' ) ); ?></span>
</nav><!-- #nav-single -->
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<div class="clear"></div><!-- .clear the floats -->
<?php get_footer(); ?>
I am trying to get all products with a specific category. It shows the data if I remove the product category code from the code else doesn't show anything. I am sure I am using the right product category.
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'men'
),
),
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$tre = get_post_meta(get_the_ID(), '_xoo-wl-users', true );
$data = json_decode($tre);
foreach($data as $key => $value)
{
echo $key;
echo "<br>";
}
}
}
Use below code in $args so it will display products only of men category
<?php $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'men', 'orderby' => 'date' ); ?>
Try this :
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'men', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $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 woocommerce_show_product_sale_flash( $post, $product ); ?>
<?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 ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
The reference here: https://wordpress.stackexchange.com/questions/67247/how-to-display-product-specific-to-a-category-with-woocommerce-plugin
Can someone help me how to display the post only for a week? i have this code that works fine in post_date:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'after' => '1 week ago'
)
)
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
but how to do it in the custom field date. Because my other events is posted a week before the events. Can someone help me?
Thanks.
This Might help you to get solution:
$week = date('W');
$year = date('Y');
$the_query = new WP_Query( 'year=' . $year . '&w=' . $week );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The previous answer was along the good track. Just modify your date_query:
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'cat' => 1,
'orderby' => 'date',
'order' => 'DESC',
// Using the date_query to filter posts from last week
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
);
?>
<ul class="weekly-list">
<?php $the_query = new WP_Query( $args ); ?>
<?php while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
I have displayed the custom post type in the widget. Now i want to add pagination in the last. because i have more than 10 posts in my custom post type.
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
wp_reset_postdata(); ?>
<?php endif; ?>
</ul>
Try this one and enter your custom post type's name in 'post_type' => 'your custom post type name'
<ul class="posts-list">
<?php if (have_posts()) : ?>
<?php
global $post;
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$cats = get_the_category();
$cat_name = $cats[0]->name;
$args = array(
'posts_per_page' => 10,
'offset' => 0,
'category' => $cat_name,
'orderby' => 'post_date',
'paged' => $paged1,
'post_type' => 'your custom post type name'
'order' => 'DESC',
'post_status' => 'publish',
'suppress_filters' => true );
$previous_post = get_posts($args);
foreach ( $previous_post as $post ) :
setup_postdata( $post ); ?>
<li>
<h5><?php the_title(); ?></h5>
<p>posted on <?php the_time(' F jS, Y') ?> by <?php the_author(); ?></p>
<p>Posted in <?php echo $cat_name->name; $cat_name = get_the_category($post->ID); ?></p>
</li>
<?php endforeach;
?>
<?php endif;
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $previous_post->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args1 );
wp_reset_postdata(); ?>
</ul>