WooCommerce - Dash in product title breaks loop - wordpress

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!

Related

How to get post using taxonomy terms in wordpress

I have a post type called "rationale" and my taxonomy name is "company_list". In taxonomy there are list of company. Each company have many rationale.
I want to get latest rationale for each company. How can i do this ?
I try below code but it show all company list but data is duplicate
<?php
//$taxonomy = 'our_work_thematic';
$myquery = array (
'post_type' => 'rationale',
'paged'=>$paged,
'posts_per_page' => -1,
);
$loop = new WP_Query($myquery);
if( $loop->have_posts() ):
while( $loop->have_posts() ):
$loop->the_post(); global $post; ?>
<?php $terms = get_the_terms( $post->ID, 'company_list' );
foreach($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}?>
<?php endwhile; ?>
<?php endif; ?>
You need to get latest term and use tax_query
$args_query = array(
'post_type' => array('rationale'),
'paged' => $paged,
'posts_per_page' => -1,
);
$terms = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
));
if (!empty($terms) && is_array($terms)) {
$args_query['tax_query'] => array(
array(
'taxonomy' => 'company_list',
'field' => 'term_id',
'terms' => array($terms['0']->term_id), // single or array with id's
),
),
}
$query = new WP_Query($args_query);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$terms = get_the_terms($post->ID, 'company_list');
foreach ($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}
}
} else {
// no post found
}
wp_reset_postdata();
try this
$terms = get_terms( array(
'taxonomy' => 'your taxonomy name',
'hide_empty' => false,
'orderby' => 'term_id',
'order' => 'asc',
) );
foreach ($terms as $terms_row) {
$terms_row->slug;
echo "<pre>";
print_r($terms_row);
echo "</pre>";
}
Thanks

How to get categories product in archive.php in WordPress?

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>
';
}

only first caption showing for attachment in Wordpress

ok I'm using the below code to generate caption for my attachments. The problem is if I have multiple images in a single page, the caption for first image shows for all the images. like if I have "Text One" as a caption for my image one, all the image showing this "Text One" caption. How can I solve this problem?
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
}
}
?>
<p class="project-caption"><?php echo $caption ?></p>
currently there is a slider, which shows the images. & the slider code is:
<?php
if($repeater):
foreach($repeater as $r):
?>
<li class="<?php echo $r["fit_to_screen"] ? "img_fit" : ""; ?>">
<?php
if($r["acf_fc_layout"] == "image"):
$html = "<img data-fit='".$r["fit_to_screen"]."' src='".$r["image"]["url"]."' alt='".$r["image"]["alt"]."'/>";
echo apply_filters( 'post_thumbnail_html', $html, $post->ID , $r["image"]["id"], "large" , array("alt"=>$r["image"]["alt"]) );
else:
echo getVideoEmbed($r["video_url"]);
endif;
?>
</li>
<?php
endforeach;
endif;
?>
You override the $caption in the loop so at the end you get the last caption.
You can print the $caption inside the loop and then you get all the captions.
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">'.$caption.'</p>';
}
You get the same caption because you are displaying the caption after the foreach, and you should do it in the foreach:
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">' . $caption . '</p>';
}
}
?>

Add WordPress custom taxonomy terms as css class

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.

How do I show the number of images attached to a post on the image attachment page?

I use the image attachment page to show images attached to a post one by one, in a slideshow sort of affect. I'd like to be able to display the total number of images attached to the parent post and the number of the particular image that's being shown on any given attachment page so you see the picture and the words "Image 3 of 15" for example.
Update... I was able to get the total number to show using this code:
<?php
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
echo $count;
?>
I still can't figure out how to show the number of the current image.
Anyone have any suggestions?
Update 2...
Greenie's answer got me almost there but it's outputting all the numbers at once:
"Image 1 of 8Image 2 of 8Image 3 of
8Image 4 of 8Image 5 of 8Image 6 of
8Image 7 of 8Image 8 of 8"
Here is the code I used:
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$currentImage = 1;
foreach ($attachments as $attachment) {
// output your image here
echo "Image ". $currentImage . " of ". $count;
$currentImage++;
}
What's going wrong?
Update 3 - THE ANSWER!
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$specific = array();
$i = 1;
foreach ( $attachments as $attachment ) {
$specific[$attachment->ID] = $i;
++$i;
}
echo "Image {$specific[$post->ID]} of {$count}";
This works:
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$specific = array();
$i = 1;
foreach ( $attachments as $attachment ) {
$specific[$attachment->ID] = $i;
++$i;
}
echo "Image {$specific[$post->ID]} of {$count}";
Add something like this to the above code:
$currentImage = 1;
foreach ($attachments as $attachment) {
// output your image here
echo "Image ". $currentImage . " of ". $count;
$currentImage++;
}
If you are looking for a plugin to manage image gallery, you can use attachments plugin,
http://wordpress.org/plugins/attachments/
It keeps the gallery separate and does not put the image gallery shortcodes in post content, thus providing you with full hold over image display in your post/page/custom post. You can also change the order of your images by just drag-n-drop
here is a sample code of how to retrieve your gallery images,
<?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
<?php if( $attachments->exist() ) : ?>
<h3>Attachments</h3>
<p>Total Attachments: <?php echo $attachments->total(); ?></p>
<ul>
<?php while( $attachments->get() ) : ?>
<li>
ID: <?php echo $attachments->id(); ?><br />
Type: <?php echo $attachments->type(); ?><br />
Subtype: <?php echo $attachments->subtype(); ?><br />
URL: <?php echo $attachments->url(); ?><br />
Image: <?php echo $attachments->image( 'thumbnail' ); ?><br />
Source: <?php echo $attachments->src( 'full' ); ?><br />
Size: <?php echo $attachments->filesize(); ?><br />
Title Field: <?php echo $attachments->field( 'title' ); ?><br />
Caption Field: <?php echo $attachments->field( 'caption' ); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

Resources