Anyone know php?

Log in to stop seeing adverts

Status
Not open for further replies.

Joe_Fox

Well-Known Member
I'm doing some work with WordPress and it's all php.

Wonder if someone can help me tweak the php so that the blog index page only shows post excerpts rather than the full post and adds a 'read more' link to the end of the excerpt taking the user to the full post.

Thanks,
Joe_Fox
 
I'm doing some work with WordPress and it's all php.

Wonder if someone can help me tweak the php so that the blog index page only shows post excerpts rather than the full post and adds a 'read more' link to the end of the excerpt taking the user to the full post.

Thanks,
Joe_Fox

You don't need to know PHP for that, you just need to know the way Wordpress themes work.

I've been doing the same thing this morning but in reverse - my theme shows the excerpt when I want it to show the full post.

The way to do it is to edit the theme file that appears in the index page (it might be index.php - but it can vary).
Change the bit that says "the_content()" to say "the_excerpt()"
 
Webbo, already tried that - it works although when you click through to the full post it still only shows the excerpt.

I need to do something like:

If ($single) {
the_content();
} else {
the_excerpt();
}

but not sure on the php syntax really.
 
Webbo, I'm using the Graphene theme if that helps. There is no post.php but there is a single.php:

Code:
<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Graphene
 * @since Graphene 1.0
 */

get_header(); ?>

	<?php
    /* Run the loop to output the posts.
     * If you want to overload this in a child theme then include a file
     * called loop-single.php and that will be used instead.
     */
     get_template_part('loop', 'single');
    ?>
            
<?php get_sidebar(); ?>
<?php get_footer(); ?>
 
I suppose I should add the loop.php

Code:
<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
            <div id="post-<?php the_ID(); ?>" <?php post_class('clearfix post'); ?>>
            
            	<?php /* Posts navigation for single post pages, but not for Page post */ ?>
                <?php if (is_single() && !is_page()) : ?>
            	<div class="post-nav clearfix">
                    <p id="previous"><?php previous_post_link() ?></p>
                    <p id="next-post"><?php next_post_link() ?></p>
                    <?php do_action('graphene_post_nav'); ?>
                </div>
                <?php endif; ?>
                
                <?php /* Post date is not shown if this is a Page post */ ?>
                <?php if (!is_page() && (get_option('graphene_hide_post_date') != true)) : ?>
                <div class="date">
                    <p><?php the_time('M'); ?><br /><span><?php the_time('d') ?></span></p>
                    <?php do_action('graphene_post_date'); ?>
                </div>
                <?php endif; ?>
                
                <div class="entry clearfix<?php if (get_option('graphene_hide_post_date') == true) {echo ' nodate';} ?>">
                
                	<?php /* Post title */ ?>
                    <h2>
                    	<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(esc_attr__('Permalink Link to %s', 'graphene'), the_title_attribute('echo=0')); ?>"><?php if (get_the_title() == '') {_e('(No title)','graphene');} else {the_title();} ?></a>
                    <?php do_action('graphene_post_title'); ?>
                    </h2>
                    
					<?php /* Post meta */ ?>
                    <div class="post-meta clearfix">
                    	
						<?php /* Post category, not shown if this is a Page post or if admin decides to hide it */ ?>
                        <?php if (!is_page() && (get_option('graphene_hide_post_cat') != true)) : ?>
                        <ul class="meta_categories">
                            <li><?php the_category(",</li>\n<li>") ?></li>
                        </ul>
                        <?php endif; ?>
                        
                        <?php /* Post author, not not shown if this is a Page post or if admin decides to hide it */ ?>
                        <?php if (get_option('graphene_hide_post_author') != true) : ?>
                        <p class="post-author">
							<?php
								if (!is_page()) {
									/* translators: this is for the author byline, such as 'by John Doe' */
									_e('by','graphene'); echo ' '; the_author_posts_link();
								}
								edit_post_link(__('Edit post','graphene'), ' (', ')');
								
								/* Show the post author's gravatar if enabled */
								if (get_option('graphene_show_post_avatar')) {
									echo get_avatar(get_the_author_meta('user_email'), 40);
								}
							?>
                        </p>
                        <?php endif; ?>
                        
                        <?php do_action('graphene_post_meta'); ?>
                    </div>
                    
                    <?php /* Post content */ ?>
                    <div class="entry-content clearfix">
                    	<?php do_action('graphene_before_post_content'); ?>
                        
                    	<?php if (!is_search() && !is_archive()) : ?>
                        <?php the_content(__('Read the rest of this entry &raquo;','graphene')); ?>
                        <?php else : ?>
                        	<?php the_excerpt(); ?>
                        <?php endif; ?>
                        <?php wp_link_pages(array('before' => __('<p><strong>Pages:</strong> ','graphene'), 'after' => '</p>', 'next_or_number' => 'number')); ?>
                        
                        <?php do_action('graphene_after_post_content'); ?>
                        
                    </div>
                    
                    <?php /* Post footer */ ?>
                    <div class="entry-footer clearfix">
                    	<?php /* Display the post's tags, if there is any */ ?>
                        <?php if (!is_page() && (get_option('graphene_hide_post_tags') != true)) : ?>
                        <p class="post-tags"><?php if (has_tag()) {_e('Tags:','graphene'); the_tags(' ', ', ', '');} else {_e('This post has no tag','graphene');} ?></p>
                        <?php endif; ?>
                        
						<?php 
							/**
							 * Display AddThis social sharing button if single post page, comments popup link otherwise.
							 * See the graphene_addthis() function in functions.php
							*/ 
						?>
                        <?php if (is_single() || is_page()) : ?>
                            <?php graphene_addthis(); ?>
                        <?php elseif (get_option('graphene_hide_post_commentcount') != true) : ?>
                        	<p class="comment-link"><?php comments_popup_link(__('Leave comment','graphene'), __('1 comment','graphene'), __("% comments",'graphene')); ?></p>
                        <?php endif; ?>
                        
                        <?php do_action('graphene_post_footer'); ?>
                    </div>
                </div>
            </div>
            
            <?php 
			/**
			 * Display Adsense advertising for single post pages 
			 * See graphene_adsense() function in functions.php
			*/ 
			?>
            <?php if (is_single() || is_page() || get_option('graphene_adsense_show_frontpage')) {graphene_adsense();} ?>
            
            <?php /* Get the comments template for single post pages */ ?>
            <?php if (is_single() || is_page()) {comments_template();} ?>
            
	<?php endwhile; ?>
    
    <?php /* Display posts navigation if this is not a single post page */ ?>
    <?php if (!is_single()) : ?>
        <?php /* Posts navigation. See functions.php for the function definition */ ?>
    	<?php graphene_posts_nav(); ?>
    <?php endif; ?>
    
