• markwill

    (@markwill)


    I am having a bit of a problem implementing a PAGE (not a post) that is a child of a category, in terms of it’s URL. So, for example, I need to have a URL of the form <categoryname>/<pagename>. The URL is “non-negotiable” for various business reasons.

    When I create a page I have the option to point to a parent PAGE but, as far as can tell, I can’t have it as a child of a category.

    Is this possible?

Viewing 4 replies - 1 through 4 (of 4 total)
  • John Parris

    (@mindctrl)

    Hi,

    Pages can’t be assigned to categories by default. Posts can.

    So, if you’re using pages, you set up a parent page called, for example, Checkout. Then you create another page and set the Parent page in the Page Attributes box. http://cl.ly/image/13400V3s3P0M

    This would give you a URL of /checkout/pagename.

    Thread Starter markwill

    (@markwill)

    Thank you for the response. I am familiar with how to create a parent/child page setup and have used that in a number of situations. The challenge I have here is down to two requirements (using /<categoryname>/<pagename> as the example):

    For SEO purposes <categoryname> is a category and will have many posts under that category.

    Unfortunately, the <pagename> content must, for business reasons, be child of the category, in terms of URL structure.

    So, it seems from your comments that I have a big problem on my hands. I would love to hear of any creative solutions to this but I have one thought.

    The current <pagename> content is implemented as a page (it’s temporarily under another page). What I could do, I guess, is somehow move this to be a POST (not a page), have it under the category but have it use some template that is intended for just that specific post (all the other posts under the category have a totally different layout).

    Is that possibly a solution? I am not familiar with what constraints I might have if I move a page to a post.

    Thanks again.

    Mark

    John Parris

    (@mindctrl)

    If you set it up as a post, you can have the URL structure contain the category by going to Settings > Permalinks and setting the custom structure to %category%/%postname%/.

    Doing this though, you’ll want to only assign posts to one category to ensure the correct one gets used.

    But, you’re left with the issue of using a specific template for that particular post. You can do this with a bit of custom code.

    First, you need the post ID of this special post. You can get that by editing the post in the admin and looking at the post ID in the URL. http://cl.ly/image/0i1e3n0T293t Note the post=744. In this case, my post ID is 744.

    Armed with this, you can create a template that’s made especially for that post. Normally a single post is displayed by the single.php template file. If you have another template, make a copy of it and name it single-744.php (replace 744 with your post ID).

    Use this code to override the template for that special post.

    function jp_single_template_id( $located_template ) {
    	$single_template = locate_template( array( sprintf( "single-%d.php", absint( get_the_ID() ) ), $located_template ) );
    	if( ! empty( $single_template ) ) {
    		return $single_template;
    	} else {
    		return $located_template;
    	}
    }
    add_filter( 'single_template', 'jp_single_template_id' );
    

    This code will load that template for just that post, and fallback to the default template for all other posts.

    Thread Starter markwill

    (@markwill)

    Thank you, mindctrl. I very much appreciate this.

    Mark

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding a page as a child of a category’ is closed to new replies.