Help with Php code in Theme

Discussion in 'PHP' started by wowcast, Aug 18, 2012.

  1. #1
    Hey guys! I have following problem with my website: www.fitmanic.de
    If you go to there, you will see (under the slider) a picture with exerpt "Sich richtig Ernähren" aka. The apple picture.
    This place is getting renewed, when i post a post. So it takes the newest post. My problem is following: if you check out the theme HP: http://wordpress.youjoomla.net/youfitness/ you will see, they have under the last post (i guess), 2 coloums of news/articles/pics. I found this part of code in my theme:

     <!--START 2COLUMN NEWS -->
          <div class="box">
            <?php $hol = 1; ?>
            <?php
                    $args=array(
                            'category_name' => $yf_feat_cat,
                            'post__not_in'  => $sticky,
                            'paged'=> $paged
                    );
                $editorials = new WP_Query($args);
                    if($editorials->have_posts()) : while($editorials->have_posts()) : $editorials->the_post();
            ?>
            <?php if ($hol == 1) echo "<div class=\"row\">"; ?>
            <div class="post hol<?php echo $hol;?>" id="post-<?php the_ID(); ?>">
              <div class="yjsquare_news" >
                <div class="yjsquare_out">
                  <div class="h3w">
                    <div class="contentheading">
                      <h2><a href="<?php the_permalink() ?>">
                        <?php the_title(); ?>
                        </a></h2>
                    </div>
                  </div>
                  <div class="yjsquare_in"> <a href="<?php the_permalink() ?>"><img class="border" src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" border="0" alt="" style="width:355px;height:129px;"/></a>
                    <p>
                      <?php trim_content2(); ?>
                    </p>
                   <a class="readon" href="<?php the_permalink() ?>"><span>Lesen Sie weiter...</span></a> </div>
                </div>
              </div>
              <?php if ($hol == 1) echo "</div>";
                     (($hol==1) ? $hol=2 : $hol=1); ?>
            </div>
            <?php endwhile; ?>
            <?php endif; ?>
          </div>
    Code (markup):
    Sadly, i am not a php pro, and i dont know what or how to manage/add some Posts/News on the 2 Column News area!

    If you requiere more infos or want to help me out, you can ask here, or add me in skype: champ_alex

    Thank you verry much for taking your time for me!
     
    wowcast, Aug 18, 2012 IP
  2. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #2
    Who wrote that? It's a mess. Why on earth does it have PHP mixed with HTML and PHP placed in needless open/close PHP tags?

    I've never seen someone use the end conditional statement tokens "endwhile" and "endif" in PHP. Why do this over curly braces?

    You need to be more specific, what you're asking doesn't even sound like a PHP issue, it sounds like a front-end layout question.

    By the way the use of CSS in that soup is diabolical. CSS stands for cascading stylesheets you shouldn't name all nested tags, especially with such un-semantic class names.

    I took the liberty to first clear up the most pressing silliness:

    
    <div class="box">
    
    <?php
    
         $hol = 1;
         $args = array('category_name' => $yf_feat_cat, 'post__not_in'  => $sticky, 'paged'=> $paged);
         $editorials = new WP_Query($args);
         if($editorials->have_posts()) : while($editorials->have_posts()) : $editorials->the_post();
         if ($hol == 1) echo "<div class=\"row\">";
    
    ?>
    
    <div class="post hol<?php echo $hol;?>" id="post-<?php the_ID(); ?>">
         <div class="yjsquare_news" >
              <div class="yjsquare_out">
                   <div class="h3w">
                        <div class="contentheading">
                        <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
                        </div>
                   </div>
                   <div class="yjsquare_in">
                        <a href="<?php the_permalink() ?>"><img class="border" src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" border="0" alt="" style="width:355px;height:129px;"/></a>
                        <p><?php trim_content2(); ?></p>
                        <a class="readon" href="<?php the_permalink() ?>"><span>Lesen Sie weiter...</span></a>
                   </div>
              </div>
         </div>
    
         <?php if ($hol == 1) echo "</div>"; (($hol==1) ? $hol=2 : $hol=1); ?>
    
    </div>
    
    <?php endwhile; endif; ?>
    
    </div>
    
    PHP:
    Next I advise that you separate the HTML and PHP by replacing the dynamic content of the HTML document with tokens like {%POST_ID%} and {%POST_TITLE%} and then using str_replace() to replace these tokens with the appropriate data. It is proper practice and structure to do this. All languages should be separated. CSS placed in a stylesheet, the HTML placed in its own file and then the PHP scripts on their own.

    Here's an example:

    (This is actually HTML5, not HTML4.1 strict)
    
    <!DOCTYPE HTML>
    <html>
    <!-- This is example.html referenced in the PHP script -->
    <head>
         <meta charset='ISO-8859-1'>
         <title>{%POST_TITLE%}</title>
         <link rel='stylesheet' href='style.css' type='text/css'>
    </head>
    
    <body>
         <h1>{%POST_TITLE%}</h1>
         <div>
              {%POST_CONTENT%}
         </div>
         <p><a title='{%POST_TITLE%} permalink' href='http://www.yoursite.com/posts/?id={%POST_ID%}'>This post's permalink</a></p>
    </body>
    
    </html>
    
    HTML:
    
    <?php
    
    // These token replacement vars would be taken from the DB
    // These are set for example purposes
    $postId = 1;
    $postTitle = "The post of the day";
    $postContent = "Hello. This is the post of the day. Please use the permalink below to reference this in future";
    
    $template = file_get_contents('example.html');
    $tokens = array('{%POST_ID%}','{%POST_TITLE%}','{%POST_CONTENT%}');
    $replace = array($postId, $postTitle, $postContent);
    print str_replace($tokens, $replace, $template);
    
    ?>
    
    PHP:
    The final result:

    
    <!DOCTYPE HTML>
    <html>
    <!-- This is example.html referenced in the PHP script -->
    <head>
         <meta charset='ISO-8859-1'>
         <title>The post of the day</title>
         <link rel='stylesheet' href='style.css' type='text/css'>
    </head>
    
    <body>
         <h1>The post of the day</h1>
         <div>
              Hello. This is the post of the day. Please use the permalink below to reference this in future
         </div>
         <p><a title='The post of the day permalink' href='http://www.yoursite.com/posts/?id=1'>This post's permalink</a></p>
    </body>
    
    </html>
    
    HTML:
    See how much neater and manageable this is? This is so important it should be a sticky. Proper layering of languages is the foundation of correct programming.
     
    Last edited: Aug 18, 2012
    BRUm, Aug 18, 2012 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    You mustn't have done a lot of theming, because it's more or less convention. When you're dealing with both PHP and HTML, it's a lot easier to keep track of endifs/endwhiles etc. than single character curly braces.

    By convention, you shouldn't mix PHP and HTML where it can be avoided. However, with theming, unless you opt to use a template engine (which I would just argue add's an unnecessary layer of complexity for no real gain), you're left with no choice for your theme files (and any deliberate ploy to avoid doing so would be a worse solution). The solution you proposed in the second half of your post is essentially a simple template engine, and as someone with quite a bit of industry experience, I can assure you that it's not proper practice. In fact, most well constructed MVC systems simply use .php files as their view files - the important distinction is that all logic is done prior to loading the view.
     
    Alex Roxon, Aug 20, 2012 IP
  4. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #4
    If the above is an example of theming I want nothing to do with it. Being a mess is the last thing wrong with it.

    The solution would be to use a decent service/framework in the first place to avoid the potential of having to clear up or circumvent someone else's poor design.

    How does my example not process logic before rendering? You're not the only one with industry experience. You missed the point of the post. Separating and layering of languages like I said, is proper practice. My example is just one solution. I never claimed you couldn't use PHP files instead of HTML.

    Did you miss this part?

    If you use even the most basic IDE with syntax highlighting there's no need for end tokens. I don't agree with you at all that they're easier to keep track of. That's purely subjective.

    If you think templating engines add no gain you've obviously never worked on large projects.

    "I pity the fool" who blindly obeys "conventions". You should look up the requirements for development employees at google and MS, they're all about finding that special person who stands out, questioning "conventions" to evolve and push the forefront of SW engineering. You have the mindset of a cubicle worker.
     
    Last edited: Aug 20, 2012
    BRUm, Aug 20, 2012 IP
  5. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #5
    I've worked on a lot of large projects, and I've never been in a situation where I felt a 3rd party templating engine would be more advantageous than a frameworks default templating system. Ideally, the view file should just be used for simple iterating and rendering and nothing more, so why complicate it? However, I'm a fairly open minded guy. Feel free to try and convince me otherwise.
     
    Alex Roxon, Aug 20, 2012 IP
  6. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #6
    Fair enough, but where did you get third party idea from? I'm quite a purist. I won't even use jQuery nevermind a cumbersome 3rd party templating library. I agree with you. The extent of my templating usage goes as far as the example above; simple, light but effective. I prefer to understand everything I use so I build my own libraries, avoid frameworks and use asynch. JavaScript over the "fancy" libraries out there.
     
    BRUm, Aug 20, 2012 IP
  7. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #7
    Fair enough, but where did you get third party idea from? I'm quite a purist. I won't even use jQuery nevermind a cumbersome 3rd party templating library. The extent of my templating usage goes as far as the example above. Simple, light but effective. I prefer to understand everything I use so I build my own libraries, avoid frameworks and use asynch. JavaScript over the "fancy" libraries out there.
     
    BRUm, Aug 20, 2012 IP
  8. wowcast

    wowcast Greenhorn

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    ..and back to my problem :D

    So how should i add posts under <!--START 2COLUMN NEWS -->. Where do ou think i should add them or what attributes do they need to posses?
     
    wowcast, Aug 25, 2012 IP
  9. wowcast

    wowcast Greenhorn

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    any suggestions? Please call me on skype: champ_alex and maybe we can figure it out together :S
     
    wowcast, Aug 31, 2012 IP
  10. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #10
    I think your question is not only too broad but it's asking a lot from a volunteer. You're better off understanding as much of the problem as you can and breaking your questions down.
     
    BRUm, Aug 31, 2012 IP
  11. wowcast

    wowcast Greenhorn

    Messages:
    89
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #11
    Well i dont know what attribute i need to add to the post; so i can add freely posts under the <!--START 2COLUMN NEWS -->

    :X I hope you understand my problem :)

    if not, like said, i would be happy to talk with you over skype


    More exactly: How does this Code figure out which post needs to be featured?

     <!--START 2COLUMN NEWS -->
          <div class="box">
            <?php $hol = 1; ?>
            <?php
                    $args=array(
                            'category_name' => $yf_feat_cat,
                            'post__not_in'  => $sticky,
                            'paged'=> $paged
                    );
                $editorials = new WP_Query($args);
                    if($editorials->have_posts()) : while($editorials->have_posts()) : $editorials->the_post();
            ?>
            <?php if ($hol == 1) echo "<div class=\"row\">"; ?>
            <div class="post hol<?php echo $hol;?>" id="post-<?php the_ID(); ?>">
              <div class="yjsquare_news" >
                <div class="yjsquare_out">
                  <div class="h3w">
                    <div class="contentheading">
                      <h2><a href="<?php the_permalink() ?>">
                        <?php the_title(); ?>
                        </a></h2>
                    </div>
                  </div>
                  <div class="yjsquare_in"> <a href="<?php the_permalink() ?>"><img class="border" src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" border="0" alt="" style="width:355px;height:129px;"/></a>
                    <p>
                      <?php trim_content2(); ?>
                    </p>
                   <a class="readon" href="<?php the_permalink() ?>"><span>Lesen Sie weiter...</span></a> </div>
                </div>
              </div>
              <?php if ($hol == 1) echo "</div>";
                     (($hol==1) ? $hol=2 : $hol=1); ?>
            </div>
            <?php endwhile; ?>
            <?php endif; ?>
          </div>
    Code (markup):
     
    wowcast, Sep 3, 2012 IP