gpt4 book ai didi

wordpress - 将自定义 HTML CSS Javascript 代码添加到某些 wordpress 帖子的正确方法是什么

转载 作者:行者123 更新时间:2023-12-04 07:21:51 30 4
gpt4 key购买 nike

我正在尝试向某些 wordpress 帖子添加自定义 HTML、CSSJQuery 代码,但我不知道我是否使用了正确的方法,因为我我只是将代码直接添加到帖子中。因为会有更多帖子可能需要使用自定义代码,并且使用这种方法我也必须复制/粘贴并自定义相同的代码到这些帖子。是否有更好的方法来做到这一点?

我对创建 wordpress 插件 了解不多,但一个想法告诉我插件是正确的方法,如果是这样,我怎样才能把它变成一个插件用于 wordpress?

代码示例如下:

<p style="text-align: left;">Post begins here and this is the text...
<div class="myDiv" >button</div>
<style type="text/css">
.myDiv{
color: #800080;
border: #000;
border-radius: 20px;
border-style: solid;
width: 50px;
}
</style>
<script type="text/javascript">
<!--
$(".farzn").on("click", function(){
alert('its Working');
});
//--></script>

最佳答案

Writing a Plugin非常简单:创建一个包含以下内容的 PHP 文件:

<?php
/* Plugin Name: Empty Plugin */

将其上传到您的 wp-content/plugins 文件夹,它将显示在插件列表中。

有趣的是, Hook wp_headwp_footer 可用于小型内联样式和脚本。检查Conditional Tags对于所有过滤可能性。

<?php
/* Plugin Name: Custom JS and CSS */

add_action( 'wp_head', 'my_custom_css' );
add_action( 'wp_footer', 'my_custom_js' );

function my_custom_css()
{
if( is_home() )
{
?>
<style type="text/css">
body {display:none}
</style>
<?php
}
if( is_page( 'about' ) )
{
?>
<style type="text/css">
body {background-color:red}
</style>
<?php
}
if( is_category( 'uncategorized' ) || in_category( array( 1 ) ) )
{
?>
<style type="text/css">
#site-title {display:none}
</style>
<?php
}
}

function my_custom_js()
{
?>
<script type="text/javascript">
<!--
jQuery("#site-description").on("click", function(){
alert('its Working');
});
//--></script>
<?php
}

最佳做法是使用操作钩子(Hook)将所有样式和脚本作为单独的文件排队 wp_enqueue_scripts .也可以使用条件标签。

关于wordpress - 将自定义 HTML CSS Javascript 代码添加到某些 wordpress 帖子的正确方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250759/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com