How do I display taxonomy info from a wp-query? - php

I've got a wp-query that works a treat on a small section of my website's dashboard - it displays a list of repeat jobs (CPTs) that are due in 6 weeks time - I'm having issues with displaying a taxonomy related to the post.
So far I've got
<?php
// get posts
$before_date = date("Ymd", strtotime("+6 weeks"));
$posts = get_posts(array(
'post_type' => 'pre_jobs',
'posts_per_page' => -1,
'meta_key' => 'pre_job_due_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'pre_job_due_date',
'value' => $before_date,
'compare' => '<',
),
),
'tax_query' => array(
array(
'taxonomy' => 'pre_job_status',
'field' => 'slug',
'terms' => array( 'repeat' )
),
),
));
if( $posts ): ?>
<hr>
<div class="dashpanel">
<div class="duedate-head">Due Date</div>
<div class="jobnumber-head">Job Type</div>
<div class="client-head">Client/Requestor</div>
<div class="customer-head">Customer</div>
</div>
<hr>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $job_type = get_field('pre_job_job_type', $client->ID ); ?>
<?php $customer = get_field('pre_job_customer', $client->ID ); ?>
<?php $job_client = get_field('pre_job_requestor', $client->ID ); ?>
<div class="dashpanel">
<a href="<?php the_permalink(); ?>">
<div class="duedate"><?php the_field('pre_job_due_date'); ?></div>
<div class="jobnumber"><?php echo $job_type[0]->post_title; ?></div>
<div class="client"><?php echo $job_client[0]->post_title; ?></div>
<div class="customer"><?php echo $customer[0]->post_title; ?></div></a>
</div>
<hr>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p>No upcoming jobs to book in.</p>
<?php endif; ?>
I'm not sure where I need to put the
<?php
$pre_job_type = get_field('pre_job_job_type');
if( $term ): ?>
Code - every time I add this code it breaks. Or am I going completely wrong somewhere?

You can add this code after you open the if ($posts) loop:
It will show the taxonomy to which this publication is related. with a link to it.
If you prefer, you can remove the link and display only the title of the term with the call $term->name
<?php
$terms = get_the_terms( $post->ID, 'YOUR_TAXONOMY_HERE' );
if ( $terms != null ) {
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'YOUR_TAXONOMY_HERE' );
echo '<li>' . $term->name . ' ' . $term->term_id . ' ' . $term->count . '</li>';
unset( $term );
}
}
?>

Have you created your expected taxonomy? If haven't at first create the taxonomy on your functions.php file or where you have created your custom post type.
Example:
/**
* Register a private 'Genre' taxonomy for post type 'book'.
*
* #see register_post_type() for registering post types.
*/
function wpdocs_register_taxonomy() {
$args = array(
'label' => __( 'Job Type', 'textdomain' ),
'public' => true,
'rewrite' => true,
'hierarchical' => true
);
register_taxonomy( 'pre_job_job_type', 'pre_jobs', $args );
}
add_action( 'init', 'wpdocs_register_taxonomy', 0 );
Or OOP approach:
public function wpdocs_register_taxonomy() {
$args = array(
'label' => __( 'Job Type', 'textdomain' ),
'public' => true,
'rewrite' => true,
'hierarchical' => true
);
register_taxonomy( 'pre_job_job_type', 'pre_jobs', $args );
}
/**
* When class is instantiated
*/
public function __construct() {
add_action('init', array($this, 'wpdocs_register_taxonomy'));// Register texonomy
}
Now your taxonomies are ready to display. Place this code where ever you want to display the taxonomies.
$pre_job_types = get_categories('taxonomy=pre_job_job_type&post_type=pre_jobs');
foreach ($pre_job_types as $job_type ) : ?>
<span> <?php echo $job_type->name; </span>
endforeach;
If you want to query your custom post with the taxonomy, follow this:
$args = array(
'post_type' => 'pre_jobs',
'status' => 'published',
'tax_query' => array(
array(
'taxonomy' => 'pre_job_job_type',
'field' => 'name',
'terms' => array( 'repeat' )
)
)
);
$query = new WP_Query( $args );
And then continue as a normal query. This query only display the posts whose taxonomy is "repeat".

