Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

php - Wordpress the_permalink() closes tag?

Hellow, im trying to code a custom theme for my wordpress site. im trying to output 5 of my posts and i want to wrap them into a link. however when i foreach() through the posts and want to wrap my html in an tag with the permalink it wont work.

The code i wrote:

<?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();
    }
    ?>

But the code which ends up on the site is complete junk and it closes the a tag before the next div. what did i do wrong? code on the site

The code which lands on the site:

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

WordPress' the_permalink() function prints the link with the tags.

Use get_the_permalink() function instead of the_permalink() for "echo"ing the link wherever needed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...