我正在尝试在 woocommerce 产品简短描述和描述选项卡中添加更多标签。普通的
<--more-->
标签在 WordPress 页面中不起作用。
页面网址:http://grumpy-albatross.w6.gravitydemo.com/product/custom-cake-printed-boxes/
现在看起来像这样 http://grumpy-albatross.w6.gravitydemo.com/product/custom-cake-printed-boxes/
我尝试添加 more
<--more-->
标签,但不起作用。我还尝试在 function.php 文件中使用另一篇文章中提到的下面的代码(限制 Woocommerce 中的产品简短描述长度),但效果不佳。
我希望能够在简短描述和详细描述中添加“阅读更多链接”。
/*==========================================================
Shorten product long description with read more button
=====================================================
=======*/
function filter_the_content( $content ) {
// Only for single product pages
if( ! is_product() ) return $content;
// Set the limit of words
$limit = 25;
// Strip p tags if needed
$content = str_replace( array( '<p>', '</p>' ), '', $content );
// If the content is longer than the predetermined limit
if ( str_word_count( $content, 0 ) > $limit ) {
// Returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
$arr = str_word_count( $content, 2 );
// Return all the keys or a subset of the keys of an array
$pos = array_keys( $arr );
// First part
$text = '<p>' . substr( $content, 0, $pos[$limit] ) . '<span id="dots">...</span>';
// More part
$text .= '<span id="more">' . substr( $content, $pos[$limit] ) . '</span></p>';
// Read button
$text .= '<button id="read-button"></button>';
$content = force_balance_tags( $text ); // needded
}
?>
<script type="text/javascript">
jQuery(document).ready( function ($) {
// Settings
var read_more_btn_txt = 'Read more';
var read_less_btn_txt = 'Read less';
// Selectors
var more = '#more';
var read_button = '#read-button';
var dots = '#dots';
// On load
$( more ).hide();
$( read_button ).html( read_more_btn_txt );
// On click
$( read_button ).on( 'click', function() {
if ( $( more ).is( ':hidden' ) ) {
$( more ).show();
$( dots ).hide();
$( read_button ).html( read_less_btn_txt );
} else {
$( more ).hide();
$( dots ).show();
$( read_button ).html( read_more_btn_txt );
}
});
});
</script>
<?php
return $content;
}
add_filter( 'the_content', 'filter_the_content', 10, 1 );