我想在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 '<p>' . $category->description . '</p>';
中添加限制摘录长度?
谢谢
肖恩。
答案 0 :(得分:1)
echo '<p>' . mb_strimwidth($category->description, 0, 30, "...") . '</p>'
使用mb_strimwidth()功能
这将限制字符,如果超过30个字符,则添加点。
答案 1 :(得分:0)
您可以使用sting函数substr(),这是您正在寻找的解决方案:
<?php $description = $category->description; ?>
<p><?php echo substr($description ,0,30); ?> </p>