Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #399

    I am wanting to include a php code which will produce a link above each post. Code link will be something like this:

    http://xxx.com/xxx/category/slug

    Can the support staff, please provide a PHP code, where the above link can be accomplished and place in the theme file (I know where to place it). I would need the category name and slug pulled from the post. It should work with my permalinks settings.

    Thank you

    #31208

    and this is where I’m stuck:

    <?php

    foreach((get_the_category()) as $category) {

    echo ‘category_nicename . ‘/” alt=”‘ . $category->cat_name . ‘” />’;

    }

    ?>Forum

    Not sure how to add the URL slug after the category name.

    Please advise.

    Thanks!

    #31209

    That didn’t come out right, please see the code that I have so far here:

    http://www.copypastecode.com/33316

    #31210

    Can you clarify exactly what you’re trying to do?

    From your description in the 1st post, you want to add a link – by the looks of the copypastecode.com code, you’re trying to add a link for each category.

    Either way, this code should get you started:

    //get all blog categories regardless of whether on this post or not
    $categories = get_categories('title_li=&orderby=name&hide_empty=0');

    //get the categories which are attached to this post
    $categories = get_the_category();

    if ($categories) {

    foreach($categories as $category) {
    $output .= "<a href='".get_bloginfo('home')."/forum/".$category->category_nicename."/' id='".$category->category_nicename."'>".$category->cat_name."</a>";
    }

    }

    Important Note: You must delete or comment out one of the $categories declarations depending on your intentions for this code.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘PHP variable for post link?’ is closed to new replies.