Taxonomy images from taxonomy list in WordPress - wordpress

I want to list all categories of my custom post type with its images. I tried many codes, but I can't get it working. With this given code I'm getting all categories as a list, but I'm not getting the value of my custom field. Someone please help me with this.
$post_type = 'product';
$tax = 'productcat';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$term_id = $tax_term->term_id;
$term_meta = get_option( 'taxonomy_' . $term_id );
$my_cf = $term_meta[ 'category_image' ];
echo $my_cf;
}
}

I think here is the answer for this problem.
<?php
foreach((get_the_category()) as $category) {
echo '<img src="http://example.com/images/' . $category->cat_ID . '.jpg" alt="' . $category->cat_name . '" />';
}
?>
For more information please click here.

Another answer for it, it is also here. it might be useful for you.
<?php
$args = array( 'type' => 'product',
'child_of' => 16,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'product_cat',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category):
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>
<div class="col-xs-6 col-sm-4 product-box"><img src="<?php echo $image; ?>"></div>
<?php
endforeach;
?>

Thank you so much for your code,
I got answer using you code, i did some modification also. this is the code
<div class="row">
<?php
$post_type = 'product';
$tax = 'productcat';
$args = array( 'type' => 'product',
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'productcat',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category):
$term_id = $category->term_id;
$variable = get_field('category_image', 'productcat_'.$term_id);
$imageURL = $variable['sizes']['medium'];
?>
<div class="col-sm-4 alt-no-padding-r-x">
<div class="products_item">
<div class="product_head">
<p><?php echo $category->name; ?></p>
</div>
<div class="top-img">
<img src="<?php echo $imageURL; ?>" />
</div>
<div class="product_desc">
<p><?php echo $category->description; ?></p>
</div>
<div class="product_list text-left no-decor">
<ul>
<?php
$args=array(
'post_type' => $post_type,
"$tax" => $category->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();?>
<li><?php the_title(); ?> </li>
<?php endwhile;
}
?>
</ul>
</div>
<div class="readmore-product no-decor">
Read More
</div>
</div>
<div class="plan-shadow"></div>
</div>
<?php
endforeach;
wp_reset_query();
?>
</div>

Related

Wordpress Custom post page with wp_pagenavi doesn't work

