在片段标识符中使用hastag和php

在片段标识符中使用hastag和php,php,wordpress,anchor,href,Php,Wordpress,Anchor,Href,我正试图构建一个导航栏,列出最新的帖子,并通过片段标识符超链接引用它们,如: <ul class="nav"> <?php $the_query = new WP_Query( 'showposts=5' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <li class=""><a href="&

我正试图构建一个导航栏,列出最新的帖子,并通过片段标识符超链接引用它们,如:

<ul class="nav">

    <?php $the_query = new WP_Query( 'showposts=5' ); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <li class=""><a href="<?php the_title(); ?> "><?php the_title(); ?></a></li>
    <?php endwhile;?>

</ul>
在我的页面下方,我有一个锚,如下所示:

<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query ->have_posts()) : $the_query -> the_post(); ?>
<section id="<?php the_title(); ?>">
<br><br><br><br><br><br>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(__('(more…)')); ?>
<?php endwhile;?>

因此,一种工作方式是:

<ul class="nav"><?php $the_query = new WP_Query( 'showposts=5' ); ?>
                                <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
                                    <li class="">
                                        <a href="#post-<?php the_id(). get_the_id() ;?>"><?php the_title(); ?></a>
                                    </li>
                                <?php endwhile;?>
                            </ul>

(从上面的评论中得到了它,但我想我应该总结一下,这样我就可以标记为解决)

“问题是,当我添加hashtag[…]”时,你在哪里这么做?我在之前添加hashtag,所以我得到href=“#”,然后什么“不起作用”以及如何不起作用?标题可能包含空格等,因此不能作为有效的
id
。我建议使用post id作为节id和散列值来链接到节(并在其前面加一个字母以确保兼容性),我称之为:对吗?。。。。当我这样做时,我得到了一个空字符串?让它工作,必须使用: