Forums

Redirecting a page to the latest single post (1 post)

  1. pushkinpassey
    Member
    Posted 2 years ago #

    Hi!
    I am trying to redirect a page to the latest post in a particular category.

    I found a redirect code from somewhere which redirects to URL mentioned in the body of the post but I can't figure out how to modify it redirect to the latest post in a particular category.

    <?php
    /*
    Template Name: Redirect
    */
    
    if (have_posts()) {
      the_post();
      $pattern = '@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@';
      preg_match($pattern, get_the_excerpt(), $matches);
      if ($matches[0]) header('Location: '.$matches[0]);
      else echo 'Enter a URL into your page body text.';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.