我有一个优惠券网站,里面基本上有很多供应商。我想创建一个相关的商店小部件,它会在侧边栏上显示 5-6 家商店。我想让他们与当前的供应商页面相关联。到目前为止,我已经设法随机显示 5-6 个商店链接,但我无法显示相关商店。我在 wordpress 中使用 Clipper 主题,并且我将所有商店都保存在优惠券商店而不是邮政类别中。
<?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 href=\"". get_category_link($rand_store->cat_ID) . "\"> $a_text </a></div>";
$displayed_cntr++;
}
$nonempty_stores[$rand_store_idx] = $nonempty_stores[$cntr];
$nonempty_stores[$cntr] = $rand_store;
$cntr++;
}
echo "</div>";
?>
欢迎任何其他方法。我试图展示所有这样的商店,
$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><a href="' . $term_link . '">' . $term->name . '</a></li>';
}
echo '</ul>';
它显示,但我无法显示相关商店,我正在努力弄清楚我应该以哪些理由继续。任何帮助,将不胜感激。