3

Good day to all!

I need to grab article's URL and modify it by deleting the last part of it (moving one level up).

Grabbing the current URL with Wordpress function <?php echo get_permalink( $post->ID ); ?>

Example of use. My current article's URL:

http://example.com/apples/dogs/coffee

Delete the last part of URL so it will be:

http://example.com/apples/dogs

(And with no slash at the end)

So this will return the current Wordpress URL:

<a href="<?php echo get_permalink( $post->ID ); ?>">Text</a>

but how can I delete the last part of it?

Thanks in advance!

6
  • 3
    Look at explode, that should get you started. Commented Jan 22, 2015 at 13:47
  • The format of the URL depends upon the settings for the WP site. Won't this break if it's not using a rewrite URL? Commented Jan 22, 2015 at 13:53
  • Possible duplicate - stackoverflow.com/questions/2430208/… Commented Jan 22, 2015 at 13:57
  • possible duplicate of Remove last element from url Commented Jan 22, 2015 at 13:59
  • None of the link you guys mention point the correct solution. This is a wordpress question, so is not a duplicate of the php questions. Commented Jan 22, 2015 at 14:15

5 Answers 5

10
$url = 'http://example.com/apples/dogs/coffee';
$newurl = dirname($url);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This one seems to be the simpliest and most accurate solution for my case. I modified it a little according to Wordpress, so it is now an ideal solution for me: <?php echo dirname(get_permalink( $post->ID )); ?>
2

Most of the answers written here would work, but it's a bad practice to parse a url using explode and RegExp. It's better to use the PHP function parse_url. In this case, you can't encounter a problem if the url changes. This code will omit the last part of the url's fragment.

Here's the code:

<?php
$url = 'http://example.com/apples/dogs/coffee';
$parsed_url = parse_url($url);
$fragment = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] : '';
$new_fragment = '';
if(!empty($fragment)){
    $fragment_parts = explode('/', $fragment);
    // Remove the last item
    array_pop($fragment_parts);
    // Re-assemble the fragment
    $new_fragment = implode('/', $fragment_parts);
}
// Re-assemble the url
$new_url = $scheme . '://' . $host . $new_fragment;
echo $new_url;
?>

Comments

2

It looks like you are simply looking for a posts' parent. That being the case you need to use 'get_post_ancestors($post->ID)'.

From the wordpress codex...

</head>
<?php

/* Get the Page Slug to Use as a Body Class, this will only return a value on pages! */
$class = '';
/* is it a page */
if( is_page() ) { 
    global $post;
        /* Get an array of Ancestors and Parents if they exist */
    $parents = get_post_ancestors( $post->ID );
        /* Get the top Level page->ID count base 1, array base 0 so -1 */ 
    $id = ($parents) ? $parents[count($parents)-1]: $post->ID;
    /* Get the parent and set the $class with the page slug (post_name) */
        $parent = get_page( $id );
    $class = $parent->post_name;
}
?>

<body <?php body_class( $class ); ?>

Comments

1

This will do what you're asking -

 echo implode('/',array_slice(explode('/',get_permalink( $post->ID )),0,-1))

but it's weak.

Only use as solution as simple as this if you can guarantee you won't have any additional stuff at the end of the URL that you need to keep.

1 Comment

Thank you, this solution is also working! But <?php echo dirname(get_permalink( $post->ID )); ?> looks just a little bit more effective to me.
1

There are many ways (explode, strpos and substr, regex). Using regex you can do something like this:

$url = 'http://example.com/apples/dogs/coffee';
$url = preg_replace('#/[^/]+?$#', '', $url);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.