combine Code 1 with Code 2

时间:2019-11-12 作者:aye cee

代码1-使用wp\\U list\\U categories在x个类别(最多y个)的列中列出产品类别,如果超过y个类别,则带有查看所有类别的链接。

代码2-后来意识到我也需要缩略图,我尝试使用get\\u术语检索列表和缩略图。然而,这只是一个很长的列表,我很难将代码1到代码2的原则应用到工作中。

这是代码2:

<?php           
                $args = array(
                \'taxonomy\'   => \'product_cat\',
                \'title_li\'   => \'\',
                \'hide_empty\' => 0,
                \'echo\'       => 0,
                \'show_count\' => 0,
                \'style\'      => \'\',
                \'parent\'    => 0
                );

$get_cats = get_terms( $args );
        ?>
<div class="design">
   <div class="menu">
       <ul class="hoverimage">
<?php
    foreach ( $get_cats as $cat ) {
    $thumbnail_id = get_term_meta( $cat->term_id, \'thumbnail_id\', true ); 
    $image = wp_get_attachment_url( $thumbnail_id ); 

   if($image == "") {
   $image = get_bloginfo(\'url\') . \'/wp-content/uploads/woocommerce-placeholder-100x100.png\';
    }
?>
    <li class="menu-item" data-image="<?php echo $image; ?>"><a href="<?php echo get_term_link( $cat ); ?>"><?php echo $cat->name; ?></a></li>
    <?php
      }
    ?>
       </ul>
   </div>
    <div class="zoom-preview">
        <div class=\'img_container\'>
        </div>
    </div>
   <div class="clear"></div>
</div>

<script>
$(function(){
    var imageContainer = \'.img_container\',
        imageList      = \'.hoverimage\',
        maxWidth       = \'parent\', // parent or specific css width/  
        defImage       = \'<?php bloginfo(\'url\'); ?>/wp-content/uploads/woocommerce-placeholder-100x100.png\';

    var $imageContainer = $(imageContainer).eq(0);
    var $imageList      = $(imageList).eq(0);
    if (maxWidth === \'parent\') { maxWidth = $imageContainer.width() + \'px\'; }
    //Load images and set hover::
    $imageList.find(\'li\').each(function(index){
        if (typeof $(this).data(\'image\') === \'string\') {
            $imageContainer.append(
                "<img id=\'imageToggle"+index+
                "\' src=\'"+$(this).data(\'image\')+
                "\' style=\'max-width: "+maxWidth+"; display:none;\' />"
            );
            $(this).data("image","imageToggle"+index);
            $(this).hover(
                function(){ loadImage($(this).data(\'image\')); },
                function(){ loadImage(\'imageToggleDef\'); }
            );  
        }
    });
    //Load default:
    $imageContainer.append(
                "<img id=\'imageToggleDef"+
                "\' src=\'"+defImage+
                "\' style=\'max-width: "+maxWidth+"\' />"
    );
    //Toggle images:
    function loadImage(imageId) {
        $imageContainer.stop(true).fadeOut(\'fast\', function(){
            $(this).find(\'img\').hide();
            $(this).find(\'img#\'+imageId).show();
            $(this).fadeIn(\'fast\');
        });
    }

});
</script>


这是代码1。我试图将其与代码2结合起来,同时仍然使用get\\u术语而不是wp\\u list\\u类别。我注意到你不能分解get\\U条款的结果,但在$i似乎是字符串而不是数字的情况下遇到了麻烦。。。。这让我不知所措,我犯了很多错误。

// First, define these.
$max_cat_count = 32; // this is \'x\'
$qty_per_column = 8; // this is \'y\'

// Then the $args array.
$args = array(
    \'taxonomy\'   => \'product_cat\',
    \'number\'     => $max_cat_count + 1, // keep the + 1
    \'title_li\'   => \'\',
    \'hide_empty\' => 0,
    \'echo\'       => 0,
    \'style\'      => \'\',
    // ... your other args here ...
);

// Get the categories list.
$get_cats = wp_list_categories( $args );

// Split the list into array items.
$cat_array = explode( "<br />", $get_cats );

$total = count( $cat_array );
$list_number = 1;
$_new_col = false;

foreach ( $cat_array as $i => $category ) {
    if ( $i >= $max_cat_count ) {
        break;
    }

    if ( $i % $qty_per_column === 0 ) {
        // Close previous column, if any.
        if ( $_new_col ) {
            echo \'</div><!-- .cat_columns -->\' . "\\n";
        }

        // Open new column.
        $id = \'cat-col-\' . $list_number;
        echo \'<div class="cat_columns" id="\' . $id . \'">\' . "\\n";

        $_new_col = true;
        $list_number++; // increment the columns count
    }

    if ( $total > $max_cat_count && $i === $max_cat_count - 1 ) {
        // Make sure to change the # to the proper URL.
        echo "\\t" . \'<div><a href="#">View All</a></div>\' . "\\n";
    } else {
        echo "\\t" . \'<div>\' . trim( $category ) . \'</div>\' . "\\n";
    }
}
// Close last column, if any.
if ( $_new_col ) {
    echo \'</div><!-- .cat_columns -->\' . "\\n";
}
输出如下:enter image description here

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

修改的PHP代码

<?php
// First, define these.
$max_cat_count = 32; // this is \'x\'
$qty_per_column = 8; // this is \'y\'

// Then the $args array.
$args = array(
    \'taxonomy\'   => \'product_cat\',
    \'number\'     => $max_cat_count + 1, // keep the + 1
    \'hide_empty\' => 0,
    // ... your other args here ...
);

// Get the categories list.
$get_cats = get_terms( $args );
$get_cats = ( ! is_wp_error( $get_cats ) ) ? $get_cats : [];

// Default thumbnail URL.
$default_thumb = content_url( \'/uploads/woocommerce-placeholder-100x100.png\' );

$total = count( $get_cats );
$list_number = 1;
$_new_col = false;

$columns = \'\';
foreach ( $get_cats as $i => $cat ) {
    if ( $i >= $max_cat_count ) {
        break;
    }

    if ( $i % $qty_per_column === 0 ) {
        // Close previous column, if any.
        if ( $_new_col ) {
            $columns .= \'</ul></div><!-- .cat_columns -->\';
        }

        // Open new column.
        $id = \'cat-col-\' . $list_number;
        $columns .= \'<div class="menu cat_columns" id="\' . $id . \'">\';
        $columns .= \'<ul class="hoverimage">\';

        $_new_col = true;
        $list_number++; // increment the columns count
    }

    if ( $total > $max_cat_count && $i === $max_cat_count - 1 ) {
        // Make sure to change the # to the proper URL.
        $columns .= \'<li class="menu-item"><a href="#">View All</a></li>\';
    } else {
        $thumbnail_id = get_term_meta( $cat->term_id, \'thumbnail_id\', true );
        $image = $thumbnail_id ? wp_get_attachment_url( $thumbnail_id ) : \'\';
        // If $image is empty, just let it be. JS will set it to $default_thumb.
        // That way, the page source/HTML will be less.

        $link = \'<a href="\' . esc_url( get_term_link( $cat ) ) . \'">\' . esc_html( $cat->name ) . \'</a>\';
        $columns .= \'<li class="menu-item" data-image="\' . esc_url( $image ) . \'">\' . $link . \'</li>\';
    }
}
// Close last column, if any.
if ( $_new_col ) {
    $columns .= \'</ul></div><!-- .cat_columns -->\';
}
?>
<div class="design">
    <div class="zoom-preview">
        <div class="img_container" data-default_thumb="<?php echo esc_url( $default_thumb ); ?>">
        </div>
    </div>
    <?php echo $columns; ?>
    <div class="clear"></div>
</div>

修改的jQuery代码

jQuery(function( $ ){
    var imageContainer = \'.img_container\',
        imageList      = \'.hoverimage\',
        maxWidth       = \'100%\'; // parent or specific css width;

    var $imageContainer = $(imageContainer).eq(0);
    var $imageList      = $(imageList);

    /*
     * I moved this var to here, and use data-default_thumb to specify the
     * default thumbnail URL. That way, you can - and should - avoid using
     * PHP in JS. So your opening DIV tag would look like:
     * <div class="img_container" data-default_thumb="http://example.com/image.png">
     */
    var defImage        = $imageContainer.data( \'default_thumb\' );

    if (maxWidth === \'parent\') {
        maxWidth = $imageContainer.width() + \'px\';
    }

    // Load images and set hover::
    $imageList.find(\'li[data-image]\').each(function(index){
        if (typeof $(this).data(\'image\') === \'string\') {
            var thumb_url = $( this ).data( \'image\' ) || defImage;
            $imageContainer.append(
                "<img id=\'imageToggle"+index+
                "\' src=\'"+ thumb_url +
                "\' style=\'max-width: "+maxWidth+"; max-height:100%; display:none;\' />"
            );
            $(this).data("image","imageToggle"+index);
            $(this).hover(
                function(){ loadImage($(this).data(\'image\')); },
                function(){ loadImage(\'imageToggleDef\'); }
            );
        }
    });

    // Load default:
    $imageContainer.append(
        "<img id=\'imageToggleDef"+
        "\' src=\'"+defImage+
        "\' style=\'max-width: "+maxWidth+"; max-height:100%\' />"
    );

    // Toggle images:
    function loadImage(imageId) {
        $imageContainer.stop(true).fadeOut(\'fast\', function(){
            $(this).find(\'img\').hide();
            $(this).find(\'img#\'+imageId).show();
            $(this).fadeIn(\'fast\');
        });
    }
});

示例CSS代码

.cat_columns {
    float: left;
    width: 20%;
}

.zoom-preview {
    float: left;
    width: 20%;
    text-align: center;
}

.clear {
    clear: both;
}
因此,基本上,只需“玩”CSS即可获得正确的布局;e、 g.在没有缩略图预览的情况下.cat_columns 宽度为25% (一行中总共有4列)。

在实现所有代码后,生成的标记/HTML将如下所示:

<div class="design">
  <div class="zoom-preview">
    <div class="img_container" data-default_thumb="{Default thumbnail URL}">
      {Thumbnails added via JS}
    </div>
  </div>
  <div class="menu cat_columns">
    <ul class="hoverimage">
      <li class="menu-item" data-image="{Thumbnail URL}"><a href="{Category URL}">{Category Name}</a></li>
      <li class="menu-item" data-image="{Thumbnail URL}"><a href="{Category URL}">{Category Name}</a></li>
      ...
    </ul>
  </div>
  <div class="menu cat_columns">
    <ul class="hoverimage">
      <li class="menu-item" data-image="{Thumbnail URL}"><a href="{Category URL}">{Category Name}</a></li>
      <li class="menu-item" data-image="{Thumbnail URL}"><a href="{Category URL}">{Category Name}</a></li>
      ...
    </ul>
  </div>
  ...
  <div class="clear"></div>
</div>

Try a demo on JS Fiddle

附加注释title_li, echo, styleshow_count 将与一起使用wp_list_categories(), 并且对get_terms().

我用过content_url() 要检索WordPress“content”URL,如下所示http://example.com/wp-content.

测试完所有内容后,应该将jQuery脚本放在外部脚本中,并使用wp_enqueue_scripts 挂钩和wp_enqueue_script() 加载脚本。(我最近发布了this answer 这可能会帮助你wp_enqueue_script().)

如果要立即加载默认缩略图,则应手动添加<img> 到HTML:

<!-- With this version, no need for data-default_thumb -->
<div class="img_container">
        <img id="imageToggleDef" src="<?php echo esc_url( $default_thumb ); ?>" alt="" style="max-width: 100%; max-height: 100%" />
</div>
您可以尝试演示here 并且确保在那里使用JS,而不是上面最初发布的JS。

相关推荐

Php.ini中的UPLOAD_max_FILESIZE错误

我在上载大约2.3 MB大小的下载主题时出错,上载的文件超过了php中的upload\\u max\\u filesize指令。ini。当我尝试将upload\\u max\\u filesize=10MB的值从默认的2M更改为所有php时。ini,php。ini开发和php。ini生产我也遇到了同样的错误。我无法上载我下载的主题,而其大小约为2.3 MB。我正在使用XAMPP。提前谢谢。