简体   繁体   English

固定链接无法在Wordpress类别下拉Ajax中按预期方式工作

[英]Permalink not working as expected in wordpress category dropdown ajax

Here's my php code: 这是我的PHP代码:

define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');
$post_type = $_GET['posts_type'];
$name = $_GET['name'];
$pageNum = $_GET['num_page'];
switch($post_type){
    case "category":
        getCategoryPosts($pageNum, $name);
        break;
}

function getCategoryPosts($p, $n){
    query_posts('posts_per_page=5&paged='.$p.'&cat='.$n);
    if(have_posts()){
        echo "<ul class='category-dropdown-posts'>";
            while(have_posts()){
                the_post();
                $permalink = get_the_permalink();
                echo "<li><a href=".$permalink.">";
                echo get_the_post_thumbnail($post->ID, array(172, 132), array('class'=>'dropdown-pic'));
                echo "<p class='dropdown-title'>".get_the_title();
                echo "</p></a></li>";
            }
        echo "</ul>";
    }
    wp_reset_query();
}

Now everything is working fine, except the permalink. 现在,除了永久链接之外,其他所有东西都工作正常。 As you can see that the a tag should be wrapped around both img and p tags, but what I get is something like this: 如您所见,a标签应该同时包裹在img和p标签周围,但是我得到的是这样的东西:

<li>
<a href="..."></a>
<img ..../>
<p>...</p>
</li>

Any idea what could be wrong? 知道有什么问题吗?

Edit: The console.log of ajax response seems to be correct, but the display is messed. 编辑:ajax响应的console.log似乎是正确的,但显示混乱。 I am using Firefox in Ubuntu in Virtual Machine. 我在虚拟机的Ubuntu中使用Firefox。 Edit 2: Here's the ajax response: 编辑2:这是ajax响应:

<ul class='category-dropdown-posts'><li><a style='display:block' href=http://localhost/wordpress/post-to-post/><img width="172" height="114" src="http://localhost/wordpress/wp-content/uploads/2014/12/post-to-post-300x199.jpg" class="dropdown-pic wp-post-image" alt="Post To Post" /><span class='dropdown-title'>Post to Post</span></a></li></ul>

I have figured out the problem. 我已经解决了问题。 As you can see the href in a is not quoted, which is causing the behaviour. 如您所见,a中的href未被引用,这导致了该行为。 Thanks for the help guys. 感谢您的帮助。

Anchor elements should not contain paragraph elements. 锚元素不应包含段落元素。 Technically this is allowed in HTML5, but some browsers may not handle it correctly. 从技术上讲,HTML5允许这样做,但是某些浏览器可能无法正确处理它。 My guess is you're viewing the source in your browser's dev tools and it is auto-closing the anchor when it encounters the paragraph. 我的猜测是您正在浏览器的开发工具中查看源代码,当遇到段落时它会自动关闭锚点。 If this is the case, viewing the actual source should show the HTML formatted as expected, even though it renders as if the anchor is closing early. 在这种情况下,查看实际源应该显示按预期格式的HTML,即使它呈现的好像锚点早早关闭一样。 Try placing the anchor inside the paragraph or setting the display attribute of the anchor to block. 尝试将锚点放置在段落中,或将锚点的显示属性设置为block。 More info: 更多信息:

Can a <p> tag in <a> tag? 可以在<a>标签中使用<p>标签吗?

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

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