Support » Fixing WordPress » get_category_link from a Theme Options variable

  • Resolved Erik Ford


    I am developing a theme that uses theme options to populate the Home page with a custom query. I would also like to have a link back to the admin’s chosen category so you can browse the archives. I am using <?php echo get_category_link( $category[0]->cat_ID ); ?> which is fine if all of the posts are in the selected category. But, if a post resides in multiple categories, this fails. If there a way I can get the category link within my custom query based on the theme option chosen? Here is the query I came up with. Any and all help is truly appreciated.

    <?php $block_one = new WP_Query('category_name='. get_option('wap8_block1') .'&showposts=1&caller_get_posts=1');
    while ( $block_one -> have_posts() ) : $block_one -> the_post();
    $category = get_the_category(); ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Take a look at get_category_link(). The example shows how to get the category ID from the category name using get_cat_ID() and you apparently have the category name in your option.

    I did try to set up the following variables, which failed miserably:

    $cat_name = get_option('wap8_block1');
    $cat_id = get_category_id($cat_name);
    $cat_link = get_category_link($cat_id);

    Then, inside of the loop, I have <a href="<?php echo $cat_link; ?>">. This returns nothing and breaks this and all subsequent loops. Any suggestions? Thanks again.

    I think the function you need is get_cat_ID():

    $cat_name = get_option('wap8_block1');
    $cat_id = get_cat_ID($cat_name);
    $cat_link = get_category_link($cat_id);

    It’s at this moment that I realize that I have been looking at this code for way too long. Thanks so much. Sometimes I need someone else to look at the bonehead mistakes and help.

    One of Murphy’s laws: The longer you work on a problem, the more likely it is that a casual observer will immediately spot the answer! 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_category_link from a Theme Options variable’ is closed to new replies.