WordPress.org

Ready to get started?Download WordPress

Forums

Plugin attempt: singleChildRedirect (3 posts)

  1. hquadrat
    Member
    Posted 3 years ago #

    Hi all,

    I'm trying to achieve a simple behaviour: when a category has only one post assigned, then when the category is clicked, please WordPress, directly show this post (indestead of bugging me with an category overview with only one post listed).

    # Pseudo code

    //check how many post are in a category
    query = "Select COUNT from CATEGORIES have articles"
    
    if (count == 1) {
        //check the post ID
        query2 = "Select the above postID"
        //and redirect
        wordpressRedirect(postID)
    }

    It might become 15 lines in total but not far more I think.

    # Where

    The most difficult question for me is: where to place my code and which hook to use (as far as I understood, hooks are specific points during runtime when WordPress renders a page and I can add my behaviour at those hooks)?
    hooks documentation

    # Why

    This behaviour would be very helpful setting up WordPress as a CMS: you could build the whole navigation up on categories (and not on pages as seen often) and whenever the category is clicked you don't see an article listing with one article but directly get redirected there.

    Any help would be apreciated.

    Rgds,

    Henning

  2. hquadrat
    Member
    Posted 3 years ago #

    I dug a little deeper in the docs: plugins_loaded hook seems a good entry point.

    Can anyone give a comment on that? Especially regarding the fact that for the successful execution of my plugin WordPress needs to know a) if a category at all and b) which category should be displayed.

    Thx.

  3. hquadrat
    Member
    Posted 3 years ago #

    Ah, that sounds a little too early, maybe init or wp_loaded fit better? I'll try with init.

    Even I'll do some tests, any expert help is appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic