Powered by MediaWiki
Personal tools

Main.php file

From B2evolution

Jump to: navigation, search

The correct title of this article is _main.php file. The missing underscore and the capital M are due to technical restrictions.

Note: this page is about b2evo 1.x. In b2evo 2.x, _main.php has been renamed to main.tpl.php and the contents follow slightly different rules. See Skins 2.0

Contents

[edit] Intro

If you want to make changes to an evoskin that go beyond the colours and background images of the css file, then you'll need to edit the _main.php for your skin. If you're not familiar with php, then it may look a little intimidating at first. The _main.php file contains comments that explain what each part of the code is for, and editing the _main.php can be a great way to learn more about php.

The _main.php file actually contains a mixture of XHTML markup and php code. Everything inside of the <?php ?> tag will be executed and will output something when the page is viewed. Everything outside of the php tags is XHTML code that will be sent to the browser just as it is.

[edit] Header

At the top of your _main.php file you may find a comment block with details and copyright information about the skin.

Then, you'll find this:

Code: Don't access this file directly
 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );

This little bit of code is added to all php files in a skin to prevent a user from trying to view the file in a browser.

After that, the real output will begin:

Code: The output begins
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php locale_lang() ?>" lang="<?php locale_lang() ?>">
<head>

The Doctype tag declares what type of code you're using. It's important for code validation. You'll only need to change it if you're using a different Doctype. The opening html tag and opening head tag are standard and shouldn't need to be changed.

Most of the code within the head tags will not need to be changed. Title, keywords, descriptions, RSS url, etc. are all generated dynamically based on blog settings in the back office.

You may, however, need to edit the line of code that points toward the css file:

Code: Declare the location of the css file
 <link rel="stylesheet" href="custom.css" type="text/css" />

If your css file uses a different filename, then replace custom.css with that name.

Inside the body tag, you may find a block of code like this:

Code: The bloglist
        // --------------------------- BLOG LIST INCLUDED HERE -----------------------------
       require( dirname(__FILE__).'/_bloglist.php' );
       // ------------------------------- END OF BLOG LIST --------------------------------

This includes the _bloglist.php file in your skin to display a list of blogs on your b2evolution install. There are backoffice settings to disable this if you don't want it. If you want to edit the way the bloglist looks, then open the _bloglist.php file and change it. If you want to move the bloglist to another part of your page, then this is the code to move.

[edit] Main content area

The main content area generally displays your blog posts. But, it can also display some special content, such as an extended list of archive links, a contact form, a user profile or a list of the latest comments.

[edit] The Post Loop

When you're viewing normal blog posts, they are outputted though the skin's post loop. The post loop is a php while loop that retrieves posts from the database and displays them using the code inside the loop. All of the code bewteen the curly braces is repeated for each post.

Code: The post loop
if( isset($MainList) ) while( $Item = & $MainList->get_item() )
{
...
}

This is the part of the skin where you can change how the posts look and what details are shown about them. The names of things are very descriptive and many of them also have comments explaining what they are. Any text preceded by a double backslash is a comment.

[edit] Special Pages

Sometimes your blog is viewed with a url like this: index.php?disp=comments. In that case, the normal list of posts will not be shown, but rather a list of the most recent comments on your blog. There are several special pages that are called in this way using this section of your skin:

Code: Special pages
<?php 
// ---------------- START OF INCLUDES FOR LAST COMMENTS, ETC. ----------------
       switch( $disp )
       {
               case 'comments':
                       // this includes the last comments if requested:
                       require( dirname(__FILE__).'/_lastcomments.php' );
                       break;
               case 'arcdir':
                       // this includes the archive directory if requested
                       require( dirname(__FILE__).'/_arcdir.php');
                       break;
               case 'profile':
                       // this includes the profile form if requested
                       require( dirname(__FILE__).'/_profile.php');
                       break;
               case 'msgform':
                       // this includes the email form if requested
                       require( dirname(__FILE__).'/_msgform.php');
                       break;
               case 'subs':
                       // this includes the subscription form if requested
                       require( dirname(__FILE__).'/_subscriptions.php');
                       break;
       }
       // ------------------- END OF INCLUDES FOR LAST COMMENTS, ETC. ------------------- 
