0

I've been reading the theme developer handbook for a bit and maybe I ended up with one too many questions on wordpress theme development. I haven't done any theme/php development in a very long time coming from C#/Java background.

I guess I just want to have a clear view before I start doing things in wordpress and this is what I wanted to do:

  • Create a page for main site and set it as main and only query a category
    • I imagine the functions of a theme acts like a controller for the pre/post filters/hooks?
  • Create another page but only allow certain custom post-types
    • does it all go through functions.php of a theme?

My apologies if this isn't the place just wanted to get a better grasp. Let me know if you need more clarification in the comments. I'm not really trying to do anything fancy with the themes, just want to have control of how I want things to be laid out before I do them.

1

Is functions.php in themes applied to all templates?

The theme's functions.php is loaded on every request, yes.

I imagine the functions of a theme acts like a controller for the pre/post filters/hooks?

A theme's functions.php is just an entrypoint for a theme to run any PHP code it wants. You can see when in the loading sequence it's loaded here. It doesn't have a formalised structure, but it typically just contains code that defines functions and hooks them. I suggest looking at the default themes to see a pretty standard example.

Regarding the things you're tying to do:

Create a page for main site and set it as main and only query a category

I don't know what you mean by "main" here. I'm assuming you mean the front page. If you want your front page to be a list of posts from a particular category, then in Settings > Reading set your front page to be the latest posts. The theme will choose which template to use to render these posts based on the template hierarchy and what's available in your theme.

If you want to change this list of latest posts on your homepage to only include posts of a particular category, then you would use the pre_get_posts hook to modify the default query to only query a particular category. For example:

add_action(
    'pre_get_posts',
    function( WP_Query $query ) {
        if ( ! is_admin() && $query->is_home() ) {
            $query->set( 'category_name', 'news' ); // Replace news with the slug of the category you want to use.
        }
    }
);

The full list of query properties you can add, change, or remove is documented here.

Create another page but only allow certain custom post-types

I'm not clear on what this would be, but it would be more complicated to do it properly. I suggest posting this as it's own question with more detail once you've got a handle on the above.

1
  • Thanks mate, by main yes I meant the front page but I guess I'll do some more homework on hooks and checkout the standard functions.php of a default theme. As for custom post-types it's a researching thing I don't really know if I need them but I'll do a new post on it if it ever comes to that. – allenskd Jul 10 '20 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.