Nothing To Lose

If you don’t have it, how can you lose it!
Subscribe

Archive for the ‘Web Development’

Convert lines of text to HTML list item – using regex

March 21, 2011 By: Dexter Category: HTML, Regular Expressions, Web Development

Many a times I have to update pages where I need to display contents as HTML lists. The problems is the contents sent to me is in n number of lines of plain text.

Generally you will end up copy pasting <li> and </li> at the beginning and end of every line, for few lines it is ok, but what about if you have around 100 lines.

Well well well… if you knew regular expressions you life will be saved, here is what you do –

  • Copy paste all text in a text editor that supports find and replace with regular expression support.

This is line one
This is line two
This is line three
This is line four
This is line five
This is line ….
…………………….
…………………….
This is line fifty

  • open the replace dialogue box fill the following (make sure you have selected the regular-expression option for find)
    • find text : ^     (yup the carat symbol)
    • replace with : <li>
    • Hit replace all — all your lines should now be showing <li> in front of it.

<li>This is line one
<li>This is line two
<li>This is line three
<li>This is line four
<li>This is line five
<li>This is line ….
<li>…………………….
<li>…………………….
<li>This is line fifty

  • Open the replace dialogue box again and repeat the same steps again only:
    • find text now should be: $
    • replace with : </li>

<li>This is line one</li>
<li>This is line two</li>
<li>This is line three</li>
<li>This is line four</li>
<li>This is line five</li>
<li>This is line ….</li>
<li>…………………….</li>
<li>…………………….</li>
<li>This is line fifty</li>

  • Do forget to add unordered list or ordered list  tags <ul> or <ol> at the first line and </ul> or </ol> respectively as the last line to complete the HTML list.

<ul>

<li>This is line one</li>
<li>This is line two</li>
<li>This is line three</li>
<li>This is line four</li>
<li>This is line five</li>
<li>This is line ….</li>
<li>…………………….</li>
<li>…………………….</li>
<li>This is line fifty</li>

</ul>

And in three steps your complete text is now  a HTML list.

Getting page and subpage and parent(s) name in title bar with wordpress.

August 20, 2009 By: Dexter Category: Blog, PHP, Tutorial, Web Development, Wordpress

Had been working with wordpress (2.8.1) lately for some web development work. Now there were some section of the site which are sub pages of some parent page again which are sub pages of some category.

lets say you have we have a scenario like this
Products is the main category. Then it has two sub categories say Laptops and Desktops
And Laptops has sub categories 14 inches, 17 inches
Desktops have sub categories Home, Office, Game Station.

When the user browses to a page say Game Station you want to display on the title bar:

Website Name » Products
Website Name » Products » Desktops
Website Name » Products » Desktops » Game Station
or
Website Name » Products » Laptops
etc
I will assume that you already know how to have pages arranged in categories and subcategories in wordpress.

What we need is to do is use few available inbuilt functions of wordpress

1. get_post_ancestors() Read more..

this one get the ancestor of the given page/post id

2. the_ID() Read More..

this get you the current post id

3. get_post() Read More

gets the info of a given post id. Returns an object.

4. the_title()

returns the name of current page.

So here is the code.

Open the header.php (from the selected theme)

and add the following between the <title> and </title>

<title> Website Name: &raquo
<?php
$postsarray = (get_post_ancestors(the_ID()));
krsort($postsarray);
foreach($postsarray as $key=>$postid)
{
$post_ids = get_post($postid);
$title = $post_ids->post_title;
echo “$title &raquo “;
}
the_title();
?>
</title>

This will generate the title with the page and parent categories automatically.

Here is what is happening.

You can directly write you website name i.e hard code it, line

$postsarray = (get_post_ancestors(the_ID()));

get_post_ancestors() returns an indexed array containing the list of all the parent categories. Say if the current page is ‘Game Station’ then my array $postarray will be link (0=>’127′, 1=>’140′). We have used the function the_ID() inside the get_post_ancestors() so that the page id is automatically given accordingly to the current page. Note that the array contains the post id and not the name.

krsort($postsarray);

Since the array is from top category to lower we reverse sort it on the keys.

foreach($postsarray as $postid)
{
$post_ids = get_post($postid);
$title = $post_ids->post_title;
echo “$title &raquo “;
}

The foreach loop picks every post id which we give to a function get_post(), which returns a object. Here the object is in $post_ids. The object contains loads of info about the post, but we are only interested in the title. So $post_ids->post_title; returns the name of the currently created objects. We store this in $tilte;

echo “$title &raquo “;

This line get the categories displayed. Of course parent first.

the_title();

It is used here since the above functionality only returns the ancestors, so to display the name of the current page we use the_title() function.

If you want to display the name of you blog name instead of you website. replace ‘Website Name’ with <?php bloginfo(‘name’) ?>
Well that is it for now.

Missing template option in WordPress

August 02, 2009 By: Dexter Category: Web Development, Wordpress

Had been working on WordPress 2.8.1 for a friends website. Had created some different templates for pages. It was working fine for quite some time. Added a new page and suddenly noticed that the template option was missing… zoop… woosh….  vanised…

Searched on the net, could not find and proper reason for the same, This page gives you some idea what ways it can be resolved. http://wordpress.org/support/topic/190138?replies=18

I just went to themes section and (re)activated the same theme again, and magically the  template option was available again.

I still do not have a clue what was wrong. But it worked.

whatever….

there is nothing to lose.