【问题标题】:WordPress get post id passed in permalink?WordPress获取永久链接中传递的帖子ID?
【发布时间】:2014-04-08 15:30:08
【问题描述】:

如何获取在永久链接中传递的帖子 ID。在wordpress中,我看到了这个:

href="<?php echo get_permalink($post->ID); ?>"

使用JQuery get 方法我可以将href 属性传递给php 中的进程。代码是:

<script type="text/javascript">
        $(document).ready(function(e) {
            $('Selector').on('click', function(){
                var href = $(this).attr('href');
                $.get('url',
                {
                    'post_id' : href    
                }, 
                function(data){
                        alert(data);    
                });
                return false;
            });
        });
    </script>

它会根据需要给我发布永久链接,但我如何获得$post-&gt;ID。我在获得永久链接后使用了url_to_postid 方法,但这不起作用,所以如果可能的话,我想获得$post-&gt;ID。谢谢你的帮助

【问题讨论】:

  • 我在获得永久链接后使用了url_to_postid 方法,但它不起作用:您收到错误消息了吗?
  • 我得到 0,WordPress codex 也提到过,失败时你会得到 0
  • get_permalink 在循环内?
  • 我看不到任何 id 属性(当您在 jquery 中使用时)请发布您的完整代码。
  • 对不起,我现在已经编辑了我的问题,我还是得到了答案,谢谢你的帮助

标签: php jquery wordpress get


【解决方案1】:

将帖子ID附加到数据属性中的链接:

<a href="<?php echo get_permalink($post->ID); ?>"
   data-id="<?php echo htmlspecialchars($post->ID); ?>"
>...</a>

然后在jQuery中检索它:

var postID = $(this).data('id');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 2015-08-21
    • 2012-02-22
    • 2012-09-11
    • 2012-07-01
    • 1970-01-01
    相关资源
    最近更新 更多