<?php /* If there is no post, display message and search form */ ?>
<?php else : ?>
    <div class="post">
        <h2><?php _e('Not found','graphene'); ?></h2>
        <div class="entry-content">
            <p>
			<?php 
				if (!is_search())
					_e("Sorry, but you are looking for something that isn't here. Wanna try a search?","graphene"); 
				else
					_e("Sorry, but no results were found for that keyword. Wanna try an alternative keyword search?","graphene"); 
			?>
                
            </p>
            <?php get_search_form(); ?>
        </div>
    </div>
    
    <?php do_action('graphene_not_found'); ?>
<?php endif; ?>
 
....and the header.php is as follows:

Code:
<?php
/**
 * The Header for our theme.
 *
 * Displays all of the <head> section and everything up till <div id="content-main">
 *
 * @package WordPress
 * @subpackage Graphene
 * @since graphene 1.0
 */
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
<head profile="http://gmpg.org/xfn/11">
    <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
    <title><?php 
		if(is_home()) {  
			echo get_bloginfo('description') . " &raquo; " . get_bloginfo('name'); 
		} 
		else { 
			echo wp_title('', false) . " &raquo; " . get_bloginfo('name'); 
		} 
		?>	
    </title>
	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> 
    <style type="text/css" media="screen">@import "<?php echo get_stylesheet_uri(); ?>";</style>  
      <!--[if lte IE 6]>
      	  <style>#container{background:none;}</style>
          <script>
          sfHover = function() {
              var sfEls = document.getElementById("menu").getElementsByTagName("LI");
              for (var i=0; i<sfEls.length; i++) {
                  sfEls[i].onmouseover=function() {
                      this.className+=" sfhover";
                  }
                  sfEls[i].onmouseout=function() {
                      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
                  }
              }
          }
          if (window.attachEvent) window.attachEvent("onload", sfHover);
		  </script>
      <![endif]-->
       
    <?php
	/* We add some JavaScript to pages with the comment form
	 * to support sites with threaded comments (when in use).
	 */
	if ( is_singular() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );

	/* Always have wp_head() just before the closing </head>
	 * tag of your theme, or you will break many plugins, which
	 * generally use this hook to add elements to <head> such
	 * as styles, scripts, and meta tags.
	 */
	wp_head();
	?>
