I had to do that, but only in some categories in my site.
This was the code I used – I put it in my theme’s functions.php:
// use template_redirect action:
add_action( 'template_redirect', 'my_redirect_to_post' );
/// Function to redirect from certain category pages to first post of those categories
function my_redirect_to_post() {
if (is_category('3') || is_category('5') ) {
$category = get_the_category();
$posts = query_posts('showposts=1&cat=".$category[0]->cat_ID);
if(have_posts()) :
wp_redirect(get_permalink($post->ID));
endif;
}
}
Of course, if you need to do it in all your categories, you can remove the condition if (is_category("3') || is_category('5') ).
Related Posts:
- Best Way to Redirect Category to Page and Hide Category
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Preventing index.php?category_name=something from redirecting
- Add forward slash on categories url (serve one version of a url)
- Multisite blog converting categories to subdomains
- Optimal way to redirect home page to category archive?
- Redirecting specific category posts to subdomain
- How to: 301 Redirect /category/ to /customname/
- How to? Redirect /category/sci-fi/ to /category/scifi/?
- Redirecting category link to first child post
- Redirect category to url with /category
- Changing permalink from /postname/ to category/postname/
- WordPress category page redirects in .htaccess not working
- Why do my categories redirect?
- WP didn’t redirect to canonical category URL
- Sub Categories Keep Redirecting to Pages With Same Slugs
- redirecting from a general category page to another specific one
- Redirect single article permalink to paginated category page
- Automatically redirect a page to a category that share the same slug
- WordPress link to post links to category (loop)
- How to display post in custom file not in archive file?
- Use blog as base for tags and categories
- Category link redirect to custom template page instead of index.php?
- Redirect depending on category chosen
- Need help with major category and permalink re-organization
- Redirect Problems with Archive Page and GET variables
- OK to redirect the root WordPress category slug?
- How to prevent WordPress from redirecting single page to prefixed category archive with same name?
- Category pages redirecting to tab on Posts page grid
- Default URL for category dropdown select option
- WordPress Category url redirects to subdomain
- Catch 404 after changing permalink structure from /%postname%/ to /%category%/%postname%/
- Paginations in Category Page Leads to Sample Pages
- htaccess redirect for all categories converted to tags now showing 404
- Page redirecting to category (IE only)
- Category Redirection Problem
- Check if a post is in any child category of a parent category
- How to export and import taxonomies (category, tag and/or custom taxonomy) and their terms
- Delete a specific category when deleting a user
- 404 error on subcategory pages
- Add line breaks in Category description via enter
- Display only the latest post from multiple categories
- Mass/Bulk assign categories to posts
- how do I filter single_cat_title to remove all instances of a particlular word
- Format Brand value in Category view [closed]
- Get multidimensional array that reflects category hierarchy
- Add Post Screen Keep Category structure
- Possible to make a subdirectory that loops through a single category?
- editing fonts of category links from the_category() funtion
- Archives for each category on WP Multisite
- Update post title with category name after editing a category
- Automatically set default password to all posts in a specific category
- Style wp_list_categories
- Warn me about ‘uncategorized’ posts
- Categories showing page not found
- Why category.php throw 404 in wordpress while calling paginate_links()?
- How to get related tags based on a category?
- Send to different single pages by category when multiple categories exist
- Why don’t posts show up in my categories on the frontend?
- Creating Category Specific menus
- How to do some action weekly?
- Some categories missing on wp-admin/edit-tags.php?taxonomy=category
- Multiple selection for wordpress widget
- How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?
- function query_posts disabling current_page_menu class
- Using if statement in index.php instead of creating separate template files
- How to get category names in post_class?
- output only if within a combination of two cats
- Panoramica Theme Portfolio
- wp_list_pages() not showing on posts
- WordPress Category Question
- Conditional widget for theme template file
- Help to display the current categories and tags of a post
- How to show the clicked category related posts on category detail page?
- No “Quick Edit” links on categories admin page
- How can I display the category name in the single post when using WP Blog and Widget?
- Redirect Category pages to blog page with query string
- WordPress display only child category in template
- Woocommerce change tag_id to name
- Im trying to add a div for just one category
- Too many categories drastically slow down my website
- Configure query with multiple categories in a custom order?
- Get category fixed in dropdown browse categories
- Category menu that filters out empty categories
- Create Cross Promoting Categories in WordPress Using ‘pre_get_posts’
- Category count but only if category has at least one post
- Determine the category that an archive-page is for
- Easiest way to display post archives in category-then-alphabetical order?
- Printing out Category and all Category Children on category.php
- Can i know if it is Category first page or 5th?
- How do i set up multiple portfolios (each with its own defined set of images -) to display on the same page?
- Custom EndPoint not working when strip child category in URL
- How to add custom prefix before category base for category page URL?
- select posts from sql except posts from specific category
- Prevent posts assigned to sub-categories being accessible also through top category URL
- Category and tags are not working properly
- How To Remove Parent Category from Child Category URL?
- Error while add Category or Tag in WordPress
- Display posts in a random order for a specific category
- Why is WordPress adding in a subcategory to my url when it has no parent page?