What exactly i want to know is how do i get my categories like in the picture. (The food types thumbnails.) I have a WordPress site with the Maya Shop theme which is based on Woo Commerce. I tried every which way i could from the menu and i hadn't managed to do it.
Also tried to fiddle a little bit with the Short code to no avail. I am new to this an i want to keep it as simple as possible. Do i have to write php code for some files or can i do it simpler than that?
That's not good answer. get_category_link() is not appropriate function to use for custom taxonomy. Function get_term_link() is what we need here.
<?php
$prod_categories = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1
));
foreach( $prod_categories as $prod_cat ) :
$cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
$term_link = get_term_link( $prod_cat, 'product_cat' );
?>
<img src="<?php echo $cat_thumb_url; ?>" alt="<?php echo $prod_cat->name; ?>" />
<?php endforeach; wp_reset_query(); ?>
I assuming that your theme doesn't have the code in place already to show the categories and their thumbnails on the homepage? If that is the case you will need to determine what template is being used and then most likely use some variation of the following code to build the display. Note: you will need to style and build any additional components to match your display exactly.
<ul>
<?php
$prod_categories = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1
));
foreach( $prod_categories as $prod_cat ) :
$cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
?>
<li><img src="<?php echo $cat_thumb_url; ?>" alt="<?php echo $prod_cat->name; ?>" /></li>
<?php endforeach; wp_reset_query(); ?></ul>
Related
I'm facing an issue with my website.
I would like to display all my custom post type named "Projets", and for each item, I want to get several term names to put in my data element.
Displaying all my post is not an issue, it's working well. I manage to display one term name using "get_terms()", but I don't know how to display several terms and put them in the right place.
I have 3 different custom taxonomy : city, typo and statut.
There is my code :
<?php
$args = array(
'post_type' => 'projets',
'posts_per_page'=>'99',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li class="content_item active" data-city="CITY_NAME_HERE" data-typo="TYPO_NAME_HERE" data-statut="STATUT_NAME_HERE">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('custom-size', ['class' => 'content_item_img', 'title' => 'Image du projet']); ?>
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile;
wp_reset_postdata();
?>
Thanks for the help!
Try this:
Inside the loop, you get the taxonomy like this (eq. "city"):
$cities = get_terms( array(
'taxonomy' => 'city',
'hide_empty' => false,
) );
You get an array of all the terms of the Taxo.
Where you need them, you have to loop them, here to give you an idea.
if(!empty($cities)){
foreach($cities as $city){
echo $city->name.', ';
}
}else{
echo 'No Cities.';
}
Comma Separated custom categories inside the loop.
<?php echo get_the_term_list( $post->ID, 'cutom_category', '', ', ', '' ) ?>
In wordpress i am creating a custom loop/query throughwhich i pass certain parameters. As i click through pages however the last page duplicates some posts/products inorder to satisfy the posts_per_page variable however i would like to specify that i dont want any repetitions. Is there a standard way to do this? It would seem like a pretty obvious point.
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'products', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<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 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 ); ?>
</li>
<?php endwhile; ?>
<?php previous_posts_link('« Previous', $loop ->max_num_pages);
next_posts_link(' Next »', $loop ->max_num_pages); ?>
<?php wp_reset_query();?>
It would seem like a pretty obvious point.
Not if you're using 'orderby' => 'rand' on your query, which is also very expensive by the way on large tables.
If you want to make sure that the items which already have been displayed will be excluded in the upcoming queries you'll need to save the post_ids which already has been displayed and pass them to the post__not_in parameter, see the codex page an search for post__not_in .
You could do something like this which should help you get the idea:
...
// don't forget to initialize the session somewhere
$already_displayed_post_ids = [];
if ( ! isset( $_SESSION['already_displayed_post_ids'] ) {
$_SESSION['already_displayed_post_ids'] = $already_displayed_post_ids;
} else {
$already_displayed_post_ids = array_merge( $already_displayed_post_ids, $_SESSION['already_displayed_post_ids'] );
}
$args = [
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => 'products',
'orderby' => 'rand',
'post__not_in' => $already_displayed_post_ids
];
$loop = new WP_Query( $args );
...
I've figured a way to enable adding categories to a PAGE (not a post). And I was just wondering if there was a way to display PAGES in a post loop, this is my code:
<?php query_posts('cat=540'); ?>
<div class="blog_module">
<?php if(has_post_thumbnail()) {
the_post_thumbnail(array(150,150));
} else {
echo '<img class="alignleft" src="'.get_bloginfo("template_url").'/images/empty_150_150_thumb.gif" width="150" height="150" />';
}
?>
<div class="entry">
<h3 class="blog_header"><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
<a class="button_link" href="<?php the_permalink(); ?>"><span>Read More</span></a>
</div>
</div>
However, this displays:
Which isn't what I want, it ONLY DISPLAYS POSTS and not the pages I have assigned to Category ID 540.
Please could someone help with a loop that would display pages that have been assigned to a category.
Thank you in advance.
Sure its easy. I guess the following should work (untested):
query_posts(array('cat'=>540,'post_type'=>page));
You can also use WP_Query which is more flexible than query_posts:
$q = new WP_Query(array('cat'=>540,'post_type'=>page));
while($q->have_posts()): $q->the_post();
// your post here
endwhile;
This should be even more flexible:
$args = array(
'post_type' => 'page',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 540
),
),
);
$query = new WP_Query( $args );
If you need posts AND pages try this:
'post_type' => 'any'
All information you need you can find on these links: https://developer.wordpress.org/reference/functions/query_posts/
https://codex.wordpress.org/Class_Reference/WP_Query
i have the following query to get all images from the "myportfoliotype" post type which works fine.
However i would like all images pulled in to be random when the page is loaded / refreshed.
I followed a few tutorials, and came up with the following code:
<?php
$query = new WP_Query( array( 'post_type' => 'myportfoliotype', 'posts_per_page' => -1, 'orderby' => 'rand' ) );
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$do_not_duplicate = $post->ID;
$thumb_id = get_post_thumbnail_id( $post_id );
$image_query = new WP_Query( array('post__not_in' => array (MultiPostThumbnails::has_post_thumbnail('myportfoliotype', 'logo'), $thumb_id ), 'orderby' => 'rand' , 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent' => get_the_ID() ) );
while( $image_query->have_posts() ) {
$image_query->the_post();
$do_not_duplicate = $post->ID;
//echo wp_get_attachment_image( get_the_ID() );
$image = wp_get_attachment_image_src(get_the_ID(), 'large');?>
<li> <a class="fancybox" rel="gallery1" href="<?php echo $image[0]; ?>"> <img src="<?php echo get_template_directory_uri(); ?>/js/timthumb.php?src=<?php echo $image[0]; ?>&w=137&h=137&f=2" alt="<?php the_title(); ?>" class="grey"/></a>
<img src="<?php echo get_template_directory_uri(); ?>/js/timthumb.php?src=<?php echo $image[0]; ?>&w=200&h=200" alt="<?php the_title(); ?>" class="color"/>
</li>
<?php
}
}
}
?>
I'm not quite sure if this is right? As mentioned before it pull in the images but not randomly....
Any help or guidance would be greatly appreciated.
Cheers, Dan
You probably have a plugin that hooks on posts_orderby filter.
If you don't want to disable this plugin, you can add 'suppress_filters'=>true to your WP_Query params.
I have solved my problem, and though i would post the answer incase anyone else was having the same problem using the "post type order" plugin.
In the setting page for "post type order" there is an option called "auto sort" you need to uncheck this this, and when making a query you should add the following:
$args = array(
'post_type' => 'post-type-here',
'orderby' => 'menu_order',
'order' => 'ASC'
);
As simple as that!
Thanks for all the help guys!
Although I have experience with PHP, WordPress is pretty new to me and I am struggling here.
I am trying to help a friend complete a theme. He works with several artists and each artist has a WP page named after the artist (i.e., Tom Jones) on which he wants to display only the posts about that artist. We define a tag for each artist ( Tom Jones ) with a slug like ( tom-jones )
Following the Codex, I am prepping the Loop in the template as follows:
$tags = get_tags();
//query_posts( array( 'tag' => $tag->slug ) );
query_posts( array( 'tag' => 'tom-jones' ) );
if( have_posts()) : while( have_posts() ) : the_post();
echo '<li id="feed<?php theID(); ?>" style="border-bottom:1px solid #404040;">';
echo '<table><tr><td width="40">';
echo '<img src="<?php echo get_post_meta($post->ID, 'image_path', true); ?>" />';
echo '</td><td><a href="';
the_permalink();
echo '">';
the_title();
echo '</a><br><span class="smTxt">Posted by ';
the_author();
echo ' on <em>';
the_time('F jS, Y');
echo '</em></span><br>';
the_excerpt();
echo '</td></tr></table></li>';
endwhile;
else:
echo '<h3>There are no posts.</h3>';
endif;
I would have thought that the query_post that is commented out would have grabbed the particular slug of the artist but it returns "No Posts". When I hard code as it is now, it works as expected.
Any help is greatly appreciated.
Try this
query_posts( array( 'tag' => get_query_var('tag') ) );
This post may help you.
Update:
As you said artist is a custom texonomy so try this
$the_tax = get_taxonomy( get_query_var( 'taxonomy' ) );
query_posts( array( 'artist' => $the_tax->labels->name ) );
Must use wp_reset_query() after the loop when you are using query_posts
You may read this post.
Update: (using WP_Query)
$the_tax = get_taxonomy( get_query_var( 'taxonomy' ) );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'artist',
'field' => 'slug',
'terms' => $the_tax->labels->name
)
)
);
$query = new WP_Query( $args );