WordPress.org

Ready to get started?Download WordPress

Forums

Retaining template across pages on clicking next/previous posts (4 posts)

  1. MKULTRON
    Member
    Posted 3 years ago #

    I have several custom templates set up for different authors and each template has a custom banner. By clicking on a "previous" link I am taken back to the previous posts by this author (archives.php I think?) however the banner is the default site banner. Is there
    a way to retain the template of the author across this pages? Maybe through a querystring?

    Thanks
    MK

  2. Digital Raindrops
    Member
    Posted 3 years ago #

    Previous and Next posts are the single.php, not sure how you mean custom templates which are pages, how do they link to posts?

    A link to the website and a scenario may help us understand!

    There are limited scenarios for the single.php

    This untested code may help, it assumes that the banner image is based on the author slug (nice name) and there is a default fallback image.

    You already have a class in the style.css for the .author-banner, and on click it would take you to the authors (user) url for a splash page.

    Example
    The code would be in the single.php in the twenty ten theme just before
    <div id="nav-above" class="navigation">

    <?php
    //Get the Author image by Digital Raindrops
    function dr_author_image( $authorid ) {
    	$slug = the_author_meta('user_nicename', $authorid );
    	if ( file_exists( get_template_directory_uri() .'/images/'.$slug.'.png' ) ) {
    		return get_template_directory_uri() .'/images/'.$slug.'.png); ';
    	}else{
    		return get_template_directory_uri() .'/images/auth-banner.png); ';
    	}
    }
    ?>
    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    	<?php
    		//Get author image by and the author url
    		$style = 'background-image: url(' .dr_author_image( $post->post_author) .');';
    		$link = the_author_meta( 'user_url' );
    	?>
    
    	<?php //display the author banner and add a link ?>
    	<div class="author-banner" style="<?php echo $style; ?>" onclick="location.href='<?php echo $link; ?>'"></div>

    The full twenty ten single.php someone else may want a look!

    If the banner is a header full width banner then you could just do this in the header.php file, and use the conditional tag if( is_single() ) to only add the banner on the single.php pages.

    HTH

    David

  3. MKULTRON
    Member
    Posted 3 years ago #

    Sorry, single.php works just fine! Getting confused over my templates a little at this stage.

    Basically I have custom template pages like this:

    http://sistersnoir.com/blog/laurence-cade-2/

    and the banner at the top "the journal of..." is specific to this page.

    When I click on the previous post "still life" at the moment, near the bottom of the page, it takes me to the last of Laurence's posts but the banner is the default site banner (as it's a different template). I guess I want the previous posts of this author to load on the same template.

  4. Digital Raindrops
    Member
    Posted 3 years ago #

    Ok if I am right each author has a page template, that is called from the menu and you have added the header image in each template.

    However the latest posts and other posts linked from the page will load the single.php so you would need a different approach.

    If all your header image code is in the single template you would add code to the single.php if you have a header.php with the default image you could add this code in header.php

    This will look to see if it is a page or a single post and load the authors image, if it was archive search index etc: no change.

    http://pastebin.com/x49p8xxy

    This is again UNTESTED as it is not something I have done, it should help in the right direction, changed the function name, moved the code, only change to authors header on page or single and only if it exists, else it will return an empty string.

    <?php
    //Get the Author image by Digital Raindrops
    function dr_author_header() {
    	$style="";
    	if ( is_page() || is_single() ) {
    		$authorid =  $post[0]->post_author;
    		$slug = the_author_meta('user_nicename', $authorid );
    		if ( file_exists( get_template_directory_uri() .'/images/'.$slug.'.png' ) ) {
    			$link = the_author_meta( 'user_url', $authorid );
    			//Get author image by and the author url
    			$style = ' style="background-image: url(' .get_template_directory_uri() .'/images/'.$slug.'.png);" ';
    			$style = $style .'  onclick="location.href='.$link.'" ';
    		}
    	}
    	return $style;
    }
    ?>
    
    <?php
    	//See if we want to swap the header banner and add a link
    	$style=dr_author_header();
    ?>
             <!-- Change the header image if set -->
    	<div class="header-png"<?php echo $style; ?>></div>

    HTH

    David

Topic Closed

This topic has been closed to new replies.

About this Topic