WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Madcore
    Member
    Posted 5 years ago #

    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?

  2. dougnbr
    Member
    Posted 5 years ago #

    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>

Topic Closed

This topic has been closed to new replies.

About this Topic