Related

How to display all posts assigned the same category?

I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.
Could anyone assist with this?
Try this code:
And modify as per your requirements:
add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {
$query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( '18' ), //Your custom category id
),
),
)
);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
}
}
wp_reset_postdata();
}
Use this code to show all posts assigned to the category cable
<?php
global $post;
$args = array( 'numberposts' => 10, 'category_name' => 'cable' );
$posts = get_posts( $args );
foreach( $posts as $post ): setup_postdata($post);
?>
<div>
<?php
the_title();
the_excerpt(); ?>
</div>
<?php endforeach; ?>
I have created with list
add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1, //--1 for unlimited and number to limit
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
),
),
));
if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
$pi_query ->the_post();
echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}
You can use WP_Query() with tax_query().
EDIT:
Try out this code to get products on category page.
add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
if(!is_product_category()){
return;
}
$term = get_queried_object();
ob_start();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
'operator' => 'IN'
)
);
$the_query = new WP_Query($args);
// The Loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
echo '<h3>' . get_the_title() . '</h3>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}

Get Products by Product category in a shortcode with WooCommerce

I have a shortcode, I would like to get all the products from a specific Woocommerce category.
add_shortcode( 'list-products', 'prod_listing_params' );
function prod_listing_params( $atts ) {
ob_start();
extract( shortcode_atts( array (
'type' => 'product',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '',
), $atts ) );
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'product_cat' => $product_cat,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<div class="#">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="#">
<span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</span></p>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<?php
$myvar = ob_get_clean();
return $myvar;
}
}
So I use the shortcode:
[list-products category="shoes"]
But it returns all the products from all categories despite providing the category in the shortcode.
How can I amend this to get by category?
Thanks
Instead of 'product_cat' => $product_cat, you should use a tax_query this way:
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['cat'],
) ),
So your code should be something like (I have revisited a bit your code):
// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('prod_listing_params') ) {
function prod_listing_params( $atts ) {
ob_start();
$atts = shortcode_atts( array (
'type' => 'product',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '', // category slug
), $atts, 'list_products' );
$query = new WP_Query( array(
'post_type' => $atts['type'],
'order' => $atts['order'],
'orderby' => $atts['orderby'],
'posts_per_page' => $atts['posts'],
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $atts['category'],
) ),
) );
if ( $query->have_posts() ) {
?>
<div class="#">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<p class="#">
<span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</span></p>
<?php endwhile;
wp_reset_postdata(); ?>
</div>
<?php
$myvar = ob_get_clean();
return $myvar;
}
}
add_shortcode( 'list_products', 'prod_listing_params' );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Example USAGE:
[list_products category="shoes"]
Related answers:
Nested shortcode to display dynamically woocommerce products category
Display a random product thumbnail for a product category in WooCommerce
you can also use shortcodes provided by WooCommerce itself
[product_category category="appliances"]

List categories and their posts from custom post type

