WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_category_link from a Theme Options variable (6 posts)

  1. Erik Ford
    Member
    Posted 3 years ago #

    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(); ?>
  2. vtxyzzy
    Member
    Posted 3 years ago #

    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.

  3. Erik Ford
    Member
    Posted 3 years ago #

    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.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    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);
  5. Erik Ford
    Member
    Posted 3 years ago #

    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.

  6. vtxyzzy
    Member
    Posted 3 years ago #

    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! :)

Topic Closed

This topic has been closed to new replies.

About this Topic