【问题标题】:Use hastag and php in fragment identifier在片段标识符中使用 hashtag 和 php
【发布时间】:2014-02-16 07:51:28
【问题描述】:

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

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

问题是当我在&lt;?php the_title(); ?&gt; 之前添加hastag 时,事情停止工作..

【问题讨论】:

  • “问题是当我添加主题标签 […]” – 你在哪里做的?
  • 我在 所以我得到 href="#"
  • 然后什么是“不工作”以及如何不工作?标题可能包含空格等,因此不构成有效的id。我建议使用帖子 id 作为部分 id 和哈希值来链接到该部分(并在其前面加上一个字母以保持兼容性)
  • 我称之为: 对吗?....我这样做时得到一个空字符串?
  • 得到它的工作,不得不使用:post->ID; echo $this_page_id ;?>

标签: php wordpress anchor href


【解决方案1】:

所以一种工作方式是:

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

(从上面的 cmets 得到它,但我想我会总结一下,所以我可以标记为解决)

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2011-05-11
    相关资源
    最近更新 更多