• Resolved n3rdemon

    (@n3rdemon)


    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • hi

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

    if (is_page('blog')) {
       get_sidebar();
    }
    Thread Starter n3rdemon

    (@n3rdemon)

    Ok, so I tried using is_page() and setting up the pages to use slugs and still nothing is happening. The url looks like https://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.

    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'))

    Thread Starter n3rdemon

    (@n3rdemon)

    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.

    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.

    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.)

    Thread Starter n3rdemon

    (@n3rdemon)

    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!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Help getting the page title into a variable.’ is closed to new replies.