Forums

[resolved] Getting the_title() and the_content() within an if..elseif (7 posts)

  1. ViPeRx007
    Member
    Posted 5 months ago #

    Alright, I'm trying to do an if elseif type statement but it needs to output div's, span's, the_title() and the_content(). Perhaps I should just post my code:

    This is just a section of it:

    <?php
             if (is_page('cleansers')) {
             echo '<div id="pagetop-cleansers"><div id="toptextContainer"><span class="toptextTitle-cleansers"> the_title(); </span><br />
      <span class="toptextBody-cleansers"> the_content(); </span>';
             } elseif (is_page('exfoliants')) {
             echo '<div id="pagetop-exfoliants"><div id="toptextContainer"><span class="toptextTitle-exfoliants"> the_title(); </span><br />
      <span class="toptextBody-exfoliants"> the_content(); ?></span>';

    This doesn't work, it just outputs the strings "the_title()" and "the_content()" and not the actual title or content. I'm not much of a programmer so I'm sort of lost.

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 5 months ago #

    <?php
             if (is_page('cleansers')) { ?>
             <div id="pagetop-cleansers"><div id="toptextContainer"><span class="toptextTitle-cleansers"> <?php the_title(); ?> </span><br />
      <span class="toptextBody-cleansers"> <?php the_content(); ?></span>
             <?php } elseif (is_page('exfoliants')) { ?>
             <div id="pagetop-exfoliants"><div id="toptextContainer"><span class="toptextTitle-exfoliants"> <?php the_title(); ?></span><br />
      <span class="toptextBody-exfoliants"> <?php the_content(); ?></span>
    
    <?php }

    I think I caugfht everything there. You told it to echo, so it was echoing! Everything inside an echo is spit out as written unless you break it up

    You have to break back into php, either as I did it here, or there is another way using periods.... which isn't my strength

    I added the closing curly brace there, to show how it fits....

  3. Rev. Voodoo
    Volunteer Moderator
    Posted 5 months ago #

    <?php
             if (is_page('cleansers')) {
             echo '<div id="pagetop-cleansers"><div id="toptextContainer"><span class="toptextTitle-cleansers">' . the_title(); . '</span><br />
      <span class="toptextBody-cleansers">' . the_content(); . '</span>';
             } elseif (is_page('exfoliants')) {
             echo '<div id="pagetop-exfoliants"><div id="toptextContainer"><span class="toptextTitle-exfoliants">' . the_title(); . '</span><br />
      <span class="toptextBody-exfoliants">' . the_content(); ?></span>';

    I think I have it right here, this is the other way to do that

  4. ViPeRx007
    Member
    Posted 5 months ago #

    Thanks for the quick reply Rev. Voodoo. Hmm, I'm not having much luck with these though. I'm getting syntax errors in Dreamweaver and a blank white page on the site itself.

  5. alchymyth
    The Sweeper
    Posted 5 months ago #

    your code was missing the closing } of the elseif; added in the version below:

    <?php
             if (is_page('cleansers')) {
             echo '<div id="pagetop-cleansers"><div id="toptextContainer"><span class="toptextTitle-cleansers">'.get_the_title().'</span><br />
      <span class="toptextBody-cleansers">'.apply_filters('the_content', get_the_content()).'</span>';
             } elseif (is_page('exfoliants')) {
             echo '<div id="pagetop-exfoliants"><div id="toptextContainer"><span class="toptextTitle-exfoliants">'.get_the_title().'</span><br />
      <span class="toptextBody-exfoliants">'.apply_filters('the_content',get_the_content()).'</span>';
         } ?>

    for use in strings, you would use get_the_title() and get_the_content()

  6. ViPeRx007
    Member
    Posted 5 months ago #

    I think I'm getting closer but now I'm getting a syntax error on line 96 (which is the very bottom line with the get_footer string. Why does php hate me so....?

    Here's the whole page code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  7. ViPeRx007
    Member
    Posted 5 months ago #

    Maybe I'm not quite as dumb as I thought. I got it working by adding:

    <?php endwhile; ?>

    between the closing div's for #post-## and #content.

    Everything is working now! Thanks everybody.

Reply

You must log in to post.

About this Topic