Support » Fixing WordPress » setting link based on page title

  • Resolved imta2d

    (@xsux2bmex)


    I am running a school blog and and attempting to link to each department’s blog entries from their department page. So far I am simply using the $the_title tag to link to the page I want: a href=”http://mywebsite.com/author/<?php the_title(); ?>. The only issue with this is that in a few cases the title has a space in it (ie: social science), but the author/username has no spaces. Can someone suggest how I would go about fixing this for the few departments this is affecting?

    Thanks for your time.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Use <?php the_permalink(); ?> instead of <?php the_title(); ?>

    Related:
    Template_Tags/the_permalink

    xsux2bmex,

    The permalinks for each post should be adding a dash between each of the title’s words. Like below:

    Title: Hello World
    Permalink: hello-world

    Check the permalinks under each post title on the edit post page.

    – – – –
    A little late on this post, nice catch though. I had permalinks on my mind at least.

    Thanks for the suggestions guys, but I probably didn’t explain myself as well as I should have.
    Each department in the school has their own information page: ie. http://mywebsite.com/departments/business/, …/departments/social-science, etc.
    On each of these pages I want to put a link to each department’s blog entries (found on the front page) via their author name (unless there’s a better way to do this).
    So the link I’ve used to do this (http://mywebsite.com/author/<?php the_title(); ?> which steals the title off each department page. This works fine except where the department name is 2 words ie. Social Science where I get: http://mywebsite.com/author/Social Science – which of course comes up as a 404 error since the author username for Social Science is: socialscience.
    I’m wondering if there is a way to strip the space out of the $the_title tag or if there is another way to accomplish what I need.

    Using $the_permalink tag as suggested simply gives me the link to the current page, which doesn’t help me.

    Then Template_Tags/get_permalink using a post ID.

    But I want all blogs by the author for each department.

    Okay not understanding so hopefully someone else can jump in with a solution.

    xsux2bmex,

    Okay, after reading it a few times I see what you’re asking for and why. Using <?php the_permalink(); ?> will give you a result such as:

    http://mywebsite.com/author/http://mywebsite.com/article-title

    I haven’t tested this but give the following a try – courtesy of Jason Boyle:

    <?php $title = get_the_title(); ?>
    <a href="http://mywebsite.com/author/post?url=<?php echo $title; ?>&<?php echo str_replace(" ", "%20", $title); ?>">
    <?php echo $title; ?></a>

    See more here: http://wpcult.com/remove-spaces-when-echoing-the_title/

    Thank you very much doc4!!
    Being new-ish to php I didn’t know how to put it all together. The code you gave me above didn’t suit my needs, but definitely got me on the right track.
    Here’s the code I used to get what I needed:

    <?php $title = get_the_title();
    $newtitle = str_replace(' ', '', $title); ?>
    
                     <h1><?php the_title(); ?><span><?php edit_post_link('Edit Page', '[ ', ' ]'); ?></span>
                     <div class="alignright"><a href="http://mywebsite.com/author/<?php echo $newtitle ?>" title="Read Blog Entries"><img src="<?php bloginfo('template_url'); ?>/images/read_blog.gif" alt="Read Blog Entries" /></a></div></h1>

    It may not be pretty, but it works. So thank you to all who tried to help me get this solved.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘setting link based on page title’ is closed to new replies.