Appending the title of the post in “Read more” links for WordPress

One of the guidelines in usability is that you should provide only one way that will lead them to a certain action. For example, if someone sees 2 option buttons, chances are that they might think that each option button does separate tasks when in fact their functions are the same. Also, we have to make sure that the links are descriptive enough and if not, then we should provide a title for the link so when a user hovers over it then they’ll know where the link will take them.

On a similar note, accessibility advocates encourages people to use unique name for every link that we use on a page (kinda like what I was saying before only have a different reason), meaning countless “Read more of this entry” is not really the best practice because you’re using the same text over and over and these texts are leading the user to different pages.

I was looking for a way to add that in <?php the_content(); ?> tag in wordpress before but I couldn’t do it. What I did then was append the the_title(); within the_content(); so it looked like this —

<?php the_content('Read more of ' . the_title() . ' & raquo;' ?>

The problem though is that it does echo the “Read more of” but the title is no where in sight! I was so ready to find a way to make it show by hacking away through function.php, thankfully I remembered to check the_content page in the codex first.

What I should’ve done in the first place is to add parameters to the the_title() tag so it will show like this —

<?php the_content('Read more of ' . the_title('', '', false)) ?>

This follows the same parameter as the the_title(); template tag.

Parameters

before
(string) Text to place before the title. Defaults to ”.

after
(string) Text to place after the title. Defaults to ”.

display
(Boolean) Display the title (TRUE) or return it for use in PHP (FALSE). Defaults to TRUE.

So you may use it like this:

<?php the_content('Read more of ' . the_title('&ldquo ;', '&rdquo ; » ;', false)) ?>

So it will echo as Read more of “Title of the Post” ».

When ‘is_home()’ doesn’t work

Note: This has been written a long time ago, some of the stuff here might not work anymore. I’d recommend using <?php is_frontpage(); > when using static frontpage instead. You can also visit the WordPress Codex for more information or contact me and I’ll try my best to help you.

You hacked your WordPress homepage because you don’t intend to make a blog out of it and then all of a sudden the very useful conditional tag <?php if(is_home()) ?> no longer works. It happened to me twice and for that 2 occurrences, I couldn’t find a solution until a couple of days ago.

I’m working on this project where I don’t need to show my posts anywhere aside from the category archive and single, not even in the homepage. What the client wants is that the home page should show all of the categories in the site, no matter if it’s empty or not and a certain page content must be pulled into it. Because I’m lazy in creating a page template just for that and assigning that page as the home page, I modified the main index template instead.

Those things are actually easy to do. All I have to do is to write this within the main index template:

<?php wp_list_categories('order_by=name&hide_empty=0&title_li='); ?>

so it’ll show all of the categories ordered by the category name whether it has posts or not. And for the page to be pulled into the main index, query_post is the answer:

<?php query_posts('page_id=ID'); while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); endwhile; ?>

so it’ll show the title and the contents of the page ID.

It worked just as expected and I was so proud of myself until the client asked that the sidebar for the home page be different from the rest of the site. That’s when I knew that the is_home() doesn’t work.

I searched high and low for in the codex and support forum but couldn’t find anything. I can’t tell the client that I can’t fix this so I tried my hunch if it works. I’m not the type who is a big fan of plugins because I’m afraid that if the site rely heavily on plugins and and if and when that certain plugins’s developer goes AWOL, the site might go berserk due to incompatibility and all I could do was search again for a plugin that could replace it.

I thought that the home page is being treated by wordpress as if it’s a page because I did pull the contents of a page into it. Turns out that I was right!

When I put <?php if(is_page('ID')) { ?> in the sidebar, it worked just as I wanted it to.

So now, I realized that once you’ve changed the content of your main index template, know that you are also taking out it being your “home” because it will start acting like a page (if you pulled a page into it).