0

I have a custom post type "article" & I need to display some custom urls is a widget in the sidebar using php code widget. When I use this code withing the loop it works perfectly:

<a href="<?php echo site_url('/article/full-text/'); ?>
<?php $slug = $post->post_name; echo $slug; ?>" title="<?php the_title_attribute(); ?>">Full Text</a>

but when I put this code in the php code widget it displays only the first part of the url. To be more clear, when I use this code within the loop the url will be (www.my-site.com/article/full-text/article-title). But in the php code widget the url will be (www.my-site.com/article/full-text/). Am I missing something here? Any help please?

2
  • Not really shure what are you trying to do.. why don't u just pass an id of post you want to link to like these: get_permalink( id ); where id == post/page/cpt id ?
    – Maikal
    Sep 7 '15 at 22:48
  • I need to display custom url so i can use change template feature based on url. thus, i need to split the url & add the custom part.
    – nisr
    Sep 7 '15 at 23:00
1

You'r missing $post variable ( it is not defined and that for you don't get post_name ).

As a solution you could add global $post;

You'r code would be like:

<a href="<?php echo site_url('/article/full-text/'); global $post;
$slug = $post->post_name; echo $slug; ?>" title="<?php the_title_attribute(); ?>">Full Text</a>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.