Forums

[resolved] template_redirect question (2 posts)

  1. Smrth
    Member
    Posted 6 months ago #

    so, i have this one question about one function that use template_redirect action hook.. for example i'll use this url mysite.com/portfolio
    that page/post don't exist, and i would like to skip that step.. i have image in my head, but dont know how to write it..

    when requested url at end has portfolio redirect to template portfolio.php an archive template

    can someone help? thanks in advance..

    p.s. portfolio is custom post type...

  2. Smrth
    Member
    Posted 6 months ago #

    function portfolio_template_redirect() {
    		$url = $_SERVER['REQUEST_URI'];
    		$url = preg_match('/portfolio/', $url, $matches);
    		if($url) {
    			include('\..\portfolio.php');
    			exit;
    		}
    	}
    	add_action('template_redirect', 'portfolio_template_redirect');

    no need.. here it is....

    here's if you link to single portfolio is portfolio/something/

    `function portfolio_template_redirect() {
    $url = $_SERVER['REQUEST_URI'];
    $url = substr($url, -10);
    if($url == 'portfolio/') {
    $url = substr($url, 0, 9);
    } elseif($url == '/portfolio') {
    $url = substr($url, 1, 10);
    }
    if($url == 'portfolio') {
    include('\..\page-portfolio.php');
    exit;
    }

    }
    add_action('template_redirect', 'portfolio_template_redirect');`

Reply

You must log in to post.

About this Topic