Category-x.php file one more time ;)

flaga Treść tego wpisu jest dostępna również w języku polskim

There is also one more situation we can use category-x.php file. This file ‘gathers’ all posts assigned to x category and display them on one page after clicking on category x name. But what if we want to have regular page with some text and all post from category x below accessed via link in menu? No problem :happy_tb: .
We need to change two things in category-x file; we have to add title for this page and text (sorry, no possibility to add text as to regular pages ;). So open your category-x file and look for line <?php if (have_posts()) : while (have_posts()) : the_post(); ?>. Just above it we need to add <h2 class="title">Page title</h2> Class might be different. Look into your page.php file and check what class is assigned to its title. The same class you need to use for title in category-x file. If you want the title to look like any other it should be linked to this page. So it will look like that:

<h2 class="title"><a href="http://example.com/category/x/">Page title</a></h2>

( To find out URl for this category just click on category x name and copy URl from browser window )
Below title (but still above <?php if (have_posts()) : while (have_posts()) : the_post(); ?>) there will be some text. You need to paste for example:

<div class="entry">
<p>This is some text which will show up on top of page for category x. It can be as long as you need...;)</p>
</div>

We need to use class to make sure that text will look as on any other page. To find out its name check on page.php file. Look there for <?php the_content(); ?> and check out the name of class just above it.
OK. Page is ready. It will look like this (it may vary depending on theme you use):
Continue Reading »

About category-x.php file

flaga Treść tego wpisu jest dostępna również w języku polskim

The most common purpose for creating and using category-x.php file is to exclude some category from showing on a front page. For example we have category ‘specials’ that can be viewed only after entering category itself but not on the main page.
First we create new file. We copy content of index.php to editor and save it as category-x.php where x is an ID of category ‘specials’. We need to put that file inside folder with theme we use. Then in index.php just bellow <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> we need to add <?php if (in_category('x')) continue; ?>. And that’s all. Any entry from category ‘special’ won’t show up on front page ;).

But we can use that file to change number of posts displayed on a page for one category only.
NOTE! There is a plugin called Custom Query String which enables to vary number of post being displayed on every page (author, archive, category, year, month, day, search, feed, home). However ALL categories will have the same number of post being displayed.
Let’s say we want to have 5 posts only displayed in category ‘extras’ while on other categories pages 10 posts can be seen. Just as I described above we create category-x.php file. In this case we DON’T need to change anything in index.php file. In category-x.php just above <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> we need to add:

<?php if (is_category()) { $posts = query_posts($query_string . '&orderby=date&showposts=5'); } ?>

And done. When we browse through category ‘extras’ on each page we can see 5 posts only.

Listing posts from one category

Sometime it is needed to display list of all post from only one category in some specified place on your site eg. a sidebar.
To achieve that you need to put following code in the place you want to have that list:

<ul>
<?php
global $post;
$myposts = get_posts('numberposts=10&order=DESC&orderby=post_date&category=xx');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li>
<?php endforeach; ?>
</ul>

xx need to be replaced with ID of that particular category.
If you want to display also content of those posts just add this
--- <?php the_content(); ?>
between </a> and </li> . And done ;).

All authors/users listed

Basically there are two kind of lists of all authors you may want to display.
1. Rather short one you can place in the sidebar for example.
It uses function <?php wp_list_authors(); ?> and following arguments are available:

  • optioncount: display number of published posts (1=true, 0=false)
  • exclude_admin
  • show_fullname; if true first and last name is displayed, if false ‘nickname’ is displayed
  • hide_empty; don’t display authors without posts

The most often I use
<?php wp_list_authors('show_fullname=1&optioncount=1'); ?> which shows full name and number of posts written by each author.

2. The second option is list of all authors/users displayed on separate page with additional info on them. I use for it page template created by Kaf Oseo. I have to admit it’s not only written very well but it’s also very easy to customize because author put comment on each piece of code that can be change adjust it to your needs.
Continue Reading »

Particular post displayed in a sidebar

Agrhhh.. :furious_tb: It drives me crazy when I need to search through few websites I’ve been working on to find the one with some solution I need again for other project. Thus I’ve made decision. I’m going to publish here all kind of tricks(?)/codes and so on I’ve used. That will serve two purposes:

  1. gather all of them in one place
  2. present solutions of problems you have also encountered and stuck with ;)

I don’t care whether they are simple or complicated – I just don’t want to waste my time on searching them any more.

Here we go…
If you want to display one particular post in the sidebar you need to insert in the sidebar.php file following code:

<?php query_posts('p=xx');
global $more;
$more = 0;
while (have_posts()) : the_post();
the_content();
endwhile; ?>

Where xx is ID of post that is supposed to be displayed.
This can be also used to display page in the sidebar. Just replace p=xx with page_id=xx :) .