I want to show the categories and their posts. (custom post type)
It should look like this:
Category 1
Post A (has cat. 1)
Post B (has cat. 1)
Category 2
Post X (has cat. 2)
Post Y (has cat. 2)
At the moment I get following output:
Category 1
Post A (in cat. 1)
Post B (in cat. 1)
Post X (in cat. 2)
Post Y (in cat. 2)
Category 2
Post A (in cat. 1)
Post B (in cat. 1)
Post X (in cat. 2)
Post Y (in cat. 2)
Heres my code: functions.php
...
register_taxonomy(
'aundo-cat',
'cdh_aundo',
array(
'hierarchical' => true,
'label' => 'Kategorien A und O',
'query_var' => true,
'rewrite' => true
)
);
...
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'cdh_aundo',
array(
'labels' => array(
'name' => __( 'A und O' ),
'singular_name' => __( 'A und O' )
),
'public' => true,
'has_archive' => false,
'menu_icon' => 'dashicons-heart',
'rewrite' => array ('slug' => 'a-und-o-der-unternehmenskommunikation'),
'supports' => array (
'title',
'editor',
'excerpt',
'thumbnail',
'category',
'custom-fields',
'revisions' )
)
);
}
Code in template file:
<?php
$cat_args = array (
'taxonomy' => 'aundo-cat',
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
$cat_query = null;
$args = array (
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'post_type' => 'cdh_aundo',
'taxonomy' => 'aundo-cat'
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
echo "<h3>". $category->name ."</h3>";
echo "<ul>";
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li>
<?php the_title(); ?>
<?php if( get_field('aundo_tipp_nummer') ): ?>
<div class="">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
</li>
<?php
}
echo "</ul>";
}
wp_reset_postdata();
}
?>
try this:
$cat_args = array (
'taxonomy' => 'aundo-cat',
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
$cat_query = null;
$args = array (
'order' => 'ASC',
'orderby' => 'title',
'posts_per_page' => -1,
'post_type' => 'cdh_aundo',
'taxonomy' => 'aundo-cat',
'term' => $category->slug
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) {
echo "<h3>". $category->name ."</h3>";
echo "<ul>";
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li>
<?php the_title(); ?>
<?php if( get_field('aundo_tipp_nummer') ): ?>
<div class="">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
</li>
<?php
}
echo "</ul>";
}
wp_reset_postdata();
}
i just added this: 'term' => $category->slug
You can try by this way
<?php
$cat = get_terms('category');
foreach ($cat as $catVal) {
echo '<h2>'.$catVal->name.'</h2>';
$postArg = array('post_type'=>'post','posts_per_page'=>-1,'order'=>'desc',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $catVal->term_id
)
));
$getPost = new wp_query($postArg);
global $post;
if($getPost->have_posts()){
echo '<ul>';
while ( $getPost->have_posts()):$getPost->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
echo '</ul>';
}
}
?>

Wordpress-Create a shortcode to list posts

