Posts Tagged ‘wordpress’

Twitter Widget Pro and Jetpack Twitter widget can’t live together

Posted on: 21st February 2012 No Comments

I installed Jetpack for WordPress on my blog today, mainly because of the stats module, but also because I was considering changing the twitter widget I use. I have used Twitter Widget Pro for a while, and the reason for the possible change was mainly that the output was a bit clogged, since it outputted the twitter app the message was composed in, as well as the “normal” message output. The Jetpack twitter widget only outputs how long since the tweets were posted, all I wanted, so it seemed like a good and lazy switch, since it became available when installing Jetpack.

But did it really? Oh, no! The Jetpack twitter widget was missing in action from the widgets page on my WordPress installation. The needed files was physically (as physically a file can be, anyway) on the server, so a long series of troubleshooting followed. This included deactivating/re-sactiavting Jetpack, deleting and re-installing Jetpack, installing Jetpack manually, and so forth.

No Jetpack twitter widget for you

No Jetpack twitter widget for you!

It was not until I deactivated Twitter Widget Pro that the missing twitter widget showed itself. I don’t know the link between these two, but when deactivating Twitter Widget Pro, the Jetpack twitter widget automatically took it’s place in the right sidebar. So they are most likely sharing something they shouldn’t.

Hey, presto! A widget comes home.

Hey, presto! A widget comes home.

But in the end problem solved, and widget changed.

Breadcrumbs on archive pages in WordPress

Posted on: 21st February 2012 No Comments

When I was editing a WordPress archive page the other day, I decided that I wanted to list the categories and sub categories together with the archive result (the title with link), but without the links to the specific category pages. Some of the links on the archive page would have just one category, and some would have more, depending on the post. This is on a page that uses  WordPress as a CMS, not just for blogging.

The solution I came up with, used the wp_get_object_terms function, that I earlier had used together with a custom taxonomy query.  The categories are stored in the term ‘category’. To make sure the categories and sub categories was displayed in the right order (parent before sub), the output had to be ordered ascending (as the parent categories on this specific page all has a lover ID than the sub categories). And I used : as a separator between the categories and post title.

All entries included title, link, last change date, and the excerpt. The code ended up something like this:

<?php while (have_posts()) : the_post(); ?>
 <div <?php post_class() ?>>
 <h2 id="post-<?php the_ID(); ?>">
 <?php
 $args = array('orderby' => 'ID', 'order' => 'ASC', 'fields' => 'all');
 $category_terms = wp_get_object_terms($post->ID, 'category', $args);
 if(!empty($category_terms)){
 if(!is_wp_error( $category_terms )){
 foreach($category_terms as $term){
 echo $term->name.' : ';
 }
 }
 }
 ?>
 <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
 <p class="changed-archive">Last modified: <?php the_modified_date(); ?></p>
<div class="entry">
 <?php the_excerpt(); ?>
 </div>
  </div>
<?php endwhile; ?>

Which gave this output (in Norwegian, mind):

Archive page with breadcrumb trail

Archive page with breadcrumb trail

I’ve also ended up using this solution on my post pages as well, but then combined with links.

Disappearing page templates thanks to bad taxonomy naming

Posted on: 5th February 2012 No Comments

On one of my other sites, I’ve recently transferred all the old, static content into a WordPress installation, so that I can WordPress as a CMS. The content is divided into five main categories, and for each category I’ve created a custom page template. The main menu links to the category pages, making them “category portals”, so to speak. And it looks good.

But yesterday, WordPress stopped using the custom page templates, and defaulted to index.php on all of my pages. But in the admin area, the pages were still listed as using the templates, and the files were actually still on the web server. I tried to upload the theme once more, without it helping, and also reinstalled WordPress through the Dahboard -> Updates screen without result. It was not until I turned off and on again my custom permalink structure that it went back to normal. My custom structure was /%category%/%postname%/ and choosing default, saving, and choosing custom structure once more, solved it. Or so I believed.

Because this morning, the custom page templates did once again not work. And I had to turn the custom structure off and on again to make it go back. And it was ok for about ten minutes, before defaulting back to the index.php again.

After some time looking angrily at my screen, it dawned on me that it might have something to do with my custom taxonomies. I had set up a taxonomy named year, that i used for the posts. In retrospect, not a very good name for a taxonomy. I had not managed to get any output for this taxonomy yet, and had kind of given up yesterday. Not one before I discovered the defaulting. So I deleted the taxonomy from the functions file, and no the problem seem to be solved.

So the moral of the story has probably something to do with naming conventions. Well, well…

Displaying custom WordPress taxonomy terms with or without links

Posted on: 22nd January 2012 No Comments

WordPress lets you create custom taxonomy terms, a feature that makes WordPress a very good CMS. If you want to display these terms (on a specific post), there are more than one way to do this.

If you want to list the terms associated with a specific post with a link to a term archive, you can use the get_the_term_list() function. This displays the terms (including a term link). To run through the terms, use code similar to this (from the codex):

<?php echo get_the_term_list( $post->ID, 'people', 'People: ', ' ', '' ); ?>

But if you want to display the terms without the links, you can use code similar to this, using wp_get_object_terms():

$people_terms = wp_get_object_terms($post->ID, 'people');
if(!empty($people_terms)){
  if(!is_wp_error( $people_terms )){
    echo '<ul>';
      foreach($people_terms as $term){
      echo '<li>'.$term->name.'</li>';
      }
    echo '</ul>';
  }
}

To display all the terms, regardless of the specific post, you can use the get_terms() function. Code similar to this lists the terms in a unordered list

<?php
$terms = get_terms("people");
 $count = count($terms);
 if ( $count > 0 ){
     echo "<ul>";
     foreach ( $terms as $term ) {
       echo "<li>" . $term->name . "</li>";
     }
     echo "</ul>";
 }
?>

How to display wordpress posts on another page

Posted on: 10th November 2010 No Comments

On my Bond page, I have installed WordPress as a blogging tool in the folder nullnullsju.net/blogg. But I wanted to display the last blogposts on the main frontpage. So how to display WordPress post outside the main blog folder?

To display the post on other pages, you need to first load WordPress’ wp-load.php file into the page in question. And the PHP code is as follow:

<?php
define('WP_USE_THEMES', false);
require('./blogg/wp-load.php');
query_posts('showposts=2');
?>

To explain the lines. The first tells WordPress it shouldn’t use WordPress’ theme. The second imports the file. You obviously need to change this to reflect your installation. And the last line tells WordPress how many posts to display. In this case, it shows two posts.

If you want to display the posts on another site, change the require line to include the whole URL.

So now you have loaded the posts, and now you have to display them. And you can do that by a code similar to this:

<?php while (have_posts()): the_post(); ?>
 <h1><?php the_title(); ?></h1>
 <a href="<?php the_permalink(); ?>">
 <?php the_excerpt(); ?>
 </a>
 <?php endwhile; ?>

You can of course format this as you want. To explain the code:
The first line starts the loop that displays the posts.
The second line displays the post title (in H1 tags).
The third line starts the link with the post’s permalink.
The forth line prints the excerpt (who then becomes the link). The link is closed in the fifth line, and the loop is closed in the last line.

If you want to display the content of the post, you use the_content() in the code.