• Resolved tollboy

    (@tollboy)


    OK so I simply want it to use the first template path if the post is by a certain author and the other template path if it is by another author. This is the mess I came up with.

    <?php get_header(); ?>
    
    <?php if ('author=3'): ?>
    
    <?php include (TEMPLATEPATH . '/nitSingle.php'); ?>
    
    <?php else : ?>
    
    <?php include (TEMPLATEPATH . '/patSingle.php'); ?>
    
    <?php endif; ?>
    <?php get_footer(); ?>

    How can I make this work? Or can this even work? This is my single.php file I was wanting to do 2 different single post files so one would load with one author and the other with the second author.

Viewing 15 replies - 1 through 15 (of 17 total)
  • Your ‘if’ is wrong. It should be <?php if (is_author('3')) : ?>. Your available conditionals are here, including some other ways to use the is_author function.

    Thread Starter tollboy

    (@tollboy)

    Thanks I was almost sure it was wrong as I have never written php before.

    At any rate I do not doubt your version is wrong, but when I use it I get a 404 page. The solution I am trying may not be an option at all. Is this something that can even work the way I am trying? Also would the “if” mess with the other “if”s in the nitSingle.php file causing this not to work properly as well? I am really thankful for any help I can get here or even a link to some info I can read.

    Thread Starter tollboy

    (@tollboy)

    I take it back this is working kind of. It is not recognizing the if statement as true though so it always follow the second templatepath. i need to have it recognize the correct author for the if statement.

    Did you read the codex page I pointed to? Because there are other ways besides ID number to ID authors.

    Thread Starter tollboy

    (@tollboy)

    Yes I did notice your link after I left my responses. I am working on trying some now, I still have not got it to work properly but at least I have a lead on how to go about doing it. If and when I find the solution I will post it.

    Thread Starter tollboy

    (@tollboy)

    I tried, for several hours now. I am loosing my mind though and need to step away. This is all I am lacking on my site so that is why I am being so relentless. Anyway here is where I am if someone wants to help me until I am able to pic it back up.

    <?php get_header(); ?>
    <?php
    if (is_author(3)) {
       include (TEMPLATEPATH . '/nitSingle.php');
    } else {
       include (TEMPLATEPATH . '/patSingle.php');
    }
    ?>
    <?php get_footer(); ?>

    OK. This isn’t going to work. The is_author() conditional is intended for use with author templates. I just realized that you are doing something different. Instead of having two single.php pages you need to have only one single and toggle the formatting– include different format files– inside the loop in single.php according which page author wrote the post being displayed. To do that you’d switch on $post->post_author inside the Loop.

    Thread Starter tollboy

    (@tollboy)

    So you are saying that i can switch between the two template paths (these would be the 2 formats), but the method I am trying with “is_author” will not work? I did see that it was meant for an archive page in the link or a related link from where you sent me.
    OK so I will try something with the $post->post_author, is it possible you could give me a quick example or a link to an example of this. This is the first theme i have created completely by myself and even though I am hitting walls i am really learning a ton. Thanks again for any help you can give me.

    I imagine something like the following inside your single.php (or whatever file handles your single posts displays):

    <?php /*
    Template Name: SinglePosts
    */
    get_header();
    if (have_posts()) {
      while (have_posts()) {
        if ('3' == $post->post_author) {
         include (TEMPLATEPATH . '/format_for_author_3.php');
        } else {
         include (TEMPLATEPATH . '/format_for_other_author.php');
        }
      }
    }
    get_footer();
    ?>

    Bear in mind that this is greatly simplified. The point is illustrate how you would switch formatting inside the Loop. Your included files should have only the code that formats the posts, nothing else. As per your original example, I’ve written this so that the header and footer are the same for both authors. Only the display of the posts is different.

    Shouldn’t make a difference, but try this…

    <?php
    if(is_author('X')) { include bloginfo('template_url').'/nitSingle.php' } else { include bloginfo('template_url').'/patSingle.php'; }
    ?>

    Where ‘X’ is the ID of the author..

    You could just test the script first doing something like this…

    <?php
    if(is_author('X')) { echo 'this is author X'; }
    else { echo 'this is not author X'; }
    ?>
    Thread Starter tollboy

    (@tollboy)

    apljdi you are the man/woman!!!
    that was what i needed exactly. As far as i can tell it is working perfectly now. Feel free to check it out at.
    https://nitpat.com
    Thanks a ton! seriously.
    here is the final code I used.

    <?php /*
    Template Name: Single Post
    */
    get_header();
    if (have_posts()) {
      while (have_posts()) {
        if ('3' == $post->post_author) {
         include (TEMPLATEPATH . '/nitSingle.php');
        } else {
         include (TEMPLATEPATH . '/patSingle.php');
        }
      }
    }
    get_footer();
    ?>

    t31os thanks for jumping in as well, but as they say if it isn’t broke don’ fix it. But i have made note of your suggestion as well. Thank you both.

    Thread Starter tollboy

    (@tollboy)

    hehe spoke a little soon, page keeps repeating over and over, I might be able to make this work still though

    Thread Starter tollboy

    (@tollboy)

    here it is working, i think

    <?php /*
    Template Name: Single Post
    */
    get_header();
        if ('3' == $post->post_author) {
         include (TEMPLATEPATH . '/nitSingle.php');
        } else {
         include (TEMPLATEPATH . '/patSingle.php');
        }
    get_footer();
    
    ?>

    What is in the nitSingle.php and patSingle.php files?

    Thread Starter tollboy

    (@tollboy)

    here is the patsingle.php file. The nitSingle is pretty much the same with some specific classes switched out, and the floats flip flopped.

    <div class="headerPostPat"></div>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="patrickPost">
    
         <div class="patrickPostTop"></div>
         <div class="patrickPostMid">
    
    <div id="dateBrown">
    <div class="dateMonth"><?php echo get_the_time(__('M')) ?></div>
    <div class="dateDay"><?php echo get_the_time(__('d')) ?></div>
    <div class="dateYear"><?php echo get_the_time(__('Y')) ?></div>
    </div>
    
         <div class="post" id="post-<?php the_ID(); ?>">
         <h1 class="article_title"><a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h1>
         <div class="entry">
         <?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
         <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
         </div>
         </div>
         </div>
         <div class="patrickPostBot"></div>	
    
    	<?php comments_template(); ?>
    	<?php endwhile; else: ?>
    		<p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>
    </div>
    
    <div class="sideRight">
    <div id="aboveSidebar">About Us</div>
    <?php get_sidebar(); ?>
    </div>
    </div></div></div>
Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘a simple if statement, but I am no php writer at all’ is closed to new replies.