1

I am trying to get a link together using 2 variables but the output is the link and title but no html / clickable link appearing.

I'm getting something link this:

http://www.mydomain.com/post1/post_title_here

Here is the code:

echo '<a href="'.the_permalink().'">'.the_title().'</a>';

Can anyone help please?

Thanks

UPDATE:

Here's the whole block of code:

<div id="MyBlock1">
        <?php
            $query = new WP_Query('posts_per_page=5');

             while( $query ->have_posts() ) : $query ->the_post();
                 echo '<li>';
                 echo '<a href="'.the_permalink().'">'.the_title().'</a>';
                 echo '</li>';
             endwhile;

            wp_reset_postdata();

        ?>
    </div>
4
  • 1
    Code looks fine to me. What is getting rendered? What is the return value of the_permalink()? Commented May 24, 2011 at 15:47
  • paste the code from the permalink function so we know what it returns Commented May 24, 2011 at 15:48
  • the_permalink() contains the link. eg: mydomain.com/post1 Commented May 24, 2011 at 15:49
  • As a general rule, if a function in Wordpress generates output of some sort, then the_function() sends the output directly to the client, while get_the_function() returns its output instead. Commented May 24, 2011 at 16:03

5 Answers 5

5

That's because the wordpress functions the_permalink() and the_title() display the respective outcomes already they need not be echoed. If you want functions that return the values, you have to use get_permalink() and get_the_title() instead.

So either do:

<div id="MyBlock1">
    <?php
        $query = new WP_Query('posts_per_page=5');
        while( $query ->have_posts() ) : $query ->the_post();
            echo '<li>';
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
            echo '</li>';
        endwhile;
        wp_reset_postdata();
   ?>
</div>

or

<div id="MyBlock1">
    <?php
        $query = new WP_Query('posts_per_page=5');
        while( $query ->have_posts() ) : $query ->the_post();
            echo '<li><a href="';
            the_permalink();
            echo '">';
            the_title();
            echo '</a></li>';
        endwhile;
        wp_reset_postdata();
   ?>
</div>

Both will work.

Sign up to request clarification or add additional context in comments.

Comments

2

Here's a checklist for debugging:

1.) Is the_title() returning an empty string? (You can check by looking at the html source)

2.) Are you echoing this inside of the body tag?

3.) Is this being echoed in a hidden html element?

Comments

2
echo '<a href="'.the_permalink().'">'.the_title().'</a>';

In this sort of situation, you'll want to use get_permalink instead of the_permalink and get_the_title instead of the_title.

echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';

WordPress the_* functions do a direct echo call, whereas the get_* functions return a value that you can use for further processing, like the concatenation you're doing.

(also note the inconsistent naming conventions - this can be a pain)

Comments

2

You could use the corresponding get_* versions:

echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';

See the codex reference for more

1 Comment

It's actually get_the_title.
0

You need to absolutely make sure that .the_title(). is definately bieng set a value. If it isn't, then there will be no displayed HTML as there is no text for the anchor tag. Just a thought (i've done it many times, try print_f(); ing the the_title(). Hope it helped.

1 Comment

Both variable are returning values.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.