</head>
<body <?php body_class(); ?>>
	<div id="container">
    	<div id="top-bar">
        	<div id="rss">
            	<a href="<?php bloginfo('rss2_url'); ?>" title="<?php esc_attr_e('Subscribe to RSS feed', 'graphene'); ?>" class="rss_link"><span><?php _e('Subscribe to RSS feed', 'graphene'); ?></span></a>
                <?php do_action('graphene_feed_icon'); ?>
            </div>
            
            <?php 
			/**
			 * Retrieves our custom search form. Note that this search form is only used
			 * in the top bar of the theme. Since the theme uses the default wordpress
			 * search form somewhere else, we do not use get_search_form() here.
			*/ 
			?>
            <div id="top_search">
            <?php get_search_form(); ?>
            <?php do_action('graphene_top_search'); ?>
            </div>
        </div>
        <?php
        if ( is_singular() && has_post_thumbnail( $post->ID ) &&( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail' ) ) &&	$image[1] >= HEADER_IMAGE_WIDTH ) {
			// Houston, we have a new header image!
			// Gets only the image url. It's a pain, I know! Wish Wordpress has better options on this one
			$header_img = get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
			$header_img = explode('" class="', $header_img);
			$header_img = $header_img[0];
			$header_img = explode('src="', $header_img);
			$header_img = $header_img[1];
		} else {
			$header_img = get_header_image();
		}
		
		/* Check if the page uses SSL and change HTTP to HTTPS if true */
		if (is_ssl() && !stripos($header_img, 'https')){
			$header_img = str_replace('http', 'https', $header_img);	
		}
		
		// Gets the colour for header texts, or if we should display them at all
		if ( 'blank' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR) || '' == get_theme_mod('header_textcolor', HEADER_TEXTCOLOR))
			$style = ' style="display:none;"';
		else
			$style = ' style="color:#' . get_theme_mod( 'header_textcolor', HEADER_TEXTCOLOR ) . ';"';
		?>
        <div id="header" style="background-image:url(<?php echo $header_img; ?>);">
        	<h1 <?php echo $style; ?> class="header_title"><a <?php echo $style; ?> href="<?php echo home_url(); ?>"><?php bloginfo('name'); ?></a></h1>
            <h2 <?php echo $style; ?> class="header_desc"><?php bloginfo('description'); ?></h2>
        </div>
        <div id="nav">
        	<!-- BEGIN dynamically generated and highlighted menu -->
        	<?php wp_nav_menu(array('container' => '', 'menu_id' => 'menu', 'menu_class' => 'clearfix', 'fallback_cb' => 'graphene_default_menu', 'depth' => 5, 'theme_location' => 'Header Menu')); ?>
            
            <?php do_action('graphene_top_menu'); ?>
            <!-- END dynamically generated and highlighted menu -->
        </div>
        
        <?php do_action('graphene_before_content'); ?>
        
        <div id="content" class="clearfix<?php if (is_page_template('template-onecolumn.php')) {echo ' one_column';} ?>">
        	<div id="content-main" class="clearfix">
            	
                <?php /* The preview slider */ ?>
                <?php if (is_front_page() && !get_option('graphene_slider_disable')) : ?>
                <?php do_action('graphene_before_slider'); ?>
                <div class="featured_slider">
                	<?php do_action('graphene_before_slideritems'); ?>
                	<div id="slider_root">
                		<div class="slider_items">
				<?php 
					/**
					 * Get the featured posts to be displayed on the slider
					*/
					global $post;
					
					/**
					 * Get the category whose posts should be displayed here. If no 
					 * category is defined, the 5 latest posts will be displayed
					*/
					$slidercat = (get_option('graphene_slider_cat') != '') ? get_option('graphene_slider_cat') : false;
					
					/* Get the list of posts to display in the slider */
					if (!$slidercat) {						
						$sliderposts = get_posts(array(
										'posts_per_page' => 5,
										'orderby' => 'date',
										'order' => 'DESC',
											   ));
					} else {
						$sliderposts = get_posts(array(
										'category_name' => $slidercat,
										'orderby' => 'date',
										'order' => 'DESC',
										'nopaging' => true
											   ));
					}
						
					/* Display each post in the slider */	
					foreach ($sliderposts as $post){
						setup_postdata($post); ?>
                        
						<div class="slider_post clearfix">
						
                        	<?php do_action('graphene_before_sliderpost'); ?>
                        
							<?php /* The slider post's featured image */ ?>
                            <div class="sliderpost_featured_image alignleft">
                                <a href="<?php the_permalink(); ?>">
                                <?php if (has_post_thumbnail()) : ?>
                                	<?php the_post_thumbnail(array(150,150,true)); ?>
                                <?php else : ?>
                                	<img src="<?php bloginfo('template_url'); ?>/images/img_slider_generic.png" alt="" />
                                <?php endif; ?>
                                </a>
                            </div>
							
                            <?php /* The slider post's title */ ?>
							<h2 class="slider_post_title">
								<a href="<?php the_permalink(); ?>">
								<?php the_title(); ?>
								</a>
							</h2>
                            
                            <?php /* The slider post's excerpt */ ?>
							<div class="slider_post_entry">
								<?php the_excerpt(); ?>
                                <a class="block_link" href="<?php the_permalink(); ?>"><?php _e('View full post', 'graphene'); ?></a>
                                
                                <?php do_action('graphene_slider_postentry'); ?>
							</div>
						</div>
					<?php	
					}
                    
                ?>
                		</div>
                	</div>
                    
                    <?php /* The slider navigation */ ?>
                    <div class="slider_nav">
						<?php $i = 0; foreach ($sliderposts as $post) : ?>
                        <a href="#" <?php if ($i == 0) {echo ' class="active"';} ?>><span><?php the_title(); ?></span></a>
                        <?php $i++; endforeach; ?>
                        
                        <?php do_action('graphene_slider_nav'); ?>
                    </div>
                    
                </div>
                <?php endif; ?>
 
