SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Template or hard code?

    hi, i am working on a wordpress site and i have couple of images that i want in background, i got like 15 pages and each page will have different background. i wanted to ask what will be the best approach? make a template with a div and hard code image url or make 15 template which seems insane and put images by hard code.
    All those who wander aren't lost.

  2. #2
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    What determines what image is shown? Category? Author? Random? etc
    Ryan B | My Blog | Twitter

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Author/Admin
    All those who wander aren't lost.

  4. #4
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Some may not like this, but whatever. In styles.css, I would make styles for each image:
    Code CSS:
    #authName {
     background: url();
    }
    #authName2 {...}
    Replace name with something you can recognize.

    Now in header.php
    PHP Code:
    <body
     <?php
       
    if(is_author(xxx){
       echo 
    "id=\"authName\""
       
    }
      
    // continue through authors
     
    ?>
    >
    For reference: http://codex.wordpress.org/Function_Reference/is_author
    Last edited by rguy84; Jan 11, 2012 at 09:32. Reason: add reference
    Ryan B | My Blog | Twitter

  5. #5
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    how about using custom fields?
    All those who wander aren't lost.

  6. #6
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Custom fields wouldn't let you swap backgrounds to my knowledge.
    Ryan B | My Blog | Twitter

  7. #7
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Custom fields hold text, not links. You would need to do major tweaking to the theme to make it work...
    Ryan B | My Blog | Twitter

  8. #8
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i guess custom field will work. a question though, how do i set a path to a image in custom field, which r uploaded in wp-content/uploads?
    All those who wander aren't lost.

  9. #9
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am having issues, i have copied a template but every template shows this on top of heading and also posted by abc, in the template code, what should i remove to get rid of this?
    All those who wander aren't lost.

  10. #10
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You need to edit a few files. I found: http://codex.wordpress.org/Using_Cus..._a_post_easily. Note, using custom fields to do this, you will need to point to the file every post. My way is automatic
    Ryan B | My Blog | Twitter

  11. #11
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nofel View Post
    I am having issues, i have copied a template but every template shows this on top of heading and also posted by abc, in the template code, what should i remove to get rid of this?
    I would need to see code to even take a guess.
    Ryan B | My Blog | Twitter

  12. #12
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, it served me the purpose i was looking for, i just had to put the url of every image in my custom field, i m not getting into much of php coz i am not a php guru :P but thanks, i got one issue on every page i get search bar before the title like this and i am guessing it has something to do with get_template_part? how can i get rid of it and the 'posted by' from the code
    All those who wander aren't lost.

  13. #13
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Again, I need to see both the php code and the rendered code to be able to help. You're saying why does my car not start, however you won't pop the hood,
    Ryan B | My Blog | Twitter

  14. #14
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hahah, nice example you gave. i made it work by using the_content() and get_the_title(); however the permalinks are gone here the template code
    Code PHP:
    <?php
    /*
        Template Name:Backgound images
    */
    get_header(); the_post();?>
            <div id="container">
                <div id="content" role="main" style="margin-top:10px;">
     
                <?php
                /* Run the loop to output the posts.
                 * If you want to overload this in a child theme then include a file
                 * called loop-index.php and that will be used instead.
                 */
                 //get_template_part( 'loop', 'index' );
     
                 echo "<h2 class=\"entry-title\"><a rel=\"bookmark\">".get_the_title(). "</a></h2>";
                 the_content();
                ?>
                </div><!-- #content -->
            </div><!-- #container -->
     
    <?php get_sidebar();?>
    <div id="photo">
        <img src="<?php echo get_post_meta($post->ID,'bg_images',true); ?>" />
    </div>
    <?php get_footer(); ?>
    and the link for live site
    All those who wander aren't lost.

  15. #15
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Just wanted to know, if its possible to use template in post? because i don't see any template drop down, whereas i got template which is showing up in pages
    All those who wander aren't lost.

  16. #16
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    311
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nofel View Post
    Just wanted to know, if its possible to use template in post? because i don't see any template drop down, whereas i got template which is showing up in pages
    that's not possible by default. There are plugins available that enable templates for posts. Or you can create category / taxonomy based layouts.
    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  17. #17
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah ok. i will look into it but why doesn't post have templates? without plugin
    All those who wander aren't lost.

  18. #18
    SitePoint Addict
    Join Date
    Oct 2008
    Location
    Virtual World is my location
    Posts
    311
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well thats because its the way posts are supposed to be. Usually page templates serve a purpose like if you want a specific post with some specific design. In case of posts its a group of different content. Hence say wordpress allows you to say create a category template. If you want to have all posts with say Category - Featured as a different layout you could create that using category templates.
    RecipesNext.com - recipes, kitchen queries, videos & more...
    BargainNext.com - coupons / deals / bargains / offers & more...

  19. #19
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    The piece of code that you posted is actually a template. A bare-bones level of how everything works is, you start a new post, when you hit publish, and go to your homepage, index.php is called. Based on what you specify in you settings, it grabs the last x posts. When you click on a post title, single.php is called.

    Side note: My code above is not 100% correct, my apologies. The correct code is:
    PHP Code:
    <body 
     <?php
       
    if(is_single):
       if(
    is_author(xxx){ 
       echo 
    "id=\"authName\"" 
       

      
    // continue through authors 
      
    endif;
     
    ?> 
    >
    The orginal code would change the background based on the latest author, so this included the main page of the blog. In your code block you posted in #14, the line:
    PHP Code:
     echo "<h2 class=\"entry-title\"><a rel=\"bookmark\">".get_the_title(). "</a></h2>" 
    is close to permalinks, you just forgot to start the link. You need to change it to:
    PHP Code:
     echo "<h2 class=\"entry-title\"><a href=\"".get_permalink()."\" rel=\"bookmark\">".get_the_title(). "</a></h2>"
    Quote Originally Posted by nofel View Post
    Ah ok. i will look into it but why doesn't post have templates? without plugin
    You can create templates for specific posts, post categories and author specific. This page mentions the author part: http://codex.wordpress.org/Author_Te...ile_is_Used.3F since you were just 0changing the background image, I detailed how to do it via header.php versus setting up author specific templates. I would have to think a minute, but at first glance, you'd have to edit header.php anyway.
    Ryan B | My Blog | Twitter


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •