• Hello,

    I am trying to do something I think is quite simple but I can’t for the life of me find how to do it.

    I am using WP mainly as a CMS and news portal. However, I also have a true “blog” subsection (defined as a special WP article category for journal-like entries, let’s say ID = 6). It appears as a separate tab in my menu.

    I have a special template file for it and I know that, using the hierarchy, I can just invoke it through category-6.php.

    Only it’s not very obvious to my visitors and I would like them to get to this page through a special template file called blog.php (for instance) and not category-6.php. Redirects wouldn’t do the thing as I would like the users to
    – see domain.tld/blog.php in their browser when they’re in the category
    – see domain.tld/blog.php as a clear link in the menu (I’m using a straightforward HTML link). But of course, if I do that, WP can’t find the file, since it’s not looking in its own directory but in the real domain root.

    How can I get it to understand where to look? Can I register a custom-named file in a template to achieve this result and how? I would really like to obfuscate the “category-ID.php” filename everywhere.

    With my thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi

    Don’t try to do this through a category page.

    1) Use a custom page template. See this page
    https://codex.www.ads-software.com/Pages#Creating_Your_Own_Page_Templates

    2) Create a static WordPress page called Blog. Assign it the custom page template you made in step 1. That will give the blog a permalink of https://domain.tld/blog/

    3) Add a query_posts statement
    https://codex.www.ads-software.com/Template_Tags/query_posts
    before the WordPress loop in the custom template you made in step 1.
    The query_posts statement should be

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          query_posts("cat=6&paged=$paged"); ?>

    This assumes that posts you want displayed on this page are all assigned to category 6.

    Thread Starter KillerWhale

    (@killerwhale)

    Thank you for your help! I’m seeing things a lot clearer (and it will help in the future for other problems!), but it’s not working correctly…

    I have an automatically generated menu (I’m using Artisteer as a main generated template, hacking it afterwards).
    – When I leave Artisteer to its own devices, it’s effectively parsing the “blog.php” template as a “?page=###” and I arrive on the right page (which is not querying the posts correctly, but one problem at a time). However I don’t have the URL as “blog.php”, neither in the link nor in the URL bar.
    – When I hardcode a test link as <a href=”https://domain.tld/blog&#8221;, I still have a 404.

    I am guessing it has something to do with the way the permalinks are configured (I left them on default)? What would be the right syntax to use?

    With my thanks,

    I am not familiar with that theme.

    In general, you are never going to see a URL of blog.php in WordPress – that is not how it works.

    You create a custom template in the theme folder named blog.php (as example) and assign it as a custom template to a page created in the WordPress page editor. The url of that page will then correlate to however the permalinks are set. For CMS use I generally set permalinks to custom /%postname%/ if the page slug is blog that creates a permalink of https://example.com/blog/

    you are correct, if permalinks are not set, the URL would be something like https://example.com/?p=3

    Try changing them and see if that helps

    Thread Starter KillerWhale

    (@killerwhale)

    Alright, the link structure works, thank you! I guess it was obvious, but I’m still finding my way around ??

    I have a last problem: no entries show on this custom blog page. The code shows

    <?php
    /*
    Template Name: myblog
    */
    ?>
    
    <?php get_header(); ?>
    <div class="contentLayout">
    <div class="content">
    
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          query_posts("cat=6&paged=$paged"); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="Post">
        <div class="Post-body">

    And so on with the layout. It’s really copy-n-paste from my index page, which works fine. Any ideas?

    Thank you!

    Thread Starter KillerWhale

    (@killerwhale)

    Forget about the previous post; when I kept manipulating entries and layout, somehow the page lost its customized layout. It works now perfectly. THANK YOU!

    I now have one true last problem. When I try to click on my blog entry permalink (looking like domain.tld/blog/postid), I keep bouncing back on the blog page, with all the posts (and of course it means I can’t add comments). How can I get a single post?

    The link title looks like

    <span class="PostHeader"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'kubrick'), the_title_attribute('echo=0')); ?>">

    And my permalink structure is
    /%category%/%postid%

    I tried with the default permalink structure and it works, however. Of course, the links are not pretty in this case. ??

    Hi

    1) it should be /%category%/%post_id%
    an underscore was missing in post_id

    2) “blog” or “myblog” is not going to be part of the url for a single post

    if the post slug is ‘today-is-thursday’ and its in a category News with slug ‘news’ and the post id is 137 and WP is installed in the domain root folder, the post URL will be https://mydomain.com/news/137
    I tried it here and that is the result I got.

    You are not going to have “blog” in the permalink for a single post unless you specify category as part of the permalink and the post is in the category “blog”

    Does this address what you are asking?

    Thread Starter KillerWhale

    (@killerwhale)

    Hi,

    Sorry I did not spell the permalink correctly, but that was correct in WP (post_id).

    Yup, I did all that:
    1. Affected to the relevant post the “blog” category
    2. Called the page with the custom template “blog” so that the permalinks direct to domain.tld/blog

    The resulting post URL is indeed domain.tld/blog/###, which is perfectly fine. However this link does not lead to the sole concerned article with its correct layout (and comment entry); I arrive on a page which URL is domain.tld/blog/### but it looks exactly like domain.tld/blog (the custom-template page with the blog posts listed).

    When I set the permalinks to default, the URLs are more complex and not showing what I would like, but the page template and permalinks to each article work as intended…

    On a side note, don’t you have performance issues when having permalinks start with category, tag and such? I read so in the codex and I seemed to notice a difference on my own website, even though I only have 20-something pages.

    (I have come up with a temporary fix in the meantime: keep the default permalinks and have a permanent redirect from /blog to /?cat=6 through .htaccess, but that’s not really an elegant solution ?? )

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom filename for category template’ is closed to new replies.