WordPress.org

Forums

Minimal index.php; Is the code solid? (15 posts)

  1. Tom Kroscavage
    Member
    Posted 4 years ago #


    Dose this code look ok

    <?php  get_header(); ?>
    <div id="content">
    	<?php if (is_single() || is_category() || is_archive()) {get_sidebar();} ?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<?php if (!is_page()) { ?>
    
        <div class="post-headline">
    	<h2>
    	  <?php if( !is_single() AND !is_page() ) { ?> <a href="<?php the_permalink() ?>" >
    	  <?php } the_title(); if( !is_single() AND !is_page() ) { ?></a> <?php } ?>
    	</h2>
        </div>
    
    	<?php } ?><?php if(is_category() || is_archive()) { the_excerpt(); } else { the_content(); } ?><?php endwhile; ?>
    	<?php posts_nav_link(' - ','« Prev','Next »') ?>
    	<?php if (is_single()) {comments_template();} ?>
    	  <?php else : ?><?php endif; ?>
    </div>
    <?php   get_footer(); ?>
  2. Tom Kroscavage
    Member
    Posted 4 years ago #

    Is minimal faster?

  3. Tom Kroscavage
    Member
    Posted 4 years ago #

    I made a few changes is it safe? Do I need more "end if"s?

    <?php  get_header(); ?>
    <?php if (is_single() || is_category() || is_archive()) {get_sidebar();} ?>
     <div id="content">
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<?php if (!is_page()) { ?>
    
    	<h2><?php if( !is_single() AND !is_page() ) { ?> <a href="<?php the_permalink() ?>" ><?php } the_title(); if( !is_single() AND !is_page() ) { ?></a> <?php } ?></h2>
    
    	<?php } ?><?php if(is_category() || is_archive()) { the_excerpt(); } else { the_content(); } ?><?php endwhile; ?>
    	<?php posts_nav_link(' - ','« Prev','Next »') ?>
    	<?php if (is_single()) {comments_template();} ?>
    	  <?php else : ?>Nothing Here<?php endif; ?>
     </div>
    <?php   get_footer(); ?>
  4. Tom Kroscavage
    Member
    Posted 4 years ago #

    will this work?

  5. Stop bumping.

    If it works, it works. I don't know if it'd be significantly faster. You'd have to run some tests.

  6. Tom Kroscavage
    Member
    Posted 4 years ago #

    [Moronic expletive deleted]

  7. Sharolyn Rogers
    Member
    Posted 4 years ago #

    I would like to do something like that.

  8. Sharolyn Rogers
    Member
    Posted 4 years ago #

    It only calls comment.php if it is a single post? Tom can you comment the code for me? Can I use it?

  9. Tom Kroscavage
    Member
    Posted 4 years ago #

    Sure Sharolyn start saving your money it aint gonna be cheap. Here's the code. I guess I'm the php expert around here.

    <?php  get_header(); ?>// Call the header.php
    <?php if (is_single() || is_category() || is_archive()) {get_sidebar();} ?>// Call the sidebar if its not a page
    
     <div id="content">
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>// The print post loop
    	<?php if (!is_page()) { ?>// Pages jump out of the loop here 
    
    	<h2> <?php if( !is_single() AND !is_page() ) { ?> <a href="<?php the_permalink() ?>" > <?php } the_title(); if( !is_single() AND !is_page() ) { ?> <?php } ?></h2>//  List post title links
    
    	<?php } ?><?php if(is_category() || is_archive()) { the_excerpt(); } else { the_content(); } ?>// List the excerpts or full content for posts
    	<?php endwhile; ?>// end of loop
    	<?php posts_nav_link(' - ','« Prev','Next »') ?> // Prev Next post page links
    	<?php if (is_single()) {comments_template();} ?> // Call comments.php if it is a post
    	  <?php else : ?>Nothing Here<?php endif; ?> // bad url
     </div><--End content div-->
    <?php   get_footer(); ?>// Call the footer.php
  10. Michael
    Forum Moderator
    Posted 4 years ago #

    you can save a few bits:

    if (is_single() || is_category() || is_archive())

    category is always archive, so this does the same:

    if (is_single() || is_archive())

    same here: if(is_category() || is_archive())
    simplifies to: if(is_archive())

    ---
    the <a tag needs closing:

    <h2> <?php if( !is_single() AND !is_page() ) { ?> <a href="<?php the_permalink() ?>" > <?php } the_title(); if( !is_single() AND !is_page() ) { ?></a> <?php } ?></h2>//  List post title links

    instead of if( !is_single() AND !is_page() ) you could try the if( !is_singular() ) conditional tag in the above.

  11. Tom Kroscavage
    Member
    Posted 4 years ago #

    Thanks alchymyth it works perfect after I fixed my comments. Here it is

    Pages don't jump out of the loop where I have them marked, do they?

  12. Michael
    Forum Moderator
    Posted 4 years ago #

    if your code exeeds 10 lines, please paste the code into a http://pastebin.com/ and post the link to it here.
    a:
    makes it easier to follow the thread;
    b:
    adds line numbers, keeps the formatting, adds syntax highlighting (if ticked properly at the bottom before submitting the pastebin), makes replying with additions/changes easy, ...

    as far as i could follow your code, pages would just jump the h2 title, and are back at the beginning of this line: <?php } ?><?php if(is_category() || is_archive()) { the_excerpt ...

  13. Tom Kroscavage
    Member
    Posted 4 years ago #

    Thanks again alchmyth that helps. Here it is in the paste bin
    index.php

    So if its a page this <?php if (!is_page()) { ?>
    jumps to this curly bracket? <?php } ?>

  14. Michael
    Forum Moderator
    Posted 4 years ago #

    So if its a page this jumps to this curly bracket?

    yes

    it is quite diffficult to think ahead for every possible situation - that is where testing and debugging comes in.

    for instance, looking at your template again, it might be useful to rewrite this line:
    <?php if (is_single() || is_archive()) {get_sidebar();} ?><!--Call the sidebar if its not a page-->
    into what the comment actually says:
    <?php if (!is_page()) {get_sidebar();} ?><!--Call the sidebar if its not a page-->
    otherwise the sidebar might not get added on the home page for instance, or on attachment pages ...

  15. Tom Kroscavage
    Member
    Posted 4 years ago #

    Dude! I like it.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.