WordPress:Adding Asides

来自站长百科
Xxf3325讨论 | 贡献2008年7月11日 (五) 14:37的版本 (新页面: __TOC__ When you lean towards someone and tell them a little bit of information, you are making an "aside" comment. In blogs, you can do that on your blog by passing on small bits of inf...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

When you lean towards someone and tell them a little bit of information, you are making an "aside" comment. In blogs, you can do that on your blog by passing on small bits of information to your readers called Asides.

Also known as remaindered links or linkblog, Asides were originally implemented by Matt Mullenweg, developer of WordPress, and it soon spread far and wide and became a very popular method of adding little bits of information to your blog.

Asides can be run from within your WordPress site, and do not require installation of another blog. All you need is a few lines of code, and a new category.

Using Asides requires you to make some changes in the WordPress Loop, but if you are careful, everything will be all right.

Asides With a Plug-In

The MiniPosts2 plugin allows you to flag any post as an aside, without having to edit code or create special categories. It uses the widgets plugin to allow you easy control over the placement of your aside list in your sidebar. To mark a post as an aside, you just have to check a box on the post edit page.

See also SideBlog which runs as a widget or via normal sidebar hacking.

The AsideShop plugin allows you to show aside posts on your blog's frontpage in a different way without theme template modification. Make a template in Administration Panel and assign it to a category which contain aside posts. AsideShop does not add any information to posts or categories.

Asides With Code Hacking

If you are comfortable editing PHP files, then these two approaches are for you.

Matt's Asides

center|frame|Asides at photomatt.netThe following are the step by step instructions for manually adding Asides to your WordPress site. Below are WordPress plugins that will make the process much easier.

1. Make a backup copy of your index.php in your WordPress Theme, typically found at /wp-content/themes/yourtheme. Seriously. BACK IT UP!!
2. In your WordPress:Administration Panels > Manage > Categories add a category for your Asides. You can call it Asides if you want. Note the Category ID number of the category. In the following examples we will use category ID 14, but you change this to the category ID of your Asides category.
3. Open your Theme's index.php template file in a text editor.
4. Search for:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Comment it out like this:
<?php //if (have_posts()) : while (have_posts()) : the_post(); ?>
5. Under this paste:
<?php
if ($posts)
{
function stupid_hack($str)
{
return preg_replace('|</ul>\s*<ul class="linklog">|', '', $str);
}
ob_start('stupid_hack');
foreach($posts as $post)
{
the_post();
?>
6. The next line should be your date call (<?php the_date(); ?>). Under that, paste this:
<?php if ( in_category(14) && !is_single() ) : ?>
<ul class="linklog">
<li id="p<?php the_ID(); ?>"><?php echo wptexturize($post->post_content); ?>
<span><?php comments_popup_link('(0)', '(1)',
'(%)')?> <a href="<?php the_permalink(); ?>"
title="Permalink: <?php echo 
wptexturize(strip_tags(stripslashes($post->post_title), '')); ?>"
rel="bookmark">#</a> <?php edit_post_link('(e)'); ?></span></li>
</ul>
<?php else: // If it's a regular post or a permalink page ?>
7: At the bottom of the index.php file, find:
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
8. Delete those lines. And instead, paste these lines:
<?php endif; // end if in category ?>
<?php
}
}
else
{
echo '<p>Sorry no posts found.</p>';
}
?>

That's it. Asides should now be working. Only thing that remains is styling the CSS.

Styling Your Asides

What we've done is create a very basic style. You should add your own style to blend it in with the rest of your site.

To style the Asides, add these example lines to the CSS:

ul.linklog li {
border:1px solid #ff0000;
font:Verdana;
font-size:14pt;
background-color:#ffff00;
}

ul.linklog li a {
text-decoration:none;
}

Displaying Asides on the Sidebar

right|frame|Asides on the Sidebar at noscope.comREMEMBER: The following section has nothing to do with Matt's asides. They are INDEPENDENT of each other and will not work with each other.

There are times when you want to display your Asides in your sidebar. In the sidebar.php template file of your WordPress Theme, add the following where you wish the asides to appear. If you want them outside of the list, make sure to position this above or below the Unordered List (<ul>) tags or within them as a List Item (<li>):

<?php
if ($posts) : foreach ($posts as $post) : the_post();
?>
<?php if (in_category(14)) { ?>
<div class="asides_sidebar">
 <?php echo $post->post_excerpt ?> <?php the_content(); ?>
 <small><?php comments_popup_link(__('#'), __('(1)'), __('(%)')); ?>
   <?php edit_post_link('Edit', ' — '); ?></small>
</div>
<?php } ?>
<?php endforeach; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Now, you can style the CSS for the div "asides_sidebar" to your liking in the style.css style sheet.

With Asides in the sidebar, you may want to disable them from being displayed within the main loop. This can be easily done with a snippet of code within the loop.

Find this line within the loop in your main index.php file:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Just after that line, paste this line (change the 14 to your category ID number):

<?php if (in_category('14') && is_home() ) continue; ?>

After that, you will then need to find this line:

<?php endwhile; ?>

And just before that line, paste this:

<?php } ?>

After doing all of that, you will need to put this line:

<?php rewind_posts(); ?>

Either before the loop in your index.php file or before the loop in your sidebar.php file, depending on which one comes last. So, for example, if <?php get_sidebar(); ?> comes after the <?php endif; ?> line in index.php, then you will have to put it before the loop in sidebar.php.

Voila. Asides will now be displayed on the sidebar, rather than within the loop, giving it a more 'linkblog' feel.

Resources