WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have set the blog to be a different page other than the home page.

I want to have a link from single.php to this blog page.

Is there any function that pulls out URL for the blog ?

share|improve this question
up vote 24 down vote accepted

To build on Sagive's answer, you'll want to wrap the ID in get_permalink() to get the actual link.

<?php echo '<a href="' . get_permalink( get_option( 'page_for_posts' ) ) . '">Our Blog</a>'; ?>
share|improve this answer
Why make it so difficult? This makes more sense: <a href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>">Our Blog</a> – user2019515 Jan 18 '15 at 4:10

You can use get_option of page_for_posts to get the page ID to either assign it to a variable or to echo it if you wish to do so.

<?php $postsPageId = get_option('page_for_posts'); ?>
<a href="index.php?p=<?php echo $postsPageId; ?>">Our Blog</a>

For additional information of the defualt get_option visit: Option Reference

share|improve this answer

Best way to check the option before setting the permalink is as follows:

if ( get_option( 'page_for_posts' ) ) {
   echo '<a href="'.esc_url(get_permalink( get_option( 'page_for_posts' ) )).'">'.esc_html__( 'Blog', 'textdomain' ).'</a>';
} else {
   echo '<a href="'.esc_url( home_url( '/' ) ).'">'.esc_html__( 'Blog', 'textdomain' ).'</a>';
share|improve this answer

Agree with the Hugh Man that it is better to check the option before echoing the link, but it is possible to set the static page as a front page and leave the posts page empty. In this case, the link will just point to the home URL. A better approach is to provide a fallback to the posts archive page. Something like this:

function slug_all_posts_link() {
    if ( 'page' == get_option( 'show_on_front' ) ) {
        if ( get_option( 'page_for_posts' ) ) {
            echo esc_url( get_permalink( get_option( 'page_for_posts' ) ) );
        } else {
            echo esc_url( home_url( '/?post_type=post' ) );
    } else {
        echo esc_url( home_url( '/' ) );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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