Forums

[resolved] Permalinks and custom page template question (15 posts)

  1. Harryson
    Member
    Posted 6 months ago #

    Hi,

    I managed to make my permalink look like I want it: http://example.com/news/149/waiting/. Since news/ is a custom page template, that shows all the news aka. posts.

    However, when I go to that permalink, first of all it doesn't make the News link active or use my custom page template!?

    Can I make it so, the permalink works like above, but doesn't use the index.php, but the News page template. And my custom page template catches the $_GET information and works itself out?

    Or, maybe I can have my custom single post template? (However, this means that somehow it still must make my News page link active.)

    Thanks in advance,
    Harryson

  2. Ipstenu
    Half-Elf Support Rogue & Mod
    Posted 6 months ago #

    A ... custom page template? Do you mean a custom post-type?

  3. Harryson
    Member
    Posted 6 months ago #

    Possible, not familiar with the wordpress terms yet. Simply put, I wish to have separate Blog page (meaning, static frontpage) called News, that I have cracked. Now I would like to have the "read more" or "detailed view" News -> Article.

  4. Ipstenu
    Half-Elf Support Rogue & Mod
    Posted 6 months ago #

    AH! Okay, a static front page doesn't add /news/ to your links.

    Change your permalinks to /news/<whatever> and that'll force news into your URL :) It will NOT mess with any pages, JUST your posts.

  5. Harryson
    Member
    Posted 6 months ago #

    Yes, Ive got /news/%post_id%/%postname%/ and separate Home and News page. The News page, uses custom page template [theme_dir]/news.php etc. However, the permalink will force the index.php file as the template.. It ignores the News page template, also it doesn't mark the News in the links <ul> as active..even though its news/ and news/ID/TITLE.

    From my point of view, there should be two options now. Either to have a custom single post template. Or I cannot use permalinks and generate the links myself.. so it would not ignore my news/ and then in the news.php use $_GET information to display the post.

  6. Ipstenu
    Half-Elf Support Rogue & Mod
    Posted 6 months ago #

    No, that's not how it works, but I see why you're confused :)

    Using a TEMPLATE does absolutely nothing to separate your posts. At all. Ever. It's just a design change for a PAGE (not posts, and not 'archives').

    If you want to have one set of posts use /ID/TITLE and another to use /news/ID/TITLE then what you want is to make news into a Custom Post Type, which is separate from your normal posts.

    http://codex.wordpress.org/Post_Types#Custom_Types

  7. Harryson
    Member
    Posted 6 months ago #

    I see. I've got a direction now, thanks. Still not 100% clear yet, but I will let you know what happens :)

  8. Harryson
    Member
    Posted 6 months ago #

    Ok stuck again.

    So I first off created single-news_view.php.

    Then registered the post type:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    	register_post_type( 'news_view',
    		array(
    			'labels' => array(
    				'name' => __( 'News' ),
    				'singular_name' => __( 'News' )
    			),
    			'public' => true,
    			'has_archive' => true,
    			'rewrite' => array('slug' => 'news')
    		)
    	);
    }

    I was hoping that, that's all it takes and it will automatically use that single-news_view.php, but it doesn't. What am I missing?

  9. Ipstenu
    Half-Elf Support Rogue & Mod
    Posted 6 months ago #

    Read http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress for nitty gritty on CPTs

    Bear in mine, the template hierarchy for CPTS is this:

    single-[custom_post_name].php
    archive-[custom_post_name].php

    so you'd want single-news.php for the individual posts and archive-news.php for the example.com/news/ page itself.

  10. Harryson
    Member
    Posted 6 months ago #

    I see, this makes sense, but isn't working. I was wondering how I can make my own "posts system", so this is good. However, I cannot make my own templates work.

    I have got archive-news.php and single-news.php. But it still keeps using the index.php.

    I changed my permalink to /%post_id%/%postname%/ as /news/%post_id%/%postname%/ would result as example.com/news/news/ID/TITLE. I guess that part at least was correct.

    Hmm.. but yeah, the templates are not working. I tried adding Template in the Page attributes in News page edit view, but that doesn't have any effect on the /news/ itself.

    If I could get archive-news.php and single-news.php working, like I want them to work. Then basically all my worries are over and I could use this on so many "custom catalogs".

  11. Harryson
    Member
    Posted 6 months ago #

    Is it wrong to have a page called News at all? Or should I include my news in the menu with 'show_in_menu' => true in the register_post_type() array?

  12. Harryson
    Member
    Posted 6 months ago #

    So I deleted News as a page, because this seems to be the correct way.

    Still cannot make my own templates to work.

    Also removed it from the Main menu and added 'show_in_menu' => true to the register_post_type(). Does not appear in menu however :(

  13. Ipstenu
    Half-Elf Support Rogue & Mod
    Posted 6 months ago #

    Sorry, I was sucked off into meetings and problems at work.

    Yes, delete the news page. That will conflict with 'has_archive' => true,

    Templates - single-news.php is the template for any post made in the news CPT. If that name isn't working, try just news.php for it but I pulled the names from http://codex.wordpress.org/images/1/18/Template_Hierarchy.png so those should have been right.

  14. Harryson
    Member
    Posted 6 months ago #

    So as far as I understand, everything should be working now.

    From a blank wordpress:

    1. Install and very basic theme setup
    2. Create the single and archive templates
    3. Register the custom post type
    4. Put some sample data to your new post type
    ..done, correct?

    Though, it does generate a list in archives-view and displays the contents in single-view. But it does that using index.php and not their representative templates.

    Done all that, but its not working. Also, the link doesn't appear in the menu (and also, technically should be active, but next to the custom page not appearing, that's a very small priority.)

    To be honest, I'm completely stuck. Had my hopes up, as I need to create sort of "catalog features" for some other pages and custom post types seems perfect for the job.

    So how to debug this? Is there some basic step I'm missing out, since I'm not familiar with wordpress at all and basically am rushing to meat the deadline of the project. Maybe some functions.php setup function/variable? ..or maybe some setting in the admin panel -to allow custom posts type templates?

    0:30 in Estonia, so I'll report back in ~10 hours. Thanks for taking time to help me, appreciated. Good night :)

  15. Harryson
    Member
    Posted 6 months ago #

    Ok figured it out. The problem was in my post-type registration:

    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    	register_post_type( 'news_view',
    		array(
    			'labels' => array(
    				'name' => __( 'News' ),
    				'singular_name' => __( 'News' )
    			),
    			'public' => true,
    			'has_archive' => true,
    			'rewrite' => array('slug' => 'news'),
    			'supports' => array('title', 'author', 'thumbnail', 'excerpt'),
    			'show_in_menu' => true
    		)
    	);
    }

    The register_post_type( 'news_view', part to be precise. As my templates where single-news.php etc.. Then the template must be single-news_view.php or my register must be news.

    So it is fixed now, thanks for your help, learning about post-types is a MUST in wordpress :)

Reply

You must log in to post.

About this Topic