Saturday, August 9, 2014

Wordpress: A better way to create short texts

I created a shorttext functionality for my personal use to ease my job based on how I solved this in my own CMS. You can read the original post here.

Using the CCTM plugin is not easy however and after counseling a colleague I started looking for an easier solution.

By using 2 these plugins, custom fields can be added much easier:

With the first one you create your custom post types. In my case it was names Shorttexts, but this could be Books, Movies or anything you need it for.

With the second plugin you create a group of custom fields that you attach to your custom post type and create the needed custom fields in the group. In my case I created a Link and a Link text custom field in my Shorttext rules group and attached it to the Shorttexts post type. Now when creating or modifying Shorttexts entries, I can insert a link and text for the link too.

Inserting the data into the html/php source code by post ID is really easy. For the default post_title and post_content fields I created 2 helper functions in the theme's functions.php file:

 function getPostTitle($id) {  
      return get_the_title($id);  
 function getPostContent($id) {  
      $post_object = get_post($id);    
      return do_shortcode( $post_object->post_content );  

Displaying the custom fields is really easy to as it is described in the plugins documentation:

In my case for example to show the link I added to the shorttext all I need is this:

 <a href="<?php the_field('link', $post_id); ?>"><?php the_field('link_text', $post_id); ?></a>  

Complementary cat picture:

No comments:

Post a Comment