I have a coupon site, which basically has many vendors in it. I want to create a related stores widget kind of a thing which would display 5-6 stores on the side bar. and i want to them to relate to the current vendor page. till now i have managed to display 5-6 stores links randomly, but i am not able to display the related stores.
I am using clipper theme in wordpress, and i have kept all my stores in Coupons-Stores and not Post-categories.
<?php
$stores = get_terms('stores'); // Note: this returns NULL non-empty cats. It is not normal, could be because of the widget.
// Get all non-empty stores
$nonempty_stores = array();
foreach( $stores as $store )
if ( !empty( $store ) )
$nonempty_stores[] = $store;
$stores_count = count($nonempty_stores);
global $random_seed;
// Set initial seed value
$random_seed = $last_update;
if (DISPLAY_SAME_STORES_LIST_FOR_ALL_STORE_PAGES == false)
$random_seed = $random_seed + $store_catid;
function mtech_rand($min, $max)
{
global $random_seed;
$random_seed = ((1103515245*$random_seed)+12345)%(1<<31);
return (($random_seed%($max-$min+1))+$min);
}
echo "<div class=\"hdsboxbg\"> ";
$cntr = 0;
$displayed_cntr = 0;
while (($cntr < $stores_count) && ($displayed_cntr < NUM_OF_RANDOM_STORES_TO_DISPLAY))
{
$rand_store_idx = mtech_rand($cntr, $stores_count-1);
$rand_store = $nonempty_stores[$rand_store_idx];
if (($store_catid == 0) || ($rand_store->cat_ID != $store_catid)) // Random Stores Widget should not display a link to the current store page
{
$a_text = $rand_store->name . ' Coupons';
//$a_text = $rand_store->name;
echo "<div class=\"hdsmod\"> $a_text </div>";
$displayed_cntr++;
}
$nonempty_stores[$rand_store_idx] = $nonempty_stores[$cntr];
$nonempty_stores[$cntr] = $rand_store;
$cntr++;
}
echo "</div>";
?>
Any other methods are welcome. i have tried to display all the stores like this,
$terms = get_terms('stores');
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
it displays but i am not able to display the related stores, i am struggling to figure out on which grounds should i proceed. Any help would be appreciated.
got it.
<?php
$taxonomy = 'stores';
$args1=array(
'include'=> array(12,30)
);
$terms = get_terms('stores',$args1 );
echo '<ul>';
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'stores' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Related
I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
I have a custom post of portfolio items. Within the custom post I have categories set up, as well as a custom taxonomy of clients. I am listing the portfolio items by client, but I need also need to filter these by which category they appear in. I have this so far:
$terms = get_terms('client');
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
echo "<ul>";
foreach ( $terms as $term ) {
$term_id = $term->term_id;
echo '<li class="item"> ';
$taxonomy = 'category';
$client_cats = get_terms($taxonomy);
foreach ($client_cats as $client_cat) {
echo '<li>' . $client_cat->name.'</a></li>';
}
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//It will give category/term image url
}
echo $meta_image; // category/term image url
echo $term->name . "</li>";
}
echo "</ul>";
} else {
echo "No posts";
}
But this gives me a list of the clients with a list of all the categories after each one. How can I filter the categories so that only the ones the client is linked to is displayed?
Thanks for any help! :)
I am currently trying to create a plugin using Pinterest for Wordpress. I have ran into a slight problem, I've been trying to return an image with a link but only been able to only return a link.
<?php
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request['body'], true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}
?>
Anyone experienced with Wordpress Plugins and Pinterest can help me with this problem ?
Try to change your code as below. You will get text in $pin['note'] and image url in $img['url'].
function displayPinterest()
{
$request = wp_remote_get( 'https://api.pinterest.com/v1/boards/mercurizen/shoeswatches/pins/?access_token=AXgIMQmjCFDXyJsHni0wYUdZxshcFETISS1nfSRDAz1G7WBDJgAAAAA&fields=image,note,link&limit=5' );
$pins = json_decode( $request, true );
if( !empty( $pins['data'] ) ) {
echo '<ul>';
foreach( $pins['data'] as $pin ) {
$img = $pin['image']['original'];
echo '<li>' . $pin['note']. '</li>';
}
echo '</ul>';
}
}
I have e commerce website in wordpress. There are lot of product in it & many product comes under multiple categories like 600 mah Power bank is comes under automobile, IT, media etc. My problem is when i go to the detail of a product there it by default pick up only one category no matter if go through IT category at the end it shows me automobile like this Home / Shop / industry / Automobile / 600 mah Power Bank. But i went to this product via IT so it should show me like this Home / Shop / industry / IT / 600 mah Power Bank.
How can iget path where i come from previous page?
if you are using Woocommerce you can use the following directly, if not it will need adapting but you get the idea:
if (get_post_type() == 'product' && is_single() && ! is_attachment()) {
echo $prepend;
if ($terms = get_the_terms($post->ID, 'product_cat')) {
$referer = wp_get_referer();
foreach ($terms as $term) {
$referer_slug = (strpos($referer, $term->slug));
if ($referer_slug == true) {
$category_name = $term->name;
$ancestors = get_ancestors($term->term_id, 'product_cat');
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor) {
$ancestor = get_term($ancestor, 'product_cat');
if (! is_wp_error($ancestor) && $ancestor) {
echo $before . '' . $ancestor->name . '' . $after . $delimiter;
}
}
echo $before . '' . $category_name . '' . $after . $delimiter;
}
}
}
echo $before . get_the_title() . $after;
}
The main bulk of the work here is done by wp_get_referer which gets the referring URL of the product your visitor has navigated to. The rest of the code checks if a valid category is contained within the URL and uses it in the breadcrumb.
See Jonathon Js post here for more information http://www.cryoutcreations.eu/forums/t/wrong-breadcrumbs-displayed
Updated answer for anyone still encountering this issue on a newer version of WooCommerce, this solution worked for me on WooCommerce 3.5.4.
This code should be placed inside the following file path (i.e. a child theme)
/wp-content/themes/your-child-theme/woocommerce/global/breadcrumb.php
It will override the default WooCommerce breadcrumb code.
http://pastebin.com/raw/bemM8ZNF
/**
* Shop breadcrumb
*
* This template can be overridden by copying it to yourtheme/woocommerce/global/breadcrumb.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.3.0
* #see woocommerce_breadcrumb()
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( $breadcrumb ) {
echo $wrap_before;
if ( is_single() && get_post_type() == 'product' ) {
echo $prepend;
if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
$referer = wp_get_referer();
$printed = array();
foreach( $terms as $term){
if(in_array($term->id, $printed)) continue;
$referer_slug = (strpos($referer, '/'.$term->slug.'/'));
if(!$referer_slug==false){
$printed[] = $term->id;
$category_name = $term->name;
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
$ancestors = array_reverse( $ancestors );
foreach ( $ancestors as $ancestor ) {
$ancestor = get_term( $ancestor, 'product_cat' );
if ( ! is_wp_error( $ancestor ) && $ancestor )
echo $before . '' . $ancestor->name . '' . $after . $delimiter;
}
echo $before . '' . $category_name . '' . $after . $delimiter;
}
}
}
echo $before . get_the_title() . $after;
} else {
foreach ( $breadcrumb as $key => $crumb ) {
echo $before;
if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key + 1 ) {
echo '' . esc_html( $crumb[0] ) . '';
} else {
echo esc_html( $crumb[0] );
}
echo $after;
if ( sizeof( $breadcrumb ) !== $key + 1 ) {
echo $delimiter;
}
}
}
echo $wrap_after;
}
C/O: Joris Witteman
I reworked the code a little bit to account for a primary category and a referred one, having infinite parents.
I used the breadcrumb filter as it is quite clean, directly in the single product template, but in case you need it elsewhere you can use the single conditional.
function breadcumbs_referred_or_primary ($main, $terms)
{
// Our primary term is 520 (hardcoded)
$referer = wp_get_referer();
$referredTerm = -1;
$referredTermIndex = -1;
$primaryTermId = 520; // hardcoded
$primaryTermIndex = -1;
foreach($terms as $key => $term) {
if ($referredTerm != -1) break; // we found it in a previous iteration!
$ancestors = get_ancestors( $term->term_id, 'product_cat');
array_unshift($ancestors, $term->term_id);
if ($primaryTermIndex == -1 && in_array($primaryTermId, $ancestors)) $primaryTermIndex = $key;
foreach ($ancestors as $ancestor) {
if($referredTerm != -1) break 2; // we found it in a previous iteration!
$ancestor = get_term( $ancestor, 'product_cat' );
$referer_slug = (strpos($referer, '/'.$ancestor->slug.'/'));
if (!$referer_slug==false) { // it's found in the current level
$referredTerm = $term->term_id;
$referredTermIndex = $key;
}
}
}
// we return either the browsed terms or the primary term
if ($referredTermIndex != -1) {
return $terms[$referredTermIndex];
} else {
return $terms[$primaryTermIndex];
}
}
add_filter('woocommerce_breadcrumb_main_term', 'breadcumbs_referred_or_primary', 10, 2);
My solution is:
if (!empty($breadcrumb)) {
echo $wrap_before;
if (is_single() && get_post_type() == 'product') {
$breadcrumb_diff = [];
$breadcrumb_diff[] = $breadcrumb[0];
if ($terms = get_the_terms($post->ID, 'product_cat')) {
$referer = wp_get_referer();
$site_url = site_url();
$referer = str_replace($site_url . '/zoomagazin/', '', $referer);
$referer_array = explode('/', $referer);
foreach ($referer_array as $term_slug) {
$get_term_by_slug = get_term_by('slug', $term_slug, 'product_cat');
$breadcrumb_diff[] = [$get_term_by_slug->name, get_term_link($term_slug, 'product_cat')];
}
$breadcrumb_diff[]= $breadcrumb[count($breadcrumb) - 1];
foreach ($breadcrumb_diff as $key => $crumb) {
echo $before;
if (!empty($crumb[1]) && sizeof($breadcrumb_diff) !== $key + 1) {
echo '' . esc_html($crumb[0]) . '';
} else {
echo esc_html($crumb[0]);
}
echo $after;
if (sizeof($breadcrumb) !== $key + 1) {
echo $delimiter;
}
}
}
} else {
foreach ($breadcrumb as $key => $crumb) {
echo $before;
if (!empty($crumb[1]) && sizeof($breadcrumb) !== $key + 1) {
echo '' . esc_html($crumb[0]) . '';
} else {
echo esc_html($crumb[0]);
}
echo $after;
if (sizeof($breadcrumb) !== $key + 1) {
echo $delimiter;
}
}
}
echo $wrap_after;
}
This seems like a simple enough problem but I'm a novice at PHP and I've been working on this for hours. I'm looping through posts in an archive and showing a different logo for each post based on a certain attribute. Here's my existing function in functions.php:
function show_logo() {
global $post;
$attribute_names = array( 'pa_product-type'
);
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
if ( $term->name == 'L1' ) {
// Show L1 Logo
}
elseif ( $term->name == 'M1' ) {
// Show M1 Logo
}
elseif ( $term->name == 'H1' ) {
// Show H1 Logo
}
else {
$full_line = '<span>'. $term->name . '</span>';
}
array_push( $terms_array, $full_line );
}
echo implode( $terms_array );
}
}
}
}
All I want to do is do show a different logo if the post matches multiple terms (e.g. 'L1' AND 'M1'). I have tried many very different things but I have no idea if I'm even on the right track. Any help would be greatly appreciated.
This should be fairly easy, I'm just not exactly sure the full context of all the data involved.
Assuming you are only showing one logo per post, here is one approach:
Right before foreach ( $terms as $term ) { create three boolean variables:
$hasL1 = false;
$hasM1 = false;
$hasH1 = false;
Then when one of your term names matches, instead of just showing the logo set the appropriate variable equal to true, ie $hasL1 = true;
After the foreach is complete, either before or after echo implode( $terms_array ); depending on what makes sense, setup a new if/elseif/else block to decide what logo to show as follows:
if ($hasL1 && $hasM1 && $hasH1) { // Pick logo for all 3 }
elseif ($hasL1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1 && $hasH1) { // Pick logo for pair }
elseif ($hasH1 && $hasM1) { // Pick logo for pair }
elseif ($hasL1) { // Pick logo }
elseif ($hasM1) { // Pick logo }
elseif ($hasH1) { // Pick logo }
else { // default logo }
Of course there are many other ways to implement this too.