Sinac
Sinac

Reputation: 11694

Wordpress show the_title() if title present

What I want is the following: If a post has a title, I want to show it

In the wordpress.org codex you can see the following examples:

<?php the_title( $before, $after, $echo ); ?> 

and this example:

<?php the_title('<h3>', '</h3>'); ?>

What I am trying to do, is I add a hyperlink with the_permalink in href

<?php the_title('<h3><a href=" the_permalink(); ">', '</a></h3>'); ?>

This however does not work.

In other words: how do I write the_permalink(); in the code shown?

I also tried with:

<?php the_title('<h3><a href="' . the_permalink() . '">', '</a></h3>'); ?>

This results in:

mysite.com/?p=1

My Title

Upvotes: 2

Views: 1549

Answers (2)

The Alpha
The Alpha

Reputation: 146191

Inside the loop the_title() will echo the title. You can write it in this way

<h3><a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>

Upvotes: 0

Max Hudson
Max Hudson

Reputation: 10206

<?php $permalink = get_permalink(); the_title('<h3><a href="' . $permalink . '">', '</a></h3>'); ?>

Upvotes: 2

Related Questions