开发者

How to Display Nice URL for Wordpress Category Page?

开发者 https://www.devze.com 2023-01-11 12:37 出处:网络
I have a site whose home page shows a single Wordpress category page.I\'m using a custom theme and the index.php file there does a 301 redirect to \"mysitename.com/?cat=3\".This page is generated with

I have a site whose home page shows a single Wordpress category page. I'm using a custom theme and the index.php file there does a 301 redirect to "mysitename.com/?cat=3". This page is generated with an event calendar plugin and a lot of custom code. It works, but the displayed URL is "mysitename.com/?cat=3" which I'd prefer my visitors not see; I'd rather have the URL be just "mysitename.com", "mysitename.com/index.php", or even "mysitename.com/calendar".

The visitor can view "prev month" and "next month", which result in URLs like "mysitename.com?cat=3&eventDate=2010-07&bt=a" -- still within the same category but with additional parameters in the URL. (I am OK ha开发者_如何学编程ving a more convoluted URL like this shown to my visitors on previous/next month; it's just the initial page URL I'd like to be nicer.)

I've messed around with the permalinks but can't get nice URLs to work correctly that way. Is there an .htaccess way to do this?


In the "permalinks" section under the "Settings" tab, you can add a category base to the categories.

Thus if you have a category base of "category" and a category name "Cool Stuff" with a slug of "cool_stuff"

Your category link would link like this depending on your permalink settings.

http://yoursite.com/category/cool_stuff/


Okay. My Last answer was not base on your "edited question".

If you remove the 301 redirect to "mysitename.com/?cat=3" you can edit the index.php file.

This will most likely take care of your url issue.

You can edit the loop to show only categories from id: 3

Thus, you will make a querypost for category 3 as follow:

<?php query_posts('showposts=9'); ?>

See Query Post @ Wordpress for more info.

Put this before the loop.

The coding may look like this:

<?php query_posts('cat=3'); ?>
<?php if (have_posts()) {?>
    <?php while (have_posts()) : the_post(); ?>
        <li>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            <span style="float: right;"><?php the_time('m/d/y'); ?></span>
        </li>
<?php endwhile; }?>


You could use .htaccess to map mywebsite.com/calendar to mywebsite/?cat=3, but WordPress will still look at the original url to find out which category to use, so this will not work.

I suggest you look in the code of the plugin to find out how it gets the current category, and maybe trick it (by an extra call to query_posts('cat=3')) to think we are looking at the url for category 3 (you don't need to display the posts, just do the query). You can even do this on your homepage (but maybe use home.php, not index.php, since the latter is a fallback for other template pages), so you don't need a redirect. But the key is that you need to look at the code of your plugin (can you tell us the name?) to find out how it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号