Wordpress the_permalink() 关闭标签?

Wordpress the_permalink() closes tag?

你好, 我正在尝试为我的 wordpress 网站编写自定义主题。我正在尝试输出我的 5 个帖子,我想将它们包装成 link。然而,当我通过帖子进行 foreach() 并想将我的 html 包装在带有 permalink 的标签中时,它不会工作。

我写的代码:

<?php
    global $post;
 
    $myposts = get_posts( array(
        'posts_per_page' => 5,
    ) );
 
    if ( $myposts ) {
        foreach ( $myposts as $post ) : 
            setup_postdata( $post ); ?>

            
                <div class="container-fluid">
                    <a class="post-link" href="<?php echo the_permalink();?>">
                        <div class="post-post p-5">
                            <div class="row">
                                <div class="col-md-12">
                                    <?php the_category('<p></p>'); ?>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <h3><?php the_title(); ?></h3>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <small><?php the_author(); ?></small>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
            

        <?php
        endforeach;
        wp_reset_postdata();
    }
    ?>

但是网站上的代码完全是垃圾,它会在下一个 div 之前关闭 a 标签。我做错了什么?

登陆本站的代码:

<div class="container-fluid">
    <a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
    </a>
    <div class="post-post p-5">
        <a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
        </a>
        <div class="row">
            <a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
            </a>    
            <div class="col-md-12">
            <a class="post-link" href="http://localhost/wptheme/index.php/2021/01/11/oeoeoeoeoeoe/">
            </a>
            <a href="http://localhost/wptheme/index.php/category/allgemein/" rel="category tag">Allgemein</a>                                </div>
        </div>
            <div class="row">
                <div class="col-md-12">
                    <h3>öööööö</h3>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <small>root</small>
                </div>
            </div>
    </div>
    
</div>

看起来你正试图将一个 anker 放在一个 anker 中,这是不允许的,你的 html 的输出很可能被你的浏览器解析/解释尝试查看源代码而不是 html 被浏览器解析后。在 chrome 中,您可以通过访问查看源代码来执行此操作:http://yourdomain.com

不确定其他浏览器,您可以尝试右键单击页面并select查看源代码

WordPress 的 the_permalink() 函数打印带有标签的 link。

使用 get_the_permalink() 函数而不是 the_permalink() 在需要的地方“回显”link。