?>

If you want to edit the appearance or layout of these special pages, then you'll actually need to edit the file that is included by this block of code. For example, if you want to change the list of recent comments, then edit /skins/yourskin/_lastcomments.php.

You could also use this block of code to add a custom special page. Copy one of the sections of code and edit it with your own values.

On the latest 1.x versions, the following block is required to display special pages:

Code: Special pages on recent versions
<?php
        // ------------- START OF INCLUDE FOR COMMENTS, TRACKBACK, PINGBACK, ETC. -------------
        $disp_comments = 1;                                        // Display the comments if requested
        $disp_comment_form = 1;                        // Display the comments form if comments requested
        $disp_trackbacks = 1;                                // Display the trackbacks if requested

        $disp_trackback_url = 1;                // Display the trackbal URL if trackbacks requested
        $disp_pingbacks = 1;                                // Display the pingbacks if requested
        require( dirname(__FILE__).'/_feedback.php' );
        // -------------- END OF INCLUDE FOR COMMENTS, TRACKBACK, PINGBACK, ETC. --------------
?>

[edit] Sidebar

[edit] Normal things to show

[edit] The Author/Creator of an article

If you want to place the name of the author 'straight', then use this function

<?php $Item->author(); ?>

The complete function is this :

/**
* Template function: display author/creator of item
*
* @param string String to display before author name
* @param string String to display after author name
* @param string Output format, see {@link format_to_output()}
*/
function author( $before = , $after = , $format = 'htmlbody' )

That way, if you want text before or after the name of the author do it like this example

<?php $Item->author('Author','created this article'); ?>

gives this result Author John created this article

[edit] The blogname

On the All-blog, all articles from all blogs are shown. If you want to show which blog an article comes from, you can use this code

<?php $Item->blog_name(); ?>

[edit] A permalink to the article

You can place a permalink of the article in different ways

If you want a little icon like Afbeelding:minipost.gif, you use this code :

<?php $Item->permanent_link( '#icon#' ); ?>

To have the word 'Permalink' as your permalink, use this :

<?php $Item->permanent_link( '#text#' ); ?>

To have the title of your article as the permalink, use this :

<?php $Item->permanent_link( '#title#' ); ?>

To use your own phrase for the permalink, use this :

<?php $Item->permanent_link( 'My coined phrase' ); ?>

To use an icon or image of your own, use this :

<?php $Item->permanent_link( '<img src="rsc/icon_permalink.gif" />' ); ?>

To change the ALT-text use this

<?php $Item->permanent_link( '#title#','This is the beautiful-URL from this article.  Use this if you want to quote this article' ); ?>

The complete function is this :

/**
* Displays a permalink link to the Item
* @param string link text or special value: '#', '#icon#', '#text#', '#title#'
* @param string link title
* @param string class name
*/
function permanent_link( $text = '#', $title = '#', $class =  )
{
  echo $this->get_permanent_link( $text, $title, $class );
}

[edit] Special functions

[edit] Display the 4 most popular (most viewed articles)

<?php 
     $TOPART = new ItemList($blog, array(), '', '', -1, '', array(), '', 'DESC', 'views', 4); 
     echo "<ul>"; 
     while ($TOPART_ITEM = $TOPART->get_item()) { 
     echo "<li><a href=\"", $TOPART_ITEM->get_permanent_url(); 
     echo "\">", $TOPART_ITEM->title('','',false), "</a>"; 
     } 
     echo "</ul>"; 
 ?> 


[edit] Display a print-icon

  • Display a link to print:window, but only if we are showing the article on a single page
<?php if( $disp=='single' )  
  { ?> 
  <a href="javascript:window.print()"><img src="rsc/icon_16_print.gif" border="0" /></a> 
<?php } ?>