Changing Arguments For The Built-In Taxonomies In WordPress

With the following snippet you can change arguments for the built-in taxonomies in WordPress:

To modify arguments for the built-in taxonomies might not be a good idea unless you really know what you’re doing. I’m doing this because (not sure since when) now WordPress won’t let me display links from the link_category taxonomy (remember Links Manager?) on its own template page (taxonomy-link_category.php).

One symptom I’ve noticed is that when I visited the link_category term page (like visiting http://SITE.COM/?link_category=links), it showed the index page template but not 404 page, which meant such route did exist just the data couldn’t be shown public.

After some time-wasting trial and error I realized it’s the default arguments keep the template from working. And the snippet above did the trick perfectly.

Hope it could save someone thirty or so minutes someday in the future. 😉

Switch To Another Theme On Certain Pages In WordPress

I wrote a tiny WordPress plugin to switch themes between certain pages and the rest ones of a site. Check the gist out:

Here’s my brief explanation:

  1. Line 15-19: We need to hook to these two filters (stylesheet and template) to let WordPress know we’d like to use another theme other than the one has been set. I’d recommend we do such calls in setup_theme action. Maybe you could use other actions instead, if you find one, just let me know.
  2. Line 21-28: This is our filter function to change stylesheet WordPress would use. If you’re going to use a child theme like me, set your child theme here, or just use your theme.
    And the most important part is how I tell WordPress to switch themes on “certain pages”. I specify them by checking the $_SERVER['REQUEST_URI'] param.
  3. Line 30-37: Basically works like the previous one, just it changes the template (parent theme).

The use case of this plugin is that for a client site I built and maintain, I need to enable a survey form which will be built by Gravity Forms. Though it won’t be too difficult to build such form, I felt lazy to write some more CSS to style it. I was just wondering, it would be so much less work for me to do if I can use the default WordPress theme to display the form.

And after some googling, it’s done! I hope it could help you with your needs too.

 

Parent Post From Another Post Type And A New URL Structure In WordPress

Hello, beautiful readers! In this post we are going to solve a very specific need for custom post types in WordPress. I’ve actually written about it two years ago, which is to select post parent from another post type. But today you’ll learn more deeply on the same topic with a live example.

Say there’s a project for building online courses website, we plan to have two custom post types in WordPress, which are “Courses” and “Lessons”. Registering two CPTs won’t be a big deal for us but the not-so-easy part is we’d like to set the URL structure of the lessons in such format:

http://my-online-course.com/lesson/[course-name]/[lesson-name]/

Taking my AngularJS series for example, it would look like:

http://my-online-course.com/lesson/angularjs-wp-api/lesson-1-using-angularjs-in-wp-theme/

So the workflow to manage our online course would be like, we always add a new course first, input some course information, prerequisites, curriculum etc. After that we’ll create as many lessons as needed, and for each lesson, we have to set the “parent post” for it, which is the course it’s attached to.

If you find this use case compelling, keep reading and here’s what I’m going to show you (preview the final demo if you’d like):

  1. Registering two custom post types: Courses and Lessons.
  2. Updating the “Parent” meta box so we can choose a “course” as the parent post for a “lesson”.
  3. Setting the exactly URL structure we want.
  4. Updating the permalink of the CPT to reflect the new URL structure.

Ready? Let’s go! Read More…