1

Can someone kindly tell me where to insert my javascript code below:

<?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. 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!

1
  • @Script47 Probably. Have a look at wp_enqueue_script which will hopefully suit your needs. Commented Nov 2, 2015 at 18:19

2 Answers 2

2

Assuming this is a wordpress blog you might have something like this:

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. Here is some information on the function: https://codex.wordpress.org/Function_Reference/wp_enqueue_script

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the reply. I do appreciate your time!
1

If you want the js to execute on the server, have a look at 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.

<?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'); 
?>>

3 Comments

Thank you so much for the reply. I do appreciate your time!
Hey @OlaviSau , where it says "##EXAMPLE HERE##", is this where I put "get_post_meta" or do I have to put a dot before it ".get_post_meta"? This is for a WordPress site (just in case you need to know).
@S.Johnson you put your javascript code there. Definitely take a look at the other answers. Inline javascript isnt as good as using the wp_enqueue_script. But what it seems to me is that you want to display some stuff. To do that: <?php echo "Hello World" ?>(must be in php tags, this will be processed server side, which mean you only see what is printed out(echo prints the string that follows it)). You can put anything into that echo statement, even javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.