Search code examples
phpwordpressanchorhref

Use hastag and php in fragment identifier


I am trying to build a navbar that that list newest post and references to them via fragment identifier hyper link, as in:

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

Further down on my page I have an anchor as in:

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

Problem is that when I add hastag before <?php the_title(); ?> things stop working..


Solution

  • So a way that work is:

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

    (got it from the comments above but figured I'd summarise so I can mark as solve)