Forums

[resolved] Help getting the page title into a variable. (8 posts)

  1. N3RDemon
    Member
    Posted 2 years ago #

    Hey, I've been working on this for hours and can't figure it out. I'd like to use the page title in a selection statement in order to display the sidebar only on the blog page. So I have:

    $theTitle = wp_title('',false,'');

    if ($theTitle == "Blog") get_sidebar();

    Now when I do:

    echo $theTitle;

    I get "Blog". But for some reason, the if statement doesn't run. So then I tried adding this before the if statement and it ran:

    $theTitle = "Blog";

    So wp_title() must not be returning what I think it's returning. Can anybody clue me in? Maybe I think I'm missing something.

    Thanks

  2. stvwlf
    Member
    Posted 2 years ago #

    hi

    try this, which assumes the page slug (url) of Blog is 'blog'

    if (is_page('blog')) {
       get_sidebar();
    }
  3. N3RDemon
    Member
    Posted 2 years ago #

    Ok, so I tried using is_page() and setting up the pages to use slugs and still nothing is happening. The url looks like http://domainName/blog/ and I have an if statement at the bottom of index.php. This is what the last lines look like:

    <?php if (is_page('blog')) : ?>
    <div class="span-6 last">
    <?php get_sidebar(); ?>
    </div>
    <?php endif; ?>
    
    <?php get_footer(); ?>

    I've also checked for validation errors and W3C says everything's good, so I'm not sure what the problem is. Looking at other threads, it looks like is_page() is very temperamental. I've tried adding wp_reset_query() to the line above the if statement and still no go.

  4. stvwlf
    Member
    Posted 2 years ago #

    Is the blog page your homepage or static homepage by any chance?

    If it is you must use if (is_front_page() ) instead of if (is_page('blog'))

  5. N3RDemon
    Member
    Posted 2 years ago #

    Yeah, no it's a static page. At least, I think it is. This is how I set it up. I went to 'Settings > Reading' and selected 'Front page displays a static page'. And then I selected the 'About' page to be my home page and an empty page titled 'Blog' to be my blog page.

  6. MichaelH
    Volunteer
    Posted 2 years ago #

    I went to 'Settings > Reading' and selected 'Front page displays a static page'. And then I selected the 'About' page to be my home page and an empty page titled 'Blog' to be my blog page.

    According to the Conditional Tags article/a> you would need to use is_home() for that.

  7. alchymyth
    The Sweeper
    Posted 2 years ago #

    your first attempt works if you put two spaces infront of the "Blog" i.e. "..Blog" with the dots representing the spaces.

    $theTitle = wp_title('',false,'');
    
    if ($theTitle == "  Blog") get_sidebar();

    (I echoed '$theTitle' surrounded left and right with another character, so i could see if anything was around it.)

  8. N3RDemon
    Member
    Posted 2 years ago #

    your first attempt works if you put two spaces infront of the "Blog" i.e. "..Blog" with the dots representing the spaces.

    That worked! Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic