【问题标题】:How to Limit Excerpt Length in $category->description in archive-products.php WordPress?如何在 archive-products.php WordPress 中限制 $category->description 中的摘录长度?
【发布时间】:2018-12-30 19:38:45
【问题描述】:

我想在echo '<p>' . $category->description . '</p>'; 中将摘录长度限制为 30 个字符,这是我的代码:

<?php
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = true ;
    $cat_args = array(
        'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,
    );
    $product_categories = get_terms( 'product_category', $cat_args );
        if( !empty($product_categories) ){
            echo '<div class="container">';
            echo '<div class="row">';
            foreach ($product_categories as $key => $category) {
                echo '<div class="col-lg-6">';
                echo '<div class="card">';
                echo '<a href="'.get_term_link($category).'" >';
                $image = get_field('product_category', $category );
                if($image) {
                    echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
                    } else {
                        echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
                    }
                echo '<h3>' . $category->name . '</h3>';
                //echo $category->name;
                echo '<p>' . $category->description . '</p>';
                echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($category).'" >Discover more</button>';
            echo '</a>';
            echo '</div>';
            echo '</div>';
        }
        echo '</div>';
        echo '</div>';
    }
    else {
        // no posts found
        echo wpautop( 'Sorry, no products were found' );
    }
?>

如何在echo '&lt;p&gt;' . $category-&gt;description . '&lt;/p&gt;'; 中添加限制摘录长度?

谢谢,

肖恩。

【问题讨论】:

    标签: php wordpress categories archive


    【解决方案1】:
    echo '<p>' . mb_strimwidth($category->description, 0, 30, "...") . '</p>'
    

    使用mb_strimwidth()函数

    这将限制字符,如果超过 30 个字符,则添加点。

    【讨论】:

    • 嗨@Nawin,我要把这个string mb_strimwidth ( string $str , int $start , int $width [, string $trimmarker = "" [, string $encoding = mb_internal_encoding() ]] ) 放到functions.php 或archive-products.php 中吗?
    • @Bubble 你在哪里显示描述?我的意思是在functions.php 或archive.php 文件中?
    • 在archive-products.php中
    • @Bubble 然后将该行添加到 archive-products.php
    • @Bubble 你只需替换这一行 "echo '

      ' . $category->description . '

      ';"到这一行 "echo '

      ' . mb_strimwidth($category->description, 0, 30, "...") . '

      ';"
    【解决方案2】:

    您可以使用 sting 函数 substr() 给出的是您正在寻找的解决方案:

    <?php $description = $category->description; ?>
    
    <p><?php echo substr($description ,0,30); ?> </p>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-12
      • 2011-05-04
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多