I have created the custom post. I want to list down posts, but my shortcodes are not working.
Here is my code
function.php
// register a custom post type called 'Products'
function wptp_create_post_type() {
$labels = array(
'name' => __( 'Products' ),
'singular_name' => __( 'product' ),
'add_new' => __( 'New product' ),
'add_new_item' => __( 'Add New product' ),
'edit_item' => __( 'Edit product' ),
'new_item' => __( 'New product' ),
'view_item' => __( 'View product' ),
'search_items' => __( 'Search products' ),
'not_found' => __( 'No product Found' ),
'not_found_in_trash' => __( 'No product found in Trash' ),
);
$args = array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'custom-fields',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'post_tag', 'category' ),
);
register_post_type('product', $args );
}
add_action( 'init', 'wptp_create_post_type' );
product-page.php
add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
ob_start();
extract( shortcode_atts( array (
'type' => 'product',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '',
), $atts ) );
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
I have created a page from dashboard and select product-page as template and when I preview or load the page, but I couldn't see my posts.
I have try these below shortcodes on my page. Nothing works out for me
[list-posts]
[list-posts type="products" category = "movies" orderby="name" order="ASC"]
I followed this tutorial
Hello you need to add all code in your function.php i am pasting my what i have done
function.php
<?php
function wptp_create_post_type() {
$labels = array(
'name' => __( 'News' ),
'singular_name' => __( 'News' ),
'add_new' => __( 'New News' ),
'add_new_item' => __( 'Add New News' ),
'edit_item' => __( 'Edit News' ),
'new_item' => __( 'New News' ),
'view_item' => __( 'View News' ),
'search_items' => __( 'Search News' ),
'not_found' => __( 'No News Found' ),
'not_found_in_trash' => __( 'No News found in Trash' ),
);
$args = array(
'labels' => $labels,
'has_archive' => true,
'public' => true,
'hierarchical' => false,
'supports' => array(
'title',
'editor',
'excerpt',
'custom-fields',
'thumbnail',
'page-attributes'
),
'taxonomies' => array( 'post_tag', 'category' ),
);
register_post_type('News', $args );
}
add_action( 'init', 'wptp_create_post_type' );
add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
ob_start();
extract( shortcode_atts( array (
'type' => 'News',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '',
), $atts ) );
$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php the_title(); ?>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
?>
product-page.php
echo do_shortcode('[list-posts]');
I think that would help you. Let me know if you want something else
Move the code for your shortcode out of product-page.php into functions.php. Also, [list-posts type="products" category = "movies" orderby="name" order="ASC"] should have type="product" since your post type is product.
Leave ob_* functions for when there's no alternative.
WP_Query can be changed for get_posts() and while ( has_posts() ) for a regular foreach(). The only trouble is post_class(), but the core function is just one line, so easy to adapt. Also, extract() is outdated.
add_shortcode( 'list-posts', 'rmcc_post_listing_parameters_shortcode' );
function rmcc_post_listing_parameters_shortcode( $atts ) {
$args = shortcode_atts( array(
'type' => 'News',
'order' => 'date',
'orderby' => 'title',
'posts' => -1,
'category' => '',
), $atts );
$options = array(
'post_type' => $args['type'],
'order' => $args['order'],
'orderby' => $args['orderby'],
'posts_per_page' => $args['posts'],
'category_name' => $args['category'],
);
$posts = get_posts( $options );
$html = 'No posts found.';
if ( $posts ) {
$html = '<ul>';
foreach( $posts as $post ) {
$html .= sprintf(
'<li id="post-%s" class="%s">%s</li>',
$post->ID,
join( ' ', get_post_class( '', $post->ID ) ), // simplified version of get_class()
get_the_permalink( $post->ID ),
$post->post_title
);
}
$html .= '</ul>';
}
return $html;
}
Do not use extract() in a shortcode. In fact, extract(), should never be used. Here is an example of a shortcode I've done recently on another answer on WPSE. Use and modify as needed
add_shortcode( 'news_box', 'newsbox_new_loading_shortcode' );
function newsbox_new_loading_shortcode($atts){
ob_start();
$a = shortcode_atts(
[
'posts_per_page' => '-1',
'news_box_title' => 'Latest News',
'news_box_more' => '',
'post_type' => 'post',
'taxonomy' => '',
'terms' => '',
'category' => '',
],
$atts
);
if( '' == $a['taxonomy'] || '' == $a['terms'] ) {
if( '' == $a['category'] ) {
$args = [
'posts_per_page' => $a['posts_per_page'],
'post_type' => $a['post_type'],
];
}else{
$args = [
'posts_per_page' => $a['posts_per_page'],
'post_type' => $a['post_type'],
'category_name' => $a['category'],
];
}
}else{
$args = [
'posts_per_page' => $a['posts_per_page'],
'post_type' => $a['post_type'],
'tax_query' => [
[
'taxonomy' => $a['taxonomy'],
'field' => 'slug',
'terms' => $a['terms'],
]
]
];
}
//The following lines is for the excerpt more text NEW!!
if( 'post' != $a['post_type'] && '' != $a['news_box_more'] ){
$read_more_text = $a['news_box_more'];
}else {
$read_more_text = "Read More »";
}
// end of excerpt more text code
$q = new WP_Query($args);
if ( $q->have_posts() ) :
while($q->have_posts()) : $q->the_post();
$newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, '' );
// wp_trim_words function NEW!!
$content = get_the_content();
$trimmed_content = wp_trim_words( $content, 55, ' ...' . $read_more_text . '' );
// wp_trim_words function
?>
<li class="news-item">
<table cellpadding="4">
<tr>
<td>
<?php if( !empty($newsbox_post_img_src)) { ?>
<img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
<?php } ?>
</td>
<td>
<?php echo $trimmed_content; // Replaced the_excerpt() ?>
</td>
</tr>
</table>
</li>
<?php endwhile;
$list = ob_get_clean();
return $list;
endif;
wp_reset_postdata();
}
For support for PHP < 5.4, you can do the following for the shortcode function.
add_shortcode( 'news_box', 'newsbox_new_loading_shortcode' );
function newsbox_new_loading_shortcode($atts){
ob_start();
$a = shortcode_atts(
array(
'posts_per_page' => '-1',
'news_box_title' => 'Latest News',
'news_box_more' => '',
'post_type' => 'post',
'taxonomy' => '',
'terms' => '',
'category' => '',
),
$atts
);
if( '' == $a['taxonomy'] || '' == $a['terms'] ) {
if( '' == $a['category'] ) {
$args = array(
'posts_per_page' => $a['posts_per_page'],
'post_type' => $a['post_type'],
);
}else{
$args = array(
'posts_per_page' => $a['posts_per_page'],
'post_type' => $a['post_type'],
'category_name' => $a['category'],
);
}
}else{
$args = array(
'posts_per_page' => $a['posts_per_page'],
'post_type' => $a['post_type'],
'tax_query' => array(
array(
'taxonomy' => $a['taxonomy'],
'field' => 'slug',
'terms' => $a['terms'],
),
),
);
}
//The following lines is for the excerpt more text NEW!!
if( 'post' != $a['post_type'] && '' != $a['news_box_more'] ){
$read_more_text = $a['news_box_more'];
}else {
$read_more_text = "Read More »";
}
// end of excerpt more text code
$q = new WP_Query($args);
if ( $q->have_posts() ) :
while($q->have_posts()) : $q->the_post();
$newsbox_post_img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), '', false, '' );
// wp_trim_words function NEW!!
$content = get_the_content();
$trimmed_content = wp_trim_words( $content, 55, ' ...' . $read_more_text . '' );
// wp_trim_words function
?>
<li class="news-item">
<table cellpadding="4">
<tr>
<td>
<?php if( !empty($newsbox_post_img_src)) { ?>
<img src="<?php echo $newsbox_post_img_src[0]; ?>" width="100" class="img-circle" />
<?php } ?>
</td>
<td>
<?php echo $trimmed_content; // Replaced the_excerpt() ?>
</td>
</tr>
</table>
</li>
<?php endwhile;
$list = ob_get_clean();
return $list;
endif;
wp_reset_postdata();
}

