简体   繁体   English

将Javascript添加到php代码中

[英]Add Javascript to php code

Can someone kindly tell me where to insert my javascript code below: 有人可以告诉我在哪里插入我的javascript代码:

<?php
global $post,
$mk_options;
$page_layout = get_post_meta( $post->ID, '_layout', true );

$padding = get_post_meta( $post->ID, '_padding', true );


if ( empty( $page_layout ) ) {
$page_layout = 'full';
}
$padding = ($padding == 'true') ? 'no-padding' : '';


get_header(); ?>
<div id="theme-page" <?php echo get_schema_markup('main'); 
?>>

What is happening, the "get_post_meta" is causing my blog post dates to show in my Google SERP. 发生了什么,“get_post_meta”导致我的博客帖子日期显示在我的Google SERP中。 I did some research and am aware I need to add the code below, I'm just unsure where to insert it in the code above. 我做了一些研究,我知道我需要添加下面的代码,我只是不确定在上面的代码中将其插入的位置。

<script language="javascript" type="text/javascript">document.write 
(I believe my code goes here) </script>

I would really appreciate some help. 我真的很感激一些帮助。 This is my first time posting here, so if I am not posting questions properly, just let me know! 这是我第一次在这里发帖,所以如果我没有正确发布问题,请告诉我!

Assuming this is a wordpress blog you might have something like this: 假设这是一个wordpress博客,你可能会有这样的事情:

global $post,
$mk_options;
$page_layout = get_post_meta( $post->ID, '_layout', true );
$padding = get_post_meta( $post->ID, '_padding', true );

if ( empty( $page_layout ) )
    $page_layout = 'full';
$padding = ($padding == 'true') ? 'no-padding' : '';

/// starts here
$handle = 'some-handle';
$js = 'http://example.com/my.js';
wp_register_script( $handle, $js );
wp_enqueue_script( $handle );
/// ends here

get_header();

Afterwards, put your js code in my.js in an external file and you will be good to go. 然后,将你的js代码放在my.js的外部文件中,你就可以了。 Here is some information on the function: https://codex.wordpress.org/Function_Reference/wp_enqueue_script 以下是该函数的一些信息: https//codex.wordpress.org/Function_Reference/wp_enqueue_script

If you want the js to execute on the server, have a look at node.js 如果您希望js在服务器上执行,请查看node.js

If you want the js to execute in the browser, either echo it or close the php tag insert the script and then reopen it. 如果你想让js在浏览器中执行,要么回显它,要么关闭php标签插入脚本,然后重新打开它。

<?php
global $post,
$mk_options;
$page_layout = get_post_meta( $post->ID, '_layout', true );

$padding = get_post_meta( $post->ID, '_padding', true );


if ( empty( $page_layout ) ) {
$page_layout = 'full';
}
$padding = ($padding == 'true') ? 'no-padding' : '';


get_header(); ?>
<script type="text/javascript">##EXAMPLE HERE##</script>
<div id="theme-page" <?php echo get_schema_markup('main'); 
?>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM