[英]Prefix article keyword with date(Y-m-d)
In the CMS I am building, record names have to be unique (they are URL keywords). 在我正在构建的CMS中,记录名称必须唯一(它们是URL关键字)。 In order to achieve this with blog posts, I am attempting to prefix blog post titles with date("Ymd") in PHP.
为了通过博客文章实现此目的,我试图在PHP中为博客文章标题加上date(“ Ymd”)前缀。
I have a "title" input text field, in which the title is entered, a "keyword" text field which automatically "slugs" the title in order to turn it into a URL keyword. 我有一个“标题”输入文本字段,在其中输入了标题,还有一个“关键字”文本字段,该字段自动“插入”标题以将其变成URL关键字。
I'm having trouble figuring out how to prefix the slugged title with the date. 我在弄清楚如何用日期加上前缀的标题时遇到了麻烦。
Here's the code: 这是代码:
<input name="title" type="text" id="title" />
<input name="keyword" type="text" id="slug" />
<script type="text/javascript">
$(document).ready(function(){
$("#title").slug();
});
</script>
This part works. 这部分有效。 The title successfully turns into a keyword with dashes for spaces, eliminating special characters, etc.
标题成功地变成了一个带有破折号,空格,消除特殊字符等的关键字。
I tried including the date by adding a hidden field with the date as the value and accessing its value with the getElementById function. 我尝试通过添加一个以日期为值的隐藏字段并使用getElementById函数访问其值来包括日期。 I attempted to rework the javascript to concatenate the slugged title with the date:
我试图对javascript进行重做,以将日期不正确的标题连接起来:
<input type = "hidden" id = "postdate" value = "<?php echo date("Y-m-d"); ?>-" />
<script type="text/javascript">
$(document).ready(function(){
var getDate = document.getElementById('postdate');
var doSlug = $("#title").val();
var slugString = getDate + doSlug;
$("slugString").slug();
});
</script>
But I'm obviously not working properly with the javascript. 但是我显然不能正常使用javascript。
The output I'm after would be: "2013-10-09-title-of-this-blog-post" 我需要的输出是:“ 2013-10-09-title-of-this-blog-post”
Where am I going wrong? 我要去哪里错了?
How about: 怎么样:
$(document).ready(function(){
var getDate = $('#postdate').val(); // get the value of the postdate id, not the element.
var doSlug = $("#title").val();
var slugString = getDate + "-" + doSlug;
$('#title').val(slugString);
$('#title').slug(); // if needed.
});
The jQuery slug() function is changing the input value to be "slugged". jQuery slug()函数将输入值更改为“ slugged”。 You can't set it to "slugString", because that's a variable, not a dom element.
您不能将其设置为“ slugString”,因为这是一个变量,而不是dom元素。 If necessary, you can "reslug" the input value after setting it with val().
如有必要,可以在用val()设置输入值后“重新分配”输入值。
Edited to fix the getDate variable issue that @Oswaldo Acauan picked up 编辑以修复@Oswaldo Acauan遇到的getDate变量问题
I suggest you to do a little tweak to the plugin to be able to do what you want. 我建议您对插件做些微调 ,以便能够执行所需的操作。 As you can see in the feedle above i add two new configuration parameters
如您在上面的反馈中看到的,我添加了两个新的配置参数
prepend: null, // String to be prepended the sluged string
append: null // String to be appended the sluged string
and on the makeSlug
function i just add this two conditionals 在
makeSlug
函数上,我只添加了这两个条件
if(typeof settings.append === 'string')
slug = slug + '-' + settings.append;
if(typeof settings.prepend === 'string')
slug = settings.prepend + '-' + slug;
Now you can prepend or append a string to the slugfy version of your string 现在您可以在字符串的slugfy版本之前添加或附加字符串
$("#title").slug({
prepend: '2010-10-13',
append: 'YAYY'
});
If you decide to use this approach, here is how to use with your html 如果您决定使用此方法,则以下是与html结合使用的方法
$(document).ready(function(){
$("#title").slug({
prepend: $('#postdate').val()
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.