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
- How to display only 3 main categories, separated by commas, since they are marked in the post?
- is_category() function
- Create terms when registering taxonomy?
- Add category to custom post type automatically, using category slug
- How do I edit pre_get_post for the category the user is currently in?
- Category Alphabet List Broken
- Display Only One Category
- Display only one result from “get_the_category_list”
- Display empty taxonomy terms with get_terms()
- Connecting WordPress gallery to custom categories (taxonomy)
- Assign an icon to a category post
- How can I show the subcategories content on the parent category page?
- Get term by slug in Gutenberg
- Show only first children level of current category?
- Problem displaying one post from each category
- How do I automatically put categories and their descriptions on a WP Page?
- Taxonomy filter all children
- Bulk category update is not updating
- RSS/XML of all Categories and/or Tags
- URL path for wordpress categories
- How do I get rid of “category” from my URL structure?
- Checkboxes in the category list?
- Have different permalinks based on category
- How do I output all category with custom field?
- Add /category/ to an author archive page
- Remove subcategory and category with some rules from permalink
- How can I edit the ‘Most Used’ tab in the categories meta-box to show another custom taxonomy?
- How to remove specific categories from posts?
- Does “show_option_all” in wp_list_categories do anything?
- how to get full category path including parent and subcategories
- Categories are not listing their respective products
- Two columns in homepage each with one category
- Pagination on category.php and tag.php not working
- How do I attach a page to a category similar to how we attach media to a post?
- Display all product tags associated with a specific product category
- How replace individual elements in the RSS feed with a single string
- how to nest specific category thumbnails inside different containers
- Adding a block with query in homepage
- what file I need to create to work date archive links?
- multiple values in an array for category__and does not work with WP_Query
- How can I include custom category and tag base in template files?
- Add properties to term object
- Display all Categories
- custom wp_list_categories in single.php
- How to target all woocommerce categories that don’t have any subcategories in them?
- echo $category[0]-> cat_name; shows only one category name
- How can i echo next and previous custom category from a Taxonomy
- Change post category within pre_get_posts
- Ajax Category add doesn’t update the list table custom column
- how to get postmeta info based on multiple categories?
- create post with main category and other categories from admin panel
- get_posts – require all categories
- Add advertising box to a specific category page
- How do you add Author and date Published on Category Pages?
- Twenty Eleven Theme [duplicate]
- display certain category on custom template page
- Get post by Category in custom template
- Ajax call activate after submit in edit-tags.php page
- List products from current category
- Assign a category by user and customize the edit-tags.php?taxonomy=category page
- comments_number fails to print
- How to show posts of a specific category
- How to show child category name & post from it’s parent [closed]
- Open everything in new tab