我正在使用Taxonomy Meta插件并且已经遵循了所有的操作,但感觉我正在做的事情有问题。我只想拉出分配给每个类别的图像是自定义分类并将其显示在页面模板上。
插件的github repo可以在这里找到:https://github.com/rilwis/taxonomy-meta
请帮忙,我得到的一切都要显示,但是图片和当我在源中查看图片路径时,只有一个空的img标签。
我可以用这个来打印图像:
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta['5']) ? $meta['5'] : array();
$images = $meta['community-image'];
echo '<ul>';
foreach ($images as $categories) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($categories, 'thumbnail');
$src = $src[0];
$args=array(
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'properties_community'
);
$categories=get_categories($args);
// show image
foreach($categories as $category) {
echo '<li><a href="' . home_url() . '/?property_communities='. $category->slug .'" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '><img src="'.$src.'"/>' . $category->name.'</a> </li> ';
}
}
echo '</ul>';
但我无法弄清楚如何在图像与显示的类别匹配的位置。我只是将数字5放在那里以查看它是否有效但是如果我尝试使用$ category-&gt; term_id它不起作用,我在这里有点迷失..
更新 我现在能够拉出分配给每个类别的图像并打印名称,但我收到此错误“警告:在/home/../themes/../my-template.php中为foreach()提供的参数无效第39行“
这是我的代码
$args = array( 'taxonomy' => 'properties_community' );
$terms = get_terms('properties_community', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$images = $meta['community-image'];
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'medium');
$src = $src[0];
// show image
echo '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '"><img src="'.$src.'" />' . $term->name . '</a>';
}}
}
任何人都明白为什么我会收到此错误?我没有看到foreach有什么问题:/
第39行是:foreach ($images as $att) {
答案 0 :(得分:1)
搞定了
$args = array( 'taxonomy' => 'properties_community' );
$terms = get_terms('properties_community', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="my_term-archive">';
foreach ($terms as $term) {
$meta = get_option('additional');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$images = $meta['community-image'];
if (empty($images)) {
echo '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '"><p style="text-align:center;">' . $term->name . '</p></a>';
} else {
foreach ($images as $att) {
// get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
$src = wp_get_attachment_image_src($att, 'medium');
$src = $src[0];
// show image
echo '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '"><img src="'.$src.'" /><p style="text-align:center;">' . $term->name . '</p></a>';
}}
}
}