Webbo, I could kiss you.

How would I add a link underneath the excerpt that said "click to view full post" though?
 
Brilliant work - one thing though Webbo, now when I search for a post that has an excerpt, the whole post is shown in the result rather than just the excerpt (same true if you look at posts of a particular categories, calendar).

Before the tweaks this was the one part that was fine so is there a way to fix this?
 
Webbo, I think I've sussed this:

in the loop.php change the line (66)...
Code:
<?php if (!is_search() && !is_archive()  && !is_single()) : ?>

to...
Code:
<?php if (!is_single()) : ?>

I believe this has solved my problem. Agree?
 
Ok, I swear on my life that I did not read your post before I sussed this out.
Wise minds and that. Webbo, thanks a lot for this - no doubt I'll have more wordpress stuff soon.

I like WordPress - it's pretty powerful - what are you using it for anyway? Talkingballs blog?
 
Webbo - I'm going to call on your expertise again if that's ok (or anyone else who can help of course!)

When I go to a blog post, it says posted by <username> rather than the full name. How can I change this to show a link to the full name?
 
Webbo - I'm going to call on your expertise again if that's ok (or anyone else who can help of course!)

When I go to a blog post, it says posted by <username> rather than the full name. How can I change this to show a link to the full name?

The bit that displays the name is in loop.php, where it says "the_author_posts_link()". That calls a function which shows the username and links to a page with all their posts.

It would be easy enough to include their first name and surname instead (using info here: http://codex.wordpress.org/Template_Tags/the_author_meta ), but that would only make things more complicated further down the line, as the page showing all their posts would use their username, so you'd need to make changes to that too, and maybe in other places. It could end up getting too messy.


I would recommend making people choose usernames that identify who they are, so you can use Wordpress the way it's designed to work.
 
The bit that displays the name is in loop.php, where it says "the_author_posts_link()". That calls a function which shows the username and links to a page with all their posts.

It would be easy enough to include their first name and surname instead (using info here: http://codex.wordpress.org/Template_Tags/the_author_meta ), but that would only make things more complicated further down the line, as the page showing all their posts would use their username, so you'd need to make changes to that too, and maybe in other places. It could end up getting too messy.


I would recommend making people choose usernames that identify who they are, so you can use Wordpress the way it's designed to work.

What about showing the username AND the full name but just having the username that is clickable.
 
Cheers Webbo - now sorted.
I have a non clickable full name and a clickable username.
Problem solved.
 
Status
Not open for further replies.
Log in to stop seeing adverts

P Pld Pts
1Manchester C  412
2Arsenal410
3Newcastle410
4Liverpool49
5Aston Villa49
6Brighton48
7Nottm F48
8Chelsea47
9Brentford46
10Manchester U46
11Bournemouth45
12Fulham45
13Tottenham 44
14West Ham44
15Leicester42
16Palace42
17Ipswich42
18Wolves41
19Southampton40
20Everton40

Latest posts

Back
Top