WordPress 6.2.2 – 产品分类描述的“阅读更多” – “阅读更少”

huangapple go评论71阅读模式
英文:

WordPress 6.2.2 - Read more - Read Less for Product Category description

问题

我正在使用以下js文件以以下方式实现在WooCommerce产品类别描述中的“阅读更多”功能。

function myFunction() {
  var dots = document.getElementById("dots");
  var moreText = document.getElementById("more");
  var btnText = document.getElementById("myBtn");

  if (dots.style.display === "none") {
    dots.style.display = "inline";
    btnText.innerHTML = "阅读更多"; 
    moreText.style.display = "none";
  } else {
    dots.style.display = "none";
    btnText.innerHTML = "关闭传记"; 
    moreText.style.display = "inline";
  }
}

还有这个CSS:

#more {display: none;}

我在functions文件中使用上述js文件如下:

function enqueue_read_more_script() {
  wp_enqueue_script( 'read-more-script', get_stylesheet_directory_uri() . '/js/read-more-script.js', array(), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_read_more_script' );

在产品类别描述中,我使用以下方法来放置“阅读更多”分隔。

<span id="dots"></span><span id="more">

在描述的末尾,使用以下方法来关闭(Read less)

<span onclick="myFunction()" id="myBtn">阅读更多</span>

例如,它会看起来像这样(也附带了屏幕截图)

Lorem Ipsum is simply dummy text of the printing and typesetting industry. <span id="dots"></span><span id="more">
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
<span onclick="myFunction()" id="myBtn">阅读更多</span>

一旦我点击更新,这些元素会消失,但末尾的“阅读更多”会显示为文本,无论是在前端还是在后端的产品类别描述中。

我不确定我需要做什么来使代码工作。

英文:

I'm using the following js file in the following manner to achieve the read more functionality in the Product Category description in woocommerce.

function myFunction() {
  var dots = document.getElementById(&quot;dots&quot;);
  var moreText = document.getElementById(&quot;more&quot;);
  var btnText = document.getElementById(&quot;myBtn&quot;);

  if (dots.style.display === &quot;none&quot;) {
    dots.style.display = &quot;inline&quot;;
    btnText.innerHTML = &quot;Read more&quot;; 
    moreText.style.display = &quot;none&quot;;
  } else {
    dots.style.display = &quot;none&quot;;
    btnText.innerHTML = &quot;Close Biography&quot;; 
    moreText.style.display = &quot;inline&quot;;
  }
}

Along with this css

#more {display: none;}

I'm using the above js file in the functions file as follows

function enqueue_read_more_script() {
  wp_enqueue_script( &#39;read-more-script&#39;, get_stylesheet_directory_uri() . &#39;/js/read-more-script.js&#39;, array(), &#39;1.0&#39;, true );
}
add_action( &#39;wp_enqueue_scripts&#39;, &#39;enqueue_read_more_script&#39; );

In the product category description I'm using the following to place the "Read more" break off.

&lt;span id=&quot;dots&quot;&gt;&lt;/span&gt;&lt;span id=&quot;more&quot;&gt;

and at the end of the description using the following to close (Read less)

&lt;span onclick=&quot;myFunction()&quot; id=&quot;myBtn&quot;&gt;Read more&lt;/span&gt;

So for example, it would look like this (screenshot also attached)

Lorem Ipsum is simply dummy text of the printing and typesetting industry. &lt;span id=&quot;dots&quot;&gt;&lt;/span&gt;&lt;span id=&quot;more&quot;&gt;
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
&lt;span onclick=&quot;myFunction()&quot; id=&quot;myBtn&quot;&gt;Read more&lt;/span&gt;

WordPress 6.2.2 – 产品分类描述的“阅读更多” – “阅读更少”

Once I click update the spans disappear but the "Read More" at the end displays as text both on the front end and at the product category description in the backend.

I'm not sure what I need to do to get the code to work.

答案1

得分: 1

你可以在你的Javascript中委托点击事件,如下所示:

const el = document.getElementById("myBtn");
el.addEventListener("click", myFunction);
英文:

You can delegate the click event in your Javascript like so

const el = document.getElementById(&quot;myBtn&quot;);
el.addEventListener(&quot;click&quot;, myFunction);

答案2

得分: 0

SOLUTION 1:
解决方案 1
@mina在上面发布的最有效的解决方案,经过一些修改/添加。

JS文件在子主题/js/read-more.js中

jQuery(document).ready(function($) {
  function myFunction() {
    var dots = $("#dots");
    var moreText = $("#more");
    var btnText = $("#myBtn");

    if (dots.css("display") === "none") {
      dots.css("display", "inline");
      btnText.text("阅读更多");
      moreText.css("display", "none");
    } else {
      dots.css("display", "none");
      btnText.text("关闭传记");
      moreText.css("display", "inline");
    }
  }

  $("#myBtn").on("click", myFunction);
});

在功能文件中:

// 允许类别(标签)描述中的HTML
foreach ( array( 'pre_term_description' ) as $filter ) {
	remove_filter( $filter, 'wp_filter_kses' );
	if ( ! current_user_can( 'unfiltered_html' ) ) {
		add_filter( $filter, 'wp_filter_post_kses' );
	}
}

foreach ( array( 'term_description' ) as $filter ) {
	remove_filter( $filter, 'wp_kses_data' );
}
// 移除向类别描述添加<p>标签的默认过滤器
remove_filter( 'term_description', 'wpautop' );

// 加载用于阅读更多/阅读更少功能的JavaScript
function enqueue_read_more_script() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'read-more-script', get_stylesheet_directory_uri() . '/js/read-more.js', array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_read_more_script' );

SOLUTION 2:
解决方案 2 - 更复杂的方法

覆盖位于WooCommerce插件或GitHub中的模板templates/archive-product.php,然后在子主题中重新创建文件,如下所示:

将以下内容:

?>
<header class="woocommerce-products-header">
    <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
        <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
    <?php endif; ?>

    <?php
    /**
     * Hook: woocommerce_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    do_action( 'woocommerce_archive_description' );
    ?>
</header>
<?php

替换为:

?>
<header class="woocommerce-products-header">
    <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
        <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
    <?php endif; ?>

    <?php
    /**
     * 自定义挂钩:woocommerce_custom_archive_description。
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    do_action( 'woocommerce_custom_archive_description' );
    ?>
</header>
<?php

然后在功能文件中添加以下内容:

// 允许类别(标签)描述中的HTML
foreach ( array( 'pre_term_description' ) as $filter ) {
	remove_filter( $filter, 'wp_filter_kses' );
	if ( ! current_user_can( 'unfiltered_html' ) ) {
		add_filter( $filter, 'wp_filter_post_kses' );
	}
}

foreach ( array( 'term_description' ) as $filter ) {
	remove_filter( $filter, 'wp_kses_data' );
}
// 移除向类别描述添加<p>标签的默认过滤器
remove_filter( 'term_description', 'wpautop' );

// 加载用于阅读更多/阅读更少功能的JavaScript
function enqueue_read_more_script() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'read-more-script', get_stylesheet_directory_uri() . '/js/read-more.js', array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_read_more_script' );

// 修改类别描述输出
function woocommerce_taxonomy_archive_description() {
    if ( is_product_category() ) {
        $term_id = get_queried_object_id();
        $description = get_term_field( 'description', $term_id, 'product_cat' );

        // 以必要的HTML结构和ID显示类别描述
        echo '<div>' . wpautop( $description ) . '<span id="dots"></span><span id="more">' . $description . '</span><span onclick="myFunction()" id="myBtn">阅读更多</span></div>';
    }
}

两种解决方案的使用方法:
在CSS文件中添加:

#more {display:none;}

要在产品类别描述中使用它,请按如下方式进行:

Lorem Ipsum is simply dummy text of the printing and typesetting industry. <span id="dots"></span><span id="more">
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</span>

<span onclick="myFunction()" id="myBtn">阅读更多</span>
英文:

Two solutions that worked.

SOLUTION 1
Most efficient solution as posted by @mina above with some modification/addition.

JS file in child theme /js/read-more.js

jQuery(document).ready(function($) {
  function myFunction() {
    var dots = $(&quot;#dots&quot;);
    var moreText = $(&quot;#more&quot;);
    var btnText = $(&quot;#myBtn&quot;);

    if (dots.css(&quot;display&quot;) === &quot;none&quot;) {
      dots.css(&quot;display&quot;, &quot;inline&quot;);
      btnText.text(&quot;Read more&quot;);
      moreText.css(&quot;display&quot;, &quot;none&quot;);
    } else {
      dots.css(&quot;display&quot;, &quot;none&quot;);
      btnText.text(&quot;Close Biography&quot;);
      moreText.css(&quot;display&quot;, &quot;inline&quot;);
    }
  }

  $(&quot;#myBtn&quot;).on(&quot;click&quot;, myFunction);
});

in functions file

    // Allow HTML in term (category, tag) descriptions
foreach ( array( &#39;pre_term_description&#39; ) as $filter ) {
	remove_filter( $filter, &#39;wp_filter_kses&#39; );
	if ( ! current_user_can( &#39;unfiltered_html&#39; ) ) {
		add_filter( $filter, &#39;wp_filter_post_kses&#39; );
	}
}
 
foreach ( array( &#39;term_description&#39; ) as $filter ) {
	remove_filter( $filter, &#39;wp_kses_data&#39; );
}
// Remove the default filter that adds &lt;p&gt; tags to category descriptions
remove_filter( &#39;term_description&#39;, &#39;wpautop&#39; );

// Enqueue JavaScript for read more/read less functionality
function enqueue_read_more_script() {
    wp_enqueue_script( &#39;jquery&#39; );
    wp_enqueue_script( &#39;read-more-script&#39;, get_stylesheet_directory_uri() . &#39;/js/read-more.js&#39;, array( &#39;jquery&#39; ), &#39;1.0&#39;, true );
}
add_action( &#39;wp_enqueue_scripts&#39;, &#39;enqueue_read_more_script&#39; );

SOLUTION 2 - More complicated

Override the template templates/archive-product.php located in the Woocommerce plugin or github re-create the file in your child theme as follows,

Replace

?&gt;
&lt;header class=&quot;woocommerce-products-header&quot;&gt;
	&lt;?php if ( apply_filters( &#39;woocommerce_show_page_title&#39;, true ) ) : ?&gt;
		&lt;h1 class=&quot;woocommerce-products-header__title page-title&quot;&gt;&lt;?php woocommerce_page_title(); ?&gt;&lt;/h1&gt;
	&lt;?php endif; ?&gt;

	&lt;?php
	/**
	 * Hook: woocommerce_archive_description.
	 *
	 * @hooked woocommerce_taxonomy_archive_description - 10
	 * @hooked woocommerce_product_archive_description - 10
	 */
	do_action( &#39;woocommerce_archive_description&#39; );
	?&gt;
&lt;/header&gt;
&lt;?php

with

?&gt;
&lt;header class=&quot;woocommerce-products-header&quot;&gt;
    &lt;?php if ( apply_filters( &#39;woocommerce_show_page_title&#39;, true ) ) : ?&gt;
        &lt;h1 class=&quot;woocommerce-products-header__title page-title&quot;&gt;&lt;?php woocommerce_page_title(); ?&gt;&lt;/h1&gt;
    &lt;?php endif; ?&gt;

    &lt;?php
    /**
     * Custom Hook: woocommerce_custom_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    do_action( &#39;woocommerce_custom_archive_description&#39; );
    ?&gt;
&lt;/header&gt;
&lt;?php

Then in the functions file add the following

        // Allow HTML in term (category, tag) descriptions
foreach ( array( &#39;pre_term_description&#39; ) as $filter ) {
	remove_filter( $filter, &#39;wp_filter_kses&#39; );
	if ( ! current_user_can( &#39;unfiltered_html&#39; ) ) {
		add_filter( $filter, &#39;wp_filter_post_kses&#39; );
	}
}
 
foreach ( array( &#39;term_description&#39; ) as $filter ) {
	remove_filter( $filter, &#39;wp_kses_data&#39; );
}
// Remove the default filter that adds &lt;p&gt; tags to category descriptions
remove_filter( &#39;term_description&#39;, &#39;wpautop&#39; );

// Enqueue JavaScript for read more/read less functionality
function enqueue_read_more_script() {
    wp_enqueue_script( &#39;jquery&#39; );
    wp_enqueue_script( &#39;read-more-script&#39;, get_stylesheet_directory_uri() . &#39;/js/read-more.js&#39;, array( &#39;jquery&#39; ), &#39;1.0&#39;, true );
}
add_action( &#39;wp_enqueue_scripts&#39;, &#39;enqueue_read_more_script&#39; );


// Modify the category description output
function woocommerce_taxonomy_archive_description() {
    if ( is_product_category() ) {
        $term_id = get_queried_object_id();
        $description = get_term_field( &#39;description&#39;, $term_id, &#39;product_cat&#39; );

        // Display the term description with the necessary HTML structure and IDs
        echo &#39;&lt;div&gt;&#39; . wpautop( $description ) . &#39;&lt;span id=&quot;dots&quot;&gt;&lt;/span&gt;&lt;span id=&quot;more&quot;&gt;&#39; . $description . &#39;&lt;/span&gt;&lt;span onclick=&quot;myFunction()&quot; id=&quot;myBtn&quot;&gt;Read more&lt;/span&gt;&lt;/div&gt;&#39;;
    }
}

Create the following js file in the child theme /js/read-more.js

jQuery(document).ready(function($) {
  function myFunction() {
    var dots = $(&quot;#dots&quot;);
    var moreText = $(&quot;#more&quot;);
    var btnText = $(&quot;#myBtn&quot;);

    if (dots.css(&quot;display&quot;) === &quot;none&quot;) {
      dots.css(&quot;display&quot;, &quot;inline&quot;);
      btnText.text(&quot;Read more&quot;);
      moreText.css(&quot;display&quot;, &quot;none&quot;);
    } else {
      dots.css(&quot;display&quot;, &quot;none&quot;);
      btnText.text(&quot;Close Biography&quot;);
      moreText.css(&quot;display&quot;, &quot;inline&quot;);
    }
  }

  $(&quot;#myBtn&quot;).on(&quot;click&quot;, myFunction);
});

USAGE FOR BOTH SOLUTIONS

in the css file add

#more {display:none;}

and to use in the product category description use as follows,

Lorem Ipsum is simply dummy text of the printing and typesetting industry. &lt;span id=&quot;dots&quot;&gt;&lt;/span&gt;&lt;span id=&quot;more&quot;&gt;
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
&lt;/span&gt;

&lt;span onclick=&quot;myFunction()&quot; id=&quot;myBtn&quot;&gt;Read more&lt;/span&gt;

huangapple
  • 本文由 发表于 2023年6月15日 03:02:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476793.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定