WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] dynamic link to category (2 posts)

  1. onirisweb
    Member
    Posted 1 year ago #

    Hi,

    Like the title says, I am trying to get a dynamic link of a category.

    Actually I am building a custom archive template, which displays the 5 last posts in each category.

    Here is my code :

    <?php
                // get all the categories from the database
                $cats = get_categories();
    
                    // loop through the categries
                    foreach ($cats as $cat) {
    
                    ?>
                    <div class="<?php my_post_class(); ?>">
                    <?php
                        // setup the cateogory ID
                        $cat_id= $cat->term_id;
                        // Make a header for the cateogry
                        echo "<span class='position-band'><span class='band-side'></span><span class='band-small-title'>".$cat->name."</span><span class='band-right'></span></span>";
    
    					?>
                        <div class="clear"></div>
                        <?php
                        // create a custom wordpress query
                        query_posts("cat=$cat_id&posts_per_page=5");
                        // start the wordpress loop!
                        if (have_posts()) : while (have_posts()) : the_post(); ?>
    
                            <?php // create our link now that the post is setup ?>
                            <div class="title-cat"><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
                            <?php echo '<br/>'; ?>
    
                        <?php endwhile; ?>
    
    	<a href="?cat=<?php $cat=$cat_id ?>">(View more...)</a>
    
    				<?php	endif; // done our wordpress loop. Will start again for each category ?>
                        </div>
                                    <?php } // done the foreach statement ?>
    
                    </div>

    If this code works well, I would like to make a View More link going to the specific category page.

    Actually my view more link doesn't work. It goes to index page :(

    <a href="?cat=<?php $cat=$cat_id ?>">(View more...)</a>

    What's wrong with it ?

    Thanks for your help.

  2. onirisweb
    Member
    Posted 1 year ago #

    Ok I found how to make it :

    <?php
    $category = get_the_category();
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">View More</a>';
    }
    ?>

    If it can help somebody...

Topic Closed

This topic has been closed to new replies.

About this Topic