Archive for the ‘WordPress Logic’ Category

All posts in WordPress Logic category.

HOWTO: Make WordPress Titles Title Case

0

Posted by: Jennifer Zelazny on November 18, 2011

Categorized: HOWTOs, WordPress Logic

Tagged: ,

Ever need to make sure all your titles showed up with using “Title Case” and not just by using CSS? By adding the following to your functions.php file in your theme – you can ensure the post title will have the case you want:

/**
 * Custom Title Case for the_title()
 * @returns $content (title with title case)
 */
add_filter( 'the_title', 'sdac_use_title_case' );
add_filter( 'wp_title', 'sdac_use_title_case' );
function sdac_use_title_case( $content ) {
	$content = ucwords( $content );
	return $content;
}

Change WordPress Author Permalink

0

Posted by: Jennifer Zelazny on October 19, 2011

Categorized: WordPress, WordPress Logic

Tagged: ,

Ever want to change the “author” permalink from something like /author/jzelazny to something more custom for your CMS like /teacher/jzelazny? This is relatively easy by adding a function to your functions.php file:

// Adjust your author permalink
add_action( 'init', 'custom_theme_init' );
function custom_theme_init() {
   global $wp_rewrite;
   // Change the value of the author permalink to teacher
   $wp_rewrite->author_base = 'teacher';
}

Flush your permalinks (Settings > Permalinks) and now all your /author/ pages will now be /teacher/.

Using meta_query With WordPress 3.1+

0

Posted by: Jennifer Zelazny on April 14, 2011

Categorized: SDAC Blog, WordPress, WordPress Logic

Tagged: ,

With WordPress 3.1 – we now have the ability to use “meta_query” to show posts associated with a certain custom field. I recently used this in order to create an events listing widget for a client. I needed to query posts in the “Events” category and that had a time stamp defined (which happened to be the start date of the event). The client wanted to show only current/future events (today or later) and wanted to show them in order by the closest event date to the furthest away.

Getting the general query together was a snap – but the orderby did not work unless I had the meta_key defined (this is documented but was overlooked initially).

Example

$now = time();
$args = array(
	'category_name' => 'Events',
	'meta_query' => array(
		array(
			'key' => 'sdac_event_time_stamp',
			'value' => $now,
			'compare' => '>=',
			'type' => 'NUMERIC'
		)
	),
	'meta_key' => 'sdac_event_time_stamp',
	'order' => 'ASC',
	'orderby' => 'meta_value_num'
 );
$events_query = new WP_Query( $args);

If you take a look at the query itself – the trick was to capture the time now ($now) and then use the compare within the meta_query. Overall – this sort of query makes working with custom fields a lot easier. If you have not checked it out yet – take a look.

Documentation
Function Reference/WP Query

WordPress Taxonomy Query (Keep it Simple)

0

Posted by: Jennifer Zelazny on February 24, 2011

Categorized: WordPress, WordPress Logic

Tagged: , ,

Now that a number of people are using more and more custom taxonomies for WordPress – custom taxonomy queries are something becoming necessary.

Example: You have a WordPress site set up to show/sell products (in this example – you sell books and posters). You have a custom taxonomy called “catalog” that you use to organize all the products.

If you want to show all items that are marked “posters” in your custom catalog taxonomy you can simply use this query:

query_posts( array( 'catalog' => 'posters' ) );

Of course – you can create more complicated custom taxonomy queries, but this will get you started. By using custom taxonomies (and custom post types) – you can really open the door to creating complex content management systems (and/or really cool sites that need something more than your standard categories/tags/posts/pages).

Further documentation: http://codex.wordpress.org/Function_Reference/query_posts

Remember – keep it simple!

Change the_excerpt Length and Ending

0

Posted by: Jennifer Zelazny on January 27, 2011

Categorized: HOWTOs, SDAC Blog, WordPress, WordPress Logic

Tagged:

Using WordPress – there are some times when you want to use the_excerpt but you do not want to show the default 55 words and/or show the [...] after those 55 words.

