Premium WP Themes Code SnippetsTipsWP ThemesXOXO News
April 1, 2012
Code Snippets

Here, we discussed how to automatically create a WordPress Category. Now we’ll extend it a bit further by showing you how to create a WordPress page. We use this in our themes that have a static home page. First we create the necessary ‘Blog’ and ‘Home’ pages, then we tell WordPress to use a static home page.

So onto the code:

First you want to see if the page you are creating already exists:

If it doesn’t the next thing to do is create it:

All of this goes in your functions.php file, but we don’t want to do this every time functions.php is loaded, so we wrap this in a function, and also only execute this when we’re in the admin panel.

putting it all together, we have this:

Any questions? Ask us in the comments!

Share
March 31, 2012
Code Snippets

If you’re going to use WordPress as a CMS, then you’ll definitely want to display all of the pages in your blog. Just displaying all of the pages is really quite simple.

To do this, you want to get all of the pages, and then for each one, print out a link to it. Like this:

That’s the basics of it. Once you get that going, you’ll probably want to add some styling. Also, if you want to use drop down menus, then you’ll want to create a page hierarchy. But we’ll cover that in a later post.

Happy Blogging!

Share
March 29, 2012
Code Snippets

If you’re making a portfolio theme, you probably want there to be a category called “Gallery”. You could ask your customers to create this manually, or your theme could create this category automatically for them. Sweet! the less installation steps the better! It’s an advanced feature, but it’s really just a few lines of code.

First we’ll go through each step and then at the end, we’ll put it all together. Also all of this goes in the functions.php file

First you want to only execute this code on the admin pages, so begin with this

Next you want to check if the Gallery category already exists.

If it doesn’t exist, you want to create it. Here, we’re giving it a very short description, “All Galleries”

Finally, wrap this all in a function and add tell wordpress to call this function after theme setup using the add action hook.

Putting it all together we have the following

That’s it! You can also create pages, and subcategories.

Share