[英]How do you run a JavaScript function on a specific Wordpress page?
I'm trying to run the following script on a specific Wordpress page, but it's not working. 我正在尝试在特定的Wordpress页面上运行以下脚本,但无法正常工作。 The script does work but when not applied to the specific page. 该脚本可以工作,但是不适用于特定页面。 Yes the page id is correct. 是的,页面ID是正确的。 What did I do wrong? 我做错了什么? Thanks in advance. 提前致谢。
<?php if (is_page('page-id-48857') ):?>
<script>
jQuery(document).ready(function ($) {
$(function () {
var $content = $('#jsonContent');
var data = {
rss_url: 'https://inside.calpoly.edu/feed'
};
$.get('https://api.rss2json.com/v1/api.json', data, function (response) {
if (response.status == 'ok') {
var output = '';
$.each(response.items, function (k, item) {
output += '<div class="post-card category-medium published">';
//output += '<h3 class="date">' + $.format.date(item.pubDate, "dd<br>MMM") + "</h4>";
var tagIndex = item.description.indexOf('<img'); // Find where the img tag starts
var srcIndex = item.description.substring(tagIndex).indexOf('src=') + tagIndex; // Find where the src attribute starts
var srcStart = srcIndex + 5; // Find where the actual image URL starts; 5 for the length of 'src="'
var srcEnd = item.description.substring(srcStart).indexOf('"') + srcStart; // Find where the URL ends
var src = item.description.substring(srcStart, srcEnd); // Extract just the URL
output += '<p class="post-meta">';
//output += '<span class="published">' + item.pubDate + '</span>';
output += '<a href="https://inside.calpoly.edu/" target="_blank">@inside.calpoly.edu</span></a>';
output += '</p>';
output += '<h2 class="entry-title">' + item.title + '</h2>';
//output += '<div class="post-meta"><span>By ' + item.author + '</span></div>';
var yourString = item.description.replace(/<img[^>]*>/g,""); //replace with your string.
var maxLength = 300 // maximum number of characters to extract
//trim the string to the maximum length
var trimmedString = yourString.substr(0, maxLength);
//re-trim if we are in the middle of a word
trimmedString = trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(" ")))
output += '<div class="excerpt">'+trimmedString + '</div>';
output += '<a href="'+ item.link + '" class="more-link cpc-button activeghostdark small">Read More</a>';
output += '<a class="entry-featured-image-url" href="'+ item.link + '"><img src="' + src + '"></a>';
output += '</div>';
return k < 1;
});
$content.html(output);
}
});
});
</script>
<?php endif; ?>
You are passing in an invalid ID to the is_page()
function. 您正在将无效的ID传递给is_page()
函数。
Based off of your code sample, you should be using an integer for your post ID and not a string and also not the 'page-id' portion. 根据您的代码示例,您应该为帖子ID使用整数,而不是字符串,也不要使用“ page-id”部分。
https://developer.wordpress.org/reference/functions/is_page/ https://developer.wordpress.org/reference/functions/is_page/
Here's some example usages: 这是一些用法示例:
// When any single Page is being displayed.
is_page();
// When Page 42 (ID) is being displayed.
is_page( 42 );
// When the Page with a post_title of "Contact" is being displayed.
is_page( 'Contact' );
// When the Page with a post_name (slug) of "about-me" is being displayed.
is_page( 'about-me' );
In your case you should be using: 在您的情况下,您应该使用:
<?php if (is_page(48857) ):?>
WordPress' is_page()
function requires a "Page ID, title, slug, or array of such.". WordPress的is_page()
函数需要“页面ID,标题, is_page()
或此类的数组”。 'page-id-48857'
is the body class, you need to just use is_page( 48857 )
since the actual ID is just 48857
. 'page-id-48857'
是主体类,您只需使用is_page( 48857 )
因为实际的ID仅为48857
。
Also note that you should seriously consider using wp_enqueue_script()
instead of coding in a custom script tag. 还要注意,您应该认真考虑使用wp_enqueue_script()
而不是在自定义脚本标记中进行编码。 It will save you countless headaches in the future. 它将为您节省未来无数的头痛。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.