I want to get my Category images into my WooCommerce loop below but the array that's created from my code below doesn't seem to collect the image URL.
PHP
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
<?php foreach($catTerms as $catTerm) : ?>
<ul>
<li><?php echo $catTerm->name; ?></li>
</ul>
<?php endforeach; ?>
Would someone be kind enough to assist?
Thanks
Try this,
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'exclude' => '17,77')); ?>
<?php foreach($catTerms as $catTerm) :
$wthumbnail_id = get_woocommerce_term_meta( $catTerm->term_id,'thumbnail_id', true );
$wimage = wp_get_attachment_url( $wthumbnail_id );
?>
<ul>
<li><?php if($wimage!=""):?><img src="<?php echo $wimage?>"><?php endif;?><?php echo $catTerm->name; ?></li>
</ul>
<?php endforeach; ?>
Hope its works..
I think you have to put the UL tag out of the foreach to no repeat the UL for each category.
Related
Hi I am making an wordpress website where I am going to upload amount of pictures.
Problem is if I upload more than 1 image on the same post or page, wordpress makes a slideshow automatically 'and makes a slider thumbnail also'.
I don't want any slidershows on the post or page and thumbnail also shouldn't slide.
this is my slider code:
function rowling_flexslider($size) {
if ( is_page()) :
$attachment_parent = $post->ID;
else :
$attachment_parent = get_the_ID();
endif;
if($images = get_posts(array(
'post_parent' => $attachment_parent,
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => null,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
))) { ?>
<?php if ( !is_single() ) : // Make it a link if it's an archive ?>
<a class="flexslider" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php else : // ...and just a div if it's a single post ?>
<div class="flexslider">
<?php endif; ?>
<ul class="slides">
<?php foreach($images as $image) {
$attimg = wp_get_attachment_image($image->ID,$size); ?>
<li>
<?php echo $attimg; ?>
<?php if ( !empty($image->post_excerpt) && is_single()) : ?>
<p class="post-image-caption"><span class="fa fw fa-camera"></span><?php echo $image->post_excerpt; ?></p>
<?php endif; ?>
</li>
<?php }; ?>
</ul>
<?php
if ( !is_single() )
echo '</a>';
else
echo '</div>';
}
}
How should I do? I tried to delete php codes but it wasn't a good way.
How do i get the permalink from cubeportofolio, and the title/thumbnail from posts? With the post_type syntax from wordpress.
here is my code:
<?php
$posts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'cubeportfolio' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
until here the code is oke.
So i am getting the good permalink but my title is wrong, i havent even tried to get the thumbnail.
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I have read some things in the documentation but i still cant figure it out.
i want an ul so people can click through the posts but the problem is, i am making my blogs in an plugin. thats the wrong title and thumbnail, So i want the permalink of cubeportfolio(this is an post_type). and I want the title from posts(this is an post_type)
Hole code:
$posts = get_posts(array(
'post_type' => 'cubeportfolio',
'order' => 'date' ));
if( $posts ): ?>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php wp_reset_postdata(); ?>
<?php
$args = array(
'post_type'=> 'post',
'order' => 'date'
);
echo get_the_title($recent)."<br/>";
wp_reset_postdata();
?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Any help would be appreciated,
Id love to help you out but I would need to understand what it is you are trying to do. From the above code you are pulling in 1 "cubeportfolio" and then doing another query to pull in "posts" and then output their titles. If you are trying to create a unordered list of links to the "cubeportfolio" there is not need for the second query.
<?php $args = array(
'post_type' => 'cubeportfolio',
'posts_per_page' => 1,
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ) { ?>
<ul>
<?php while ( $the_query->have_posts() ) { $the_query->the_post();
<li>
<?php the_title(); ?>
</li>
<?php } wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
This will only pull in 1 of the "cubeportfolio" items because the query is set to 1 post_per_page. Hope this helps, if not please help me understand your plan and I can update the code to help you out.
I can not call custom field data into href.
Code:
enter code here
<?php foreach($books as $book) {?>
<li><img src="<?php echo $book ?>" /></li><?php } ?>
If you have each book as an actual page in wordpress with a parent page of books you could loop through the pages like this:
<?php
$books = get_pages( array( 'child_of' => $post->ID, 'sort_order' => 'desc' ) );?>
<ul>
<?php foreach( $books as $book ) {?>
<li><?php echo $book->post_title; ?></li>
<?php } // End ForEach ?>
</ul>
In your question you have an image as the link so you could add a post thumbnail to the single book page and then display that as the image in the link by changing the standard code to this:
<?php
$books = get_pages( array( 'child_of' => $post->ID, 'sort_order' => 'desc' ) );?>
<ul>
<?php foreach( $books as $book ) {?>
<li><?php echo get_the_post_thumbnail($book->ID, 'thumbnail'); ?></li>
<?php } // End ForEach ?>
</ul>
Currently using the following to show up specific Categories set up by WooCommerce but I would like to have another piece of script to show me specific Pages inside WordPress.
<?php $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'include' => '18, 15, 17, 16')); ?>
<?php foreach($catTerms as $catTerm) : ?>
$thumbnail_id = get_woocommerce_term_meta( $catTerm->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>
<li>
<a href="/<?php echo $catTerm->slug; ?>">
<div class="title"><?php echo $catTerm->name; ?></div>
<img src="<?php echo $image; ?>">
<p><?php echo $catTerm->description; ?></p>
</a>
</li>
<?php endforeach; ?>
I tried changing product_cat to page and post but still no luck at getting this to show me a loop of specific page content.
you use get_pages(); function to get pages like http://codex.wordpress.org/Function_Reference/get_pages
Hi I have a loop with testimonials using advanced custom fields. I need the loop to only loop one post at a time randomly i have tried query_posts but its doest work.
<?php
query_posts( 'posts_per_page=1&orderby=rand' );
if(get_field('testimonials', 'options')): ?>
<?php while(has_sub_field('testimonials', 'options')): ?>
<ul>
<li class="title"><?php the_sub_field('name'); ?></li>
<li class="site"><?php the_sub_field('website'); ?></li>
<li class="desc"><?php the_sub_field('message'); ?></li>
</ul>
<?php endwhile; ?>
<?php endif; ?>
There's a problem with the while loop, you should do it like this:
<?php
$posts = new WP_Query();
$posts->query('posts_per_page=1&orderby=rand');
if (have_posts()) :
while (posts->have_posts()) : $posts->the_post();
if(get_field('testimonials', 'options')): //Ain't no sure what does this ?>
<ul>
<li class="title"><?php the_sub_field('title'); ?></li>
<li class="site"><a href="<?php the_sub_field('website'); ?>" target="_blank">
<?php the_sub_field('website'); ?></a></li>
<li class="desc"><?php the_sub_field('message'); ?></li>
</ul>
<?php
endif;
break; // Exit loop after first post
endwhile;
endif;
?>
Look how i'm using the while loop. I don't understand what get_field does, you should pass the post ID as second parameter.
Try this for looping out one post per page:
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<ul>';
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
echo '<li class="title">'.the_sub_field('name'). '</li>';
echo '<li class="site">'.the_sub_field('website').'</li>';
echo '<li class="desc">'.the_sub_field('message').'</li>';
endwhile;
wp_reset_postdata();
I found the solution here :)
http://www.advancedcustomfields.com/resources/how-to/how-to-query-posts-filtered-by-custom-field-values/
<?php
// args
$args = array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
// get results
$the_query = new WP_Query( $args );
// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<?php the_title(); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>