I have custom page for posts which has to show up 9 posts per page and I used wp_pagenavi plugin but it doesn't work. Please help!
<div class="page-content__wrapper">
<?php
$post_category = get_field('page_category');
$posts = get_posts( array(
'numberposts' => 9,
'category_name' => $post_category,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
//...posts
<?php
}
wp_pagenavi();
wp_reset_postdata();
?>
</div>
I used WP_Query() to solve this problem (without wp_pagenavy plugin :)
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup
$post_category = get_field('page_category'); // getting category name
$posts= new WP_Query(array(
'post_type'=> 'post',
'posts_per_page' => 9,
'paged' => $paged, // don't forget to give this argument
'category_name' => $post_category,
));
if($posts->have_posts()) :
while($posts->have_posts()) : $posts->the_post(); ?>
// All posts will be here!
<?php endwhile; ?>
<div class="mt-30 text-center">
<?php
$GLOBALS['wp_query'] = $posts;
the_posts_pagination(
array(
'mid_size' => '2',
'prev_text' => '<i class="fa fa-hand-o-left"></i> <',
'next_text' => '> <i class="fa fa-hand-o-right"></i>',
'screen_reader_text' => ' '));
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

How to specify which page to get the gallery images from using wp_get_attachment_image_src function

The code currently gets all the images from the media gallery. I am trying to get the images from a gallery on a specific page. How can I achieve that?
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '30',
'post_status' => 'inherit'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = wp_get_attachment_image_src( get_the_ID(), $size = 'full');
echo
"<div class='col-sm-6 col-md-4 col-lg-3'>
<div class='gallery-img-wrap'>
<img src='" . $image[0] . "'>
</div>
</div>";
endwhile;
?>
<?php
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => '30',
'post_status' => 'inherit',
'post_parent' => 52 //page id
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image = wp_get_attachment_image_src( get_the_ID(), $size = 'full');
echo
"<div class='col-sm-6 col-md-4 col-lg-3'>
<div class='gallery-img-wrap'>
<img src='" . $image[0] . "'>
</div>
</div>";
endwhile;
?>

Exclude Draft Post in related Post Wordpress

I am currently using a custom code for related post filtered by category that displays 4 related posts.
My code works fine except it also displays the draft posts. which ideally it should and is a bit frustrating. Here is my code for the related post.
<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $tag_ids
)
),
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
);
// query posts
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
?>
<div class="related-post-title">
<?php
echo "<h2>Related Health Hub Articles</h2>";
?>
</div>
<?php
while( $my_query->have_posts() ) { $my_query->the_post();
// display each post
?>
<div class="related-post-thumb col-sm-3">
<a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
<div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<h4 class="related-title"><?php the_title();?></h4>
</a>
</div>
<?php
}
}
}
wp_reset_postdata();
?>
</div>
I have reviewed your code. you have missed the post_status column. Anything with the status "publish" is what you want.
Please add 'post_status' => 'publish' in $args array.
You can see built-in WordPress functions.
http://codex.wordpress.org/Integrating_WordPress_with_Your_Website
Try this code
<div class="relatedposts">
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $category_ids
),
array(
'taxonomy' => 'post_tag',
'field' => 'id',
'terms' => $tag_ids
)
),
'post__not_in' => array($post->ID),
'posts_per_page'=> 4, // Number of related posts that will be shown.
'post_status' => 'publish'
);
// query posts
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
?>
<div class="related-post-title">
<?php
echo "<h2>Related Health Hub Articles</h2>";
?>
</div>
<?php
while( $my_query->have_posts() ) { $my_query->the_post();
// display each post
?>
<div class="related-post-thumb col-sm-3">
<a href='<?php the_permalink(); ?>' rel='canonical' class="related-wrapper">
<div class="related-thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
<h4 class="related-title"><?php the_title();?></h4>
</a>
</div>
<?php
}
}
}
wp_reset_postdata();
?>
</div>
$search_query=get_search_query();
function __extra_where($sql){
global $wpdb;
return ' AND '.$wpdb->prefix.'posts.post_status="publish" AND ( 1=1 '.$sql.' ) ';
}
add_filter( 'posts_where', '__extra_where', 10, 2 );
$query = new WP_Query(array(
'post_status' => array( 'publish' ),
'post_type' => ['post'/*your post_type here */],
's'=>$search_query,
'order' => 'ASC'
));
remove_filter( 'posts_where', '__extra_where', 10 );

How to list all category from custom post type?

I have a post type called 'dining' and has a taxonomy called 'dining-category'.
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
In WordPress 4.6 get_terms is deprecated. So there is an alternate of this (get_categories) Read this
And here is Example code:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><?php echo $category->name; ?></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><?php echo $wcatTerm->name; ?></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
use get_terms() function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.

WordPress Getting Page Images

Having a little trouble here, I'm wanting to get all images from posts and display them in two separate locations, one location showing one image and the second location showing all the image.
If i use the Query it grabs all post image and with out using Query it grabs one image from the page. any ideas on how to get all images from page instead of posts???
<div id="fp_gallery" class="fp_gallery">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $args = array( 'numberposts' => 1, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' =>
$post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { ?>
<img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt="" class="fp_preview" style=""/>
<?php }}?>
<?php endwhile; endif; ?>
<div class="fp_overlay"></div>
<div id="fp_loading" class="fp_loading"></div>
<div id="fp_next" class="fp_next"></div>
<div id="fp_prev" class="fp_prev"></div>
<div id="outer_container">
<div id="thumbScroller">
<div class="container">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $args = array( 'numberposts' => 9999, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ( $attachments as $attachment ) { ?>
<div class="content">
<div>
<a href="#" title="<?php the_title(); ?>">
<img src="<?php echo get_bloginfo('template_url');?>/js/timthumb.php?src=<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>&h=120&w=150&zc=1" alt="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" class="thumb"/>
</a>
</div>
</div>
<?php }}?>
<?php endwhile; endif; ?>
</div>
</div>
</div>
</div>
<div id="fp_thumbtoggle" class="fp_thumbtoggle" title="View Thumbs">↑</div>
Any help would be great : )
Make numberposts -1 it will collect all posts under category
$args = array( 'numberposts' => 1, 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' =>
$post->ID );
But i'm not sure about jQuery thing you are talking about.

Resources