WordPress.org

Support

Support » How-To and Troubleshooting » Create a function to get page link by slug (I do it with categories)

Create a function to get page link by slug (I do it with categories)

  • Hi.

    I´ve created a function to get a category link by it´s slug on the theme function.php. This is the function:

    function cat_link($szKey) {
    	$idObj = get_category_by_slug($szKey);
    	$id = $idObj->term_id;
    	echo get_category_link($id);
    }

    With that, I can do <?php cat_link('category-slug');?> to get the given category link.

    So, I trying to do something like that, but with pages. I already tried those:

    function page_link($szKey) {
    	$idObj = get_page_by_path($szKey);
    	$id = $idObj->term_id;
    	echo get_page_link($id);
    }
    function page_link($szKey) {
    	$idObj = get_page_by_title($szKey);
    	$id = $idObj->term_id;
    	echo get_page_link($id);
    }

    When I try <?php page_link('page_slug');?>, it gets the last published post on the page link, instead of the given page slug link.

    Can someone tell me what i´m doing wrong and how to fix that?

Viewing 1 replies (of 1 total)
  • Ran across your post via google. Probably a bit late, but this worked for me. (Using the page title ‘SiteMap’ as an example)…

    <a href="<? echo(get_page_link(get_page_by_title('SiteMap')->ID)) ?>">SiteMap</a>
Viewing 1 replies (of 1 total)
  • The topic ‘Create a function to get page link by slug (I do it with categories)’ is closed to new replies.
Skip to toolbar