1

I have made wp_query like below. This is some part of them. And it works.

$args = .....;
$query = new WP_Query($args);
while($query->have_posts() ) :
    $query->the_post();
    ?>
        <div class="each">
            <h4><?php the_title(); ?> </h4>
        </div>
<?php endwhile; wp_reset_query(); ?>

This shows like this.

<div>title 1</div>
<div>title 2</div>
<div>title 3</div>
...

At this point, when I click one of the titles, it needs to link to the details of the clicked post. But I don''t know how to link to the detail page. I think it should be linked to 'single.php'. is this right? and how to link to that detail page(maybe singles.php)? thank you.

1
  • 1
    You don't link to templates. Use the_permalink() to dynamically output the correct URL for the post/page. When rending that post/page it will choose the template according to the template hierarchy. Commented Jan 2, 2018 at 8:17

2 Answers 2

1

Replace with below code :

<?php
$args = .....;
$query = new WP_Query($args);
while($query->have_posts() ) :
$query->the_post();
    ?>
        <div class="each">
            <h4><a href="<?php the_permalink();?>" title="<?php the_title_attribute();?>"><?php the_title();?></a></h4>
        </div>
<?php endwhile; wp_reset_query(); ?>
1

Your Code:

<h4><?php the_title(); ?> </h4>

Replace With this:

    <?php 
     the_title( sprintf( '<h4 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h4>' ); 
    ?>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.