2

我正在使用 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->term_id 它不起作用,我我从这里有点迷路了..

更新: 我现在可以提取分配给每个类别的图像并打印名称,但我收到此错误“警告:为 /home/../themes/../my-template 中的 foreach() 提供的参数无效。第 39 行的 php"

这是我的代码

$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) {

4

1 回答 1

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

}}
}
}
于 2013-04-02T16:12:10.037 回答