On a custom post type archive I'm trying to add the custom taxonomy terms slug as a css class to the tag. I've managed to get it to output the page->ID but struggling to get the $term->slug to work. Feel like I'm missing something really simple. Here's the full code, thanks for any help:
<?php
$parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
foreach ( $parent_pages as $parent_page ) {
echo '<h1 class="page-heading" id="';
echo $parent_page->post_name;
echo '">';
echo $parent_page->post_title;
echo '</h1>';
echo '<div class="wrapper grid4">';
$all_pages = get_pages(array( 'post_type'=> 'archive', 'orderby' => 'menu_order' , 'order' => 'ASC', 'sort_column' => 'menu_order' ) );
$child_pages = get_page_children($parent_page->ID, $all_pages );
foreach ( $child_pages as $child_page ) {
echo '<article class="post col ' . $child_page->ID, $term->slug .'">';
echo '<a class="fancybox" data-fancybox-type="iframe" href="http://www.iofpi.co.uk/civicworks.net/wp-content/plugins/pdfjs-viewer-shortcode/web/viewer.php?file=http://www.iofpi.co.uk/civicworks.net/wp-content/uploads/2014/05/Citizen_Manchester.pdf" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail( $child_page->ID, 'medium');
echo '</a>';
echo '<h1>';
echo $child_page->post_title;
echo '</h1>';
echo '</article>';
}
echo '</div>';
}
?>
Use this function get_the_terms( $id, $taxonomy ) just pass it the post id in your case '$parent_page->ID' and the Name of taxonomy to retrieve terms from. For example: 'category', 'post_tag', 'taxonomy slug'
It will return an object and you can then access the slug with, say $result->slug.
Related
I would like to display categories (from taxonomy-product_category) in archive-product.php.
Get categories:
Fruit
Herbs
Salad
Vegetables
See screenshot what I mean:
When I added coding in archive-product.php:
<?php
$args = array(
'post_type' => 'product',
'taxonomy' => 'product_category',
'hierarchical' => 1,
'nopaging' => false,
'posts_per_page' => '2',
'posts_per_archive_page' => '10',
'ignore_sticky_posts' => true,
'order' => 'rand',
);
echo '<div class="container">';
echo '<div class="row">';
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="col-lg-4">';
echo '<a href="'.get_the_permalink().'">';
if ( has_post_thumbnail() ) {
the_post_thumbnail(array(486,226));
}
the_title();
the_content();
echo '</a>';
echo '</div>';
}
} else {
// no posts found
echo wpautop( 'Sorry, no posts were found' );
}
echo '</div>';
echo '</div>';
// Previous/next post navigation.
previous_post_link( '%link', 'Prev post in category', true
);
next_post_link( '%link', 'Next post in category', true );
// Restore original Post Data
wp_reset_postdata();
?>
But not display categories (Fruit, Herbs, Salad, Vegetables)
Would anyone know about this?
Thanks,
Shaun.
Please try below code:
$args = array(
'taxonomy'=> 'product_category',
'order' => 'DESC',
);
$categories = get_categories($args);
print_r($categories);
You can get the list of all product categories using Below code
$orderby = 'name';
$order = 'asc';
$hide_empty = false ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '
<ul>';
foreach ($product_categories as $key => $category) {
echo '
<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>
';
}
So I'm running this query:
// find books that have this author
$author_name = get_the_title();
// args
$booksby_args = array(
'post_type' => 'product',
'post_status' => 'any',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'pa_author-woo',
'terms' => $author_name,
'field' => 'name'
),
),
);
// The Query
$thebooks_query = new WP_Query( $booksby_args );
// The Loop
if ( $thebooks_query->have_posts() ) {
echo '<h3>Books by '.$author_name.'</h3>';
echo '<ul class="author-entry__book-list">';
while ( $thebooks_query->have_posts() ) {
$thebooks_query->the_post();
$productObj = get_page_by_title( get_the_title(), OBJECT, 'product' );
$productId = $productObj->ID;
$wc_product = wc_get_product( $productId );
echo '<li class="clearfix">';
echo '<a href="'.get_permalink( $productId ).'">';
echo the_post_thumbnail('shop_thumbnail');
echo '<div class="author-entry__book-info">';
echo '<p class="title">'.get_the_title().'</p>';
echo '<p class="price">'.$wc_product->get_price_html().'</p>';
echo '</div>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
But whenever the title of a product contains a '-', the output just seems to fail after it's echo'ed the title. There's not even an empty <p class="price"> tag. Other elements lower down my template don't get outputted either.
Also, if I try define define a variable for the price, e.g. $wc_price = $wc_product->get_price_html() before all the echo statements, none of the echos render at all!
If I comment out the price echo or if the product title doesn't contain a dash, It all works just fine.
This is a snippet of what it outputs when it fails:
<div class="author-entry__book-info">
<p class="title">Rogue – The Inside Story Of SARS’s Elite Crime-busting Unit</p>
</div>
Help me understand please!
I've created a shortcode for custom post with advanced custom fields. All works correctly except the pagination. I've tried all of the options I can see on other posts, but none work for me. Pagination is working on custom post pages.
function link_carstwo( $atts ) {
extract(shortcode_atts(array(
'cartype' => 'porsche',
'section' => 'make'
), $atts));
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
$list = ' ';
echo '<div id="car-container">
<ul id="carlist">';
//Setup the query to retrieve the posts that exist under each term
global $post;
$posts = new WP_Query (array(
'post_type' => 'cars',
'orderby' => 'menu_order',
'order' => 'ASC',
$section => $cartype,
'post_status' => 'publish',
'posts_per_page' => 9,
'paged' => $paged,
));
// Here's the second, nested foreach loop that cycles through the posts associated with this category
while ( $posts->have_posts() ) { $posts->the_post();
////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
$price = get_field('price', $post->ID);
$car_image = get_field('car_image', $post->ID);
$image_position = get_field('image_position', $post->ID);
$make = get_field('make', $post->ID);
$year = get_field('year', $post->ID);
$date_purchased = get_field('date_purchased', $post->ID);
$finance_type = get_field('finance_type', $post->ID);
$job_title = get_field('job_title', $post->ID);
$model = get_field('model', $post->ID);
$list .= '<li class="carbox">
<p>TEST</p>
<div class="image2" style="background-image:url(' . $car_image .');background-position: ' . $image_position . ' center;"></div>
<p class="car"> '.$make.' ' . $model . ' ' . $year . ' </br> £ ' . $price . ' ' . $date_purchased . '</p>
<p class="finance">' . $finance_type . '</p>
<p class="fademeSmall"> ' . $job_title . '</p>
<p class="linked"></p>
</li>';
}
'</ul></div>';
return
'<div class="navigation centerWidth">'
. $list
.the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
) )
. '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">←</span> Older posts' ) ) . '</div>'
. '<div class="nav-next">' . get_previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>' ) ) . '</div>'
. 'TEST</div>' .
wp_reset_query();
}
add_shortcode( 'car-gridtwo', 'link_carstwo' );
Try this for your pagination
<?php
global $wp_query;
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
?>
The way you are using wp_query and the global variables is extremely convoluted and wrong.
while ( $post->have_posts() ) { $posts->the_post();
On the while condition you should have $posts->have_posts() rather than $post->have_posts()
You also don't need the setup_postdata($post);
I'm not sure that this will solve the issue, everything else seems "fine" to me.
EDIT
Having a closer look at the code I just noticed that your return statement is within the loop, that's wrong and might be the reason why you are having the pagination issue. Just close the While block before the return statement (and obviously remove the last closing bracket, before wp_reset_query). Don't forget to do the change I suggested before as well.
I'am looking for code in Woocommerce that allow me to display x items wth recent comments (reviews) added
simlar to [recent_products per_page="16" columns="4" orderby="rand"] shortcode but allowing to get post only with comments
I don't believe there is a shortcode for this. You'd have to write some PHP. The way that you can do this is:
$number = 5; // Change as desired
$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
if ( $comments ) {
echo '<ul class="product_list_widget">';
foreach ( (array) $comments as $comment ) {
$_product = wc_get_product( $comment->comment_post_ID );
$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
$rating_html = $_product->get_rating_html( $rating );
echo '<li><a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
echo $_product->get_image();
echo $_product->get_title() . '</a>';
echo $rating_html;
printf( '<span class="reviewer">' . _x( 'by %1$s', 'by comment author', 'woocommerce' ) . '</span>', get_comment_author() );
echo '</li>';
}
echo '</ul>';
}
(Note: this code is lifted with modification from the WooCommerce plugin, "recent reviews" widget: woocommerce/includes/widgets/class-wc-widget-recent-reviews.php)
This is untested, but something like this should work:
$args = array(
'post_type' => 'product',
'orderby' => 'comment_count',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
if ( $post->comment_count < 1 ) continue;
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
I am trying to display the category titles and their posts from a custom post type. I can get the category to display but it currently lists all the posts within the custom post area instead of separating them in to the
<?php
$taxonomy = 'staff';
$cat_args = array(
'taxonomy' => $taxonomy,
'tax_input' =>$tax_input,
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 0
);
$tax_terms = get_terms($taxonomy);
foreach ($tax_terms as $tax_term) {
echo '<div class="categorybox">';
echo '<h4> <a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></h4>';
$args = array(
'post_type' => 'staff',
"singular_label" => "Department",
'numberposts' => 5,
'taxonomy' => $taxonomy->$tax_term,
);
$posts = get_posts($args);
?>
<ul><?php
foreach($posts as $post) {
?>
<li><?php the_title(); ?></li>
<?php
} ?>
</ul><?php
echo '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>View all articles in ' . $tax_term->name.' »</a>';
echo '</div>';
}
?>
There no option taxonomy in get_posts(). Try this one
'tax_query' => array(
array(
'taxonomy' => 'staff',
'field' => 'slug',
'terms' => $tax_term->slug
)
)
<?php
$query = new WP_Query( array( 'post_type' => 'testimonials','posts_per_page' => 10 ) ); //testimonials custom post type
while($query->have_posts()) : $query->the_post();
?>
<ul>
<li>
Category:<?php the_category(', '); ?>
<h3><?php the_title(); ?></h3>
</li>
</ul>
<?php endwhile; ?>