Wordpress-Create a shortcode to list posts - php

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();
}

Related

Woocommerce product shortcode with offset attribute

I've worked with a code extracted from an old question (Woocommerce Products Offset Shortcode), so I created a new shortcode to display products with offset but I think the "orderby" and "column" parameters are not working properly.
//Shortcode for Offset Products
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
if( ! empty( $atts['category'] ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => trim( $atts['category'] ),
'operator' => $atts['operator']
),
);
}
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );
I've tried a lot of things but nothing worked fine for me :/
I would appreciate any help or code improvements!

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

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".

Woocommerce Products Offset Shortcode

I'm trying to create a custom Woocommerce products shortcode that allows me to offset a certain number of products when adding the shortcode to a page. I've been able to get the offset part working by looking at WooCommerce - Recent Products Offset and https://ericwijaya.com/load-woocommerce-loop-custom-arguments/
The problem I am having is that the Woocommerce products "category" attribute doesn't work with my new shortcode. My shorcode looks like this: [products_offset limit="8" category="fiction" offset="10" orderby="menu_order"]
Instead of displaying products using the specified category slug (in this case "fiction"), it displays all products across all categories.
This is the code I've added to my function.php:
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );`
Any help would be greatly appreciated!
Figured it out. I wasn't passing the category slug into the WP_Query. Added:
if( ! empty( $atts['category'] ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => trim( $atts['category'] ),
'operator' => $atts['operator']
),
);
}
So code all together looks like this:
//Shortcode for Offset Products
function products_offset_func( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'date',
'order' => 'desc',
'offset' => 0,
'category' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), (array) $atts );
ob_start();
$query_args = array(
'columns' => $atts['columns'],
'posts_per_page' => $atts['per_page'],
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'offset' => $atts['offset'],
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => WC()->query->get_meta_query(),
);
if( ! empty( $atts['category'] ) ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => trim( $atts['category'] ),
'operator' => $atts['operator']
),
);
}
?>
<ul class="products">
<?php
$loop = new WP_Query( $query_args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
<?php
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'products_offset', 'products_offset_func' );

dynamic mixitup plugin in wordpress

I am trying to create my own theme in WordPress. I converted the PSD file to HTML and now I want to make it dynamic with WordPress, I used the mix it up plugin as you know it's not a WordPress plugin so I tried to make it dynamic myself, I used this tutorial which provides codes and video for this issue.
as you can see in the pic, it must show my category but show 'array' and I cant find why?
here is my code
first I create post type named book with taxonomy named books_category
function p2p2_register_book(){
$labels = array(
'name' => _x( 'کتابخانه', 'books'),
'singular_name' => _x( 'کتاب', 'book' ),
'add_new' => _x( 'افزودن کتاب', '' ),
'add_new_item' => __( 'افزودن کتاب جدید' ),
'edit_item' => __( 'ویرایش کتاب' ),
'new_item' => __( 'کتاب جدید' ),
'all_items' => __( 'همه کتاب ها' ),
'search_items' => __( 'جست و جو کتاب' ),
'not_found' => _( 'کتاب یافت نشد' ),
'not_found_in_trash' => __( 'کتاب در زباله دان یافت نشد' ),
'parent_item_colon' => '',
'menu_name' => 'کتابخانه'
);
$args=array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'menu_position' => 2,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'page',
'has_archive' => true,
// 'hierarchical' => false,
'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ,'custom-fields'),
// 'taxonomies' => array( 'Books_category' ),
);
register_post_type('Book',$args);
}
add_action('init', 'p2p2_register_book');
// ============================== Adding post toxonomy to library post ================
function wp_library_post_taxonomy() {
register_taxonomy(
'books_category', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'book', //post type name
array(
'hierarchical' => true,
'label' => 'book Category', //Display name
'query_var' => true,
'show_admin_column' => true,
'rewrite' => array(
'slug' => 'books_category', // This controls the base slug that will display before each term
'with_front' => true // Don't display the category base before
)
)
);
}
add_action( 'init', 'wp_library_post_taxonomy');
include_once( 'mixitup-plug.php' );
add_theme_support('post_thumbnails');
add_image_size('Books_small_image',150,200,true);
then as the tutorial said I added this code for make it dynamic
<?php
function books_shortcode($atts){
extract( shortcode_atts( array(
'category' => ''
), $atts, '' ));
$q= new WP_Query(
array('posts_per_page'=> 30, 'post_type' => 'Book')
);
//library taxonomy query
global $paged;
global $post;
$args = array(
'post_type' => 'Book',
'paged' => $paged,
' posts_per_page' => -1,
);
$Books = new WP_Query($args);
if(is_array($Books->posts) && !empty($Books->posts)) {
foreach($Books->posts as $Books_post) {
$Books_taxs = wp_get_post_terms($Books_post->ID, 'Books_category', array("fields" => "all"));
if(is_array($Books_taxs) && !empty($Books_taxs)) {
foreach($Books_taxs as $Books_tax) {
$Books_taxs[$Books_tax->slug] = $Books_tax->name;
}
}
}
}
?>
<!--Category Filter-->
<div class="filter-book">
<ul>
<li class="filter active" data-filter="all">همه</li>
<?php foreach($Books_taxs as $Books_tax_slug => $Books_tax_name): ?>
<li class="filter" data-filter=".<?php echo $Books_tax_slug; ?>"><?php echo $Books_tax_name; ?></li>
<?php endforeach; ?>
</ul></div>
<!--End-->
<?php
$list = '<div class="library-item">';
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID();
$small_image_url=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'Books_small_image');
//Get Texanmy class
$item_classes = '';
$item_cats = get_the_terms($post->ID, 'Books_category');
if($item_cats):
foreach($item_cats as $item_cat) {
$item_classes .= $item_cat->slug . ' ';
}
endif;
$single_link =
$list .= '
<div class="mix single-library cycology '.$item_classes.'">
<div>
<img src="'.$small_image_url[0].'">
<p class="text-center">کتاب جیبی موفقیت یک</p>
</div>
</div>
<div class="mix '.$item_classes.'" >'.get_the_content().'</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('Books', 'Books_shortcode');
?>
it must be some problem with this part of my code because this part show category
<div class="filter-book">
<ul>
<li class="filter active" data-filter="all">همه</li>
<?php foreach($Books_taxs as $Books_tax_slug => $Books_tax_name): ?>
<li class="filter" data-filter=".<?php echo $Books_tax_slug; ?>"><?php echo $Books_tax_name; ?></li>
<?php endforeach; ?>
</ul></div>
Please check you have used echo array function
<li class="filter" data-filter=".<?php echo $Books_tax_slug; ?>"><?php echo $Books_tax_name; ?></li>
You can check it
<?php
# Dynamic Portfolio With Shortcode
function portfolio_shortcode($atts){
extract( shortcode_atts( array(
'category' => ''
), $atts, '' ) );
$q = new WP_Query(
array('posts_per_page' => 50, 'post_type' => 'portfolios')
);
//Portfolio taxanomy query
global $paged;
global $post;
$args = array(
'post_type' => 'portfolios',
'paged' => $paged,
'posts_per_page' => -1,
);
$portfolio = new WP_Query($args);
if(is_array($portfolio->posts) && !empty($portfolio->posts)) {
foreach($portfolio->posts as $gallery_post) {
$post_taxs = wp_get_post_terms($gallery_post->ID, 'portfolio_category', array("fields" => "all"));
if(is_array($post_taxs) && !empty($post_taxs)) {
foreach($post_taxs as $post_tax) {
$portfolio_taxs[$post_tax->slug] = $post_tax->name;
}
}
}
}
?>
<!--Category Filter-->
<div class="portfolio_button_area fix">
<button class="filter portfolio_button active" data-filter="all">Show All</button>
<?php foreach($portfolio_taxs as $portfolio_tax_slug => $portfolio_tax_name): ?>
<button class="filter portfolio_button" data-filter=".<?php echo $portfolio_tax_slug; ?>"><?php echo $portfolio_tax_name; ?></button>
<?php endforeach; ?>
</div>
<!--End-->
<?php
$list = '<div id="Container">';
while($q->have_posts()) : $q->the_post();
$idd = get_the_ID();
//Get Texanmy class
$item_classes = '';
$item_cats = get_the_terms($post->ID, 'portfolio_category');
if($item_cats):
foreach($item_cats as $item_cat) {
$item_classes .= $item_cat->slug . ' ';
}
endif;
$single_link =
$list .= '
<div class="mix '.$item_classes.'" >'.get_the_content().'</div>
';
endwhile;
$list.= '</div>';
wp_reset_query();
return $list;
}
add_shortcode('portfolio', 'portfolio_shortcode');
?>
code reference and more http://www.wp-tutorials.com/how-to-dynamic-mixitup-or-isotope-in-wordpress-step-by-step
I find the answer it's word problem. I use Book but call book so it didn't work.

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