Change the Length

Instead of only showing the default 55 words where you want to change it to something longer or shorter. To change this – just add/modify this in your functions.php file:

add_filter('excerpt_length', 'sdac_excerpt_length');
function sdac_excerpt_length( $length ) {
	return 75;
}

In that code example – I am setting the length to be 75 words.

Change the Ending

Instead of showing the default [...] after the excerpt – you can omit that or change it. To change this – just add/modify this in your functions.php file:

add_filter('excerpt_more', 'jappler_excerpt_more');
function jappler_excerpt_more( $more ) {
        global $post;
	return '... <a>ID ).'"&gt;Read More &raquo;</a>';
}

In that example – I added a read more which links to the post permalink. (This filter is available with WordPress 2.9+)

Hopefully these examples will help a little bit the next time you want to use the excerpt but wished it was easy to change the output.

HOWTO: Add a Custom Comment Class to WordPress

1

Posted by: Jennifer Zelazny on November 30, 2009

Categorized: SDAC Blog, WordPress, WordPress Logic

Tagged: , ,

Ever need to add your own class to your WordPress comment template but not sure how because of the function comment_class that controls the classes? Check out the function comment_class and you will see adding your own custom classes very easily.

Allow this sounds pretty basic – it is a lifesaver for doing a lot of customizations.

Conditionally Show Only On Home Page (Front Page)

3

There a are two functions that are used quite a bit to conditionally show content on the very front page (home page or page 1 of your blog). First – we had the function is_home(), but in WordPress version 2.5 – a new function was introduced: is_front_page().

You might think both would conditionally only display content on the home page (front page, page 1 of your blog) – but you actually need to use the following code to accomplish showing something ONLY on the front page of your paged blog:

<p>Show this text only on the home page</p>

So – if you want to easily show something on what I would consider the front page – use the code above to successfully achieve that. (This is particularly helpful if you have ads that run speficically on the home page and others that are ROS (run of site).

Further reading:

Get Top Parent Category (WordPress)

44

Posted by: Jennifer Zelazny on January 15, 2009

Categorized: Clients, HOWTOs, SDAC Blog, WordPress Logic

Tagged: , ,

There are many times when you need to show or get the top most (root) parent category in WordPress – regardless of how many subcategories you might be deep. I have used this logic for page navigation (highlight the top parent tab) – as well as within some custom loops/sidebar code.

They way to do this:

// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
$sdacReplace = array(" " => "-", "(" => "", ")" => "");
$topParent = strtolower(strtr($topParentName,$sdacReplace));

To test this you can simply put it in your header and echo out $topParent and you will see the “slug” of the category.
If you want to see the category name and not necessarily the slug – you can simply echo $topParentName.

Show One WordPress Post

6

Posted by: Jennifer Zelazny on November 4, 2008

Categorized: HOWTOs, SDAC Blog, WordPress, WordPress Logic

Tagged: ,

There is a great function you might be aware of that allows you to get one WordPress post and display it anywhere you would like (header, sidebar, home page, etc).

To show only one post, use the function get_post.

Example:

post_title;
$sdac_content = $show_post_id-&gt;post_content;
?&gt;
<h2></h2>

The code above will show the title and content of the post with the post ID of 10. If you are not sure what your post ID is, go to the manage screen in the WP-Admin, and hover over the post title. In your browser status bar, you will see the URL – and the post ID is the number at the end of the URL.

Show Only Category (Not Subcategory) Content

11

Posted by: Jennifer Zelazny on June 17, 2008

Categorized: WordPress, WordPress Logic

Tagged: , , ,

If you ever wanted to only show the content of the category you were on in the category view of WordPress (instead of the category you are on plus all of it’s subcategories – you can easily do so by adding the code below to the template that is used to show your category content

You would start your loop with this:

 

Put your title, content functions here

and where you normally see the call, put to close the additional (in_category) if statement.

This code gets the current category and then only displays categories that are in that category (not sub category)