Text box on each page

To have a text box (or image :happy_tb: ) displayed on top of each page (apart from posts in single post view) but above header I have added following piece of code to index.php, page.php and archives.php just bellow <div id="content"> [that’s div which starts content of the page]:

<div id="introtext">
<?php echo 'This message is supposed to show up just under header on each page'; ?></div>

Then I have added to style.css:
#introtext {
padding: 5px;
margin: 10px 3px 0 3px;
background-color: #990000;
color: #ffffff;
text-align: center;
clear: both;
}
I’ve used clear:both; to make sure sidebar won’t drop down the page.
And that’s final result :).
introtext
(click on image to enlarge)

Main Page – one post plus list of titles of other posts

I realize that problem was discussed many times on WP forum and few solutions’ were presented. The one I will describe here worked perfect for me. Actually the only one reason I publish it here is I want keep it in some visible place so I can use it whenever I need it ;).
Changes concern index.php file.
Here is original piece of code of Nigarila theme I use on this site:

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>
<div class="post">
<h2 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>"><?php the_title(); ?></a></h2>
<p class="post-info"><em class="user"><?php the_author_posts_link() ?></em> on <em><?php the_time('M d Y'); ?></em> | Filed under: <em><?php the_category(', ') ?></em> <?php edit_post_link(); ?></p>
<div class="post-content">
<?php the_content('<span style="color: #990000">Continue Reading &#187;</span>'); ?>
<?php wp_link_pages(); ?>
<p class="post-footer">
<?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>

First I’ve replaced

<?php if ($posts) : foreach ($posts as $post) : start_wp(); ?>

with
Continue Reading »

Random image in NextGEN but without widget ;)

My favorite plugin for images is NextGEN ( I have already written about it). There was only one small item missing – possibility of displaying random images in the sidebar. Actually such option is offered by plugin author but as a widget and I don’t use them. What to do then?
I have had a closer look at both nggwidget.php (to find out how it works) and nggfunctions.php (to check out how functions are created). Hmm… that looked pretty clean and clear so I decided to give it a try.
From nggwidget.php I’ve copied all lines responsible for display and insert of random and recent images (lines 171 to 272) and pasted them to nggfunctions.php file just above // Global function.
Then in the sidebar I’ve put in the chosen place following function
<?php nggDisplayRandomImages(x,y,z);?>
where x is number of images to display, y = width of thumbnail, z = height of thumb. I had to also change a little nggallery.css (part commented as /* –Sidebar widget–*/ ) to have random images the same style as others and… that’s all.
Works like a charm – I have random image in the sidebar without widget :thumbup_tb: .
UPDATE: Sometimes I do too much ;). Accidently I’ve discovered you can just activate random images widget under plugin tab in your admin panel and then put in the sidebar code I’ve written above…

How to create tag.php for UTW

Few times I was asked how EXACTLY create file to display archive for posts with given tag. I have to admit that information given on official UTW site are rather… short ;) so here is little bit longer explanation.
I have copied content of archive.php file to Notepad. Then I removed all conditionals specifying what kind of archive it can be. I’ve replaced them with following piece of code:

<h2 class="post-title">Archive for posts tagged with
<?php if (is_tag()) UTW_ShowCurrentTagSet("tagsetcommalist"); ?>
</h2>

Since there is only one condition I had to also remove character } from first line right below that code. Then I saved file as tag.php and put it on server to theme folder. And that’s all.

You can check how does it work choosing any tag either in the post or in the sidebar.
And below there is complete tag file (saved as *.txt) for you to download.

  tag (1.4 KiB)

NOTE! It’s a file for Nigarila theme I use on this site. If you use other theme you will have to change part that states how post are being displayed. Have fun!

PS. I do realize that tomorrow new version of WP (2.3) with built in tags shows up. But.. not everyone is going to upgrade immediately. Moreover Christine (UTW author) is working on new version of her plugin which will work with new WP so.. you may find my mini ‘tutorial’ useful anyway :happy_tb: .

How to create Image Map using free tools

There are three basic shapes of maps: rectangle, circle and polygon. For each of them you need different coordinates to outline area within which link will be embedded.
Below image points coordinate you need to find out:

rectangle circle polygon

img.png

How to do it with GIMP?
screenshot of GIMP windowWhen you hover over image in GIMP in the bottom left corner show up coordinates for present place of cursor (please, click on thumbnail to zoom it out). That’s all you need to know. But how to find out circle radius? No problem; while drawing circle using GIMP at the bottom of window you get information on height and width of area you are selecting; radius is half of that number :wink_tb: . I’ve selected area 60 x 60 px so radius of my circle is 30 px.

Continue Reading »

Page 2 of 6«123456»