Wordpress portfolio page display only images from certain filter

I have been following many tutorials on a portfolio which uses quicksand.js to do a filter animation. In the function i can create filters (categories) with slugs. I can't, after endless hour of reading, figure out how to only display the thumbnails from a certain filter.
I've included the parts of the portfolio from my functions.php
I know this is a lot of code, but this is a last resort. I can't get it working so i am hoping some php / wordpress guru might point out something i've missed.
Through reading up on it, i did managed to pull out JUST the featured images posted in 'featured' by using:
<?php
$args = array(
'post_type' => 'portfolio',
'tax_query' => array(
array(
'taxonomy' => 'filter',
'field' => 'slug',
'terms' => 'featured'
)
)
);
$query = new WP_Query( $args );
?>
However, this just pulls out the image and it's title, without the formatted HTML i need. But applying this to what i already use is where i get completely lost.
Thanks in advance.
My Index.php
h2>Featured Work</h2>
<ul id="image_gallery" class="group index_gallery filterable-grid">
<?php
// Query Out Database
$wpbp = new WP_Query(array( 'post_type' => 'portfolio', 'posts_per_page' =>'9' ) );
?>
<?php
$terms = get_terms("filter");
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>
<?php
// Begin The Loop
if ($wpbp->have_posts()) : while ($wpbp->have_posts()) : $wpbp->the_post();
?>
<?php
// Get The Taxonomy 'Filter' Categories
$terms = get_the_terms( get_the_ID(), 'filter' );
?>
<?php
$large_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_image[0];
?>
<?php
//Apply a data-id for unique indentity,
//and loop through the taxonomy and assign the terms to the portfolio item to a data-type,
// which will be referenced when writing our Quicksand Script
?>
<li class="gallery_image" data-id="id-<?php echo $count; ?>" data-type="<?php foreach ($terms as $term) { echo strtolower(preg_replace('/\s+/', '-', $term->name)). ' '; } ?>">
<?php
// Check if wordpress supports featured images, and if so output the thumbnail
if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) :
?>
<?php // Output the featured image ?>
<a rel="prettyPhoto[gallery]" class="zoom" href="<?php echo $large_image ?>">
<img class="mag" src="<?php bloginfo('template_url'); ?>/imgs/mag.png"/><div class="thumb_bg"></div><?php the_post_thumbnail('portfolio'); ?>
</a>
<?php endif; ?>
<!--<?php // Output the title of each portfolio item ?>
<p><?php echo get_the_title(); ?></p>-->
</li>
<?php $count++; // Increase the count by 1 ?>
<?php endwhile; endif; // END the Wordpress Loop ?>
<?php wp_reset_query(); // Reset the Query Loop?>
</ul>
<div class="gallery_control">
<span class="icon-search"></span>View more
</div>
<?php
$taxonomy = 'filter';
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<p>' . '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> has ' . $term->count . ' post(s). </p> ';
}
}
?>
Portfolio workings:
// function: post_type BEGIN
function post_type()
{
$labels = array(
'name' => __( 'Portfolio'),
'singular_name' => __('Portfolio'),
'rewrite' => array(
'slug' => __( 'portfolio' )
),
'add_new' => _x('Add Item', 'portfolio'),
'edit_item' => __('Edit Portfolio Item'),
'new_item' => __('New Portfolio Item'),
'view_item' => __('View Portfolio'),
'search_items' => __('Search Portfolio'),
'not_found' => __('No Portfolio Items Found'),
'not_found_in_trash' => __('No Portfolio Items Found In Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array(
'title',
'editor',
'thumbnail'
)
);
register_post_type(__( 'portfolio' ), $args);
} // function: post_type END
// function: portfolio_messages BEGIN
function portfolio_messages($messages)
{
$messages[__( 'portfolio' )] =
array(
0 => '',
1 => sprintf(('Portfolio Updated. View portfolio'), esc_url(get_permalink($post_ID))),
2 => __('Custom Field Updated.'),
3 => __('Custom Field Deleted.'),
4 => __('Portfolio Updated.'),
5 => isset($_GET['revision']) ? sprintf( __('Portfolio Restored To Revision From %s'), wp_post_revision_title((int)$_GET['revision'], false)) : false,
6 => sprintf(__('Portfolio Published. View Portfolio'), esc_url(get_permalink($post_ID))),
7 => __('Portfolio Saved.'),
8 => sprintf(__('Portfolio Submitted. <a target="_blank" href="%s">Preview Portfolio</a>'), esc_url( add_query_arg('preview', 'true', get_permalink($post_ID)))),
9 => sprintf(__('Portfolio Scheduled For: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Portfolio</a>'), date_i18n( __( 'M j, Y # G:i' ), strtotime($post->post_date)), esc_url(get_permalink($post_ID))),
10 => sprintf(__('Portfolio Draft Updated. <a target="_blank" href="%s">Preview Portfolio</a>'), esc_url( add_query_arg('preview', 'true', get_permalink($post_ID)))),
);
return $messages;
} // function: portfolio_messages END
// function: portfolio_filter BEGIN
function portfolio_filter()
{
register_taxonomy(
__( "filter" ),
array(__( "portfolio" )),
array(
"hierarchical" => true,
"label" => __( "Filter" ),
"singular_label" => __( "Filter" ),
"rewrite" => array(
'slug' => 'filter',
'hierarchical' => true
)
)
);
} // function: portfolio_filter END
add_action( 'init', 'post_type' );
add_action( 'init', 'portfolio_filter', 0 );
add_filter( 'post_updated_messages', 'portfolio_messages' );
I am not sure what you are doing here but change this
<?php
$large_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_image[0];
?>
to this
<?php
$large_images[] = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
$large_image = $large_images[0];
?>
I found a solution:
<?php
$args = array(
'post_type' => 'project',
'tax_query' => array(
array(
'taxonomy' => 'filter',
'field' => 'slug',
'terms' => 'featured'
)
)
);
$query = new WP_Query( $args );
?>
Only displays my filtered item with a name of featured.
This is the code I use to get full image and thumb of attached images:
$args = array(
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => null,
'post_type' => 'attachment',
);
$upload_dir = wp_upload_dir();
$upload_url = $upload_dir['baseurl'];
// Get img data
$attachments = get_children( $args );
$images = array();
// Loop through attached images and get thumb + large img url
foreach($attachments as $attachment) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'thumbnail' );
$img['thumb'] = $image_attributes[0];
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'large' );
$img['large'] = $image_attributes[0];
array_push($images,$img);
}
// Get the image that you have set as the featured image in the post
$featured_img = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
$featured_img_thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );

Categories