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.

What determines what image is shown? Category? Author? Random? etc


Some may not like this, but whatever. In styles.css, I would make styles for each image:

#authName {
 background: url();
#authName2 {...}

Replace name with something you can recognize.

Now in header.php

   echo "id=\\"authName\\""
  // continue through authors

For reference: http://codex.wordpress.org/Function_Reference/is_author

how about using custom fields?

Custom fields wouldn’t let you swap backgrounds to my knowledge.

Custom fields hold text, not links. You would need to do major tweaking to the theme to make it work…

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?

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?

You need to edit a few files. I found: http://codex.wordpress.org/Using_Custom_Fields_to_attach_images,_links_or_files_to_a_post_easily. Note, using custom fields to do this, you will need to point to the file every post. My way is automatic

I would need to see code to even take a guess.

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 :stuck_out_tongue: 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

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,

hahah, nice example you gave. i made it work by using the_content() and get_the_title(); however the permalinks are gone :frowning: here the template code

    Template Name:Backgound images
get_header(); the_post();?>
        <div id="container">
            <div id="content" role="main" style="margin-top:10px;">

            /* 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>";
            </div><!-- #content -->
        </div><!-- #container -->

<?php get_sidebar();?>
<div id="photo">
    <img src="<?php echo get_post_meta($post->ID,'bg_images',true); ?>" />
<?php get_footer(); ?>

and the link for live site

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.

Ah ok. i will look into it but why doesn’t post have templates? without plugin

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.

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:

   echo "id=\\"authName\\"" 
  // continue through authors 

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:

 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:

 echo "<h2 class=\\"entry-title\\"><a href=\\"".get_permalink()."\\" rel=\\"bookmark\\">".get_the_title(). "</a></h2>";

You can create templates for specific posts, post categories and author specific. This page mentions the author part: http://codex.wordpress.org/Author_Templates#Which_Template_File_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.:slight_smile: