我来这里是最后的手段,我希望这将是一种快速而简单的解决方法。

我一直在研究一些Javascript,以在同一页面的6个不同部分上创建“阅读更多...”和“ ...阅读较少”功能。

我有一段文字,我只想显示第一行。单击“阅读更多...”将显示其余内容。

下面的代码片段显示代码本身正在运行,但是尝试将其实际应用到我的网站/主题时遇到了很多麻烦。任何尝试将此应用到functions.php文件的尝试。



$(".more").toggle(function () {
    $(this).text("less..").siblings(".complete").show();
}, function () {
    $(this).text("more..").siblings(".complete").hide();
});

.complete {
    display:none;
}
.more {
    color:navy;
    cursor:pointer;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<span class="teaser">text goes here</span>

<span class="complete"> this is the
complete text being shown</span>

<span class="more">more...</span>





我遇到的问题是,每次尝试将其应用于我的functions.php时,它都会产生一个错误,导致整个网站瘫痪。

有人知道我在做什么错吗?或将其应用于wordpress主题的最佳方法是什么?

我正在使用的主题是“现代化”。

对此的任何帮助将是巨大的。

提前谢谢了。

最佳答案

尝试将上述代码片段添加到位于您的Assets文件夹中的js中,然后在您的functions.php文件中使用wp_register_script()wp_enqueue_script()add_action()函数添加对该文件的引用

function my_enqueued_scripts()
{

global $post;
// Register file that contains your js
wp_register_script('myscript', get_template_directory_uri() . '/assets /js/myscript.js');

if (isset($post)) {
    //load it when needed depending on the page -template-home.php
    if (is_page_template('template-home.php')) {
        wp_enqueue_script('myscript');
    }
}
}


然后调用你的函数

add_action('wp_enqueue_scripts', 'my_enqueued_scripts', 10);

09-10 10:29
查看更多