Quantcast
Viewing all articles
Browse latest Browse all 5527

eltweety on "Show posts under the same category"

Hello!
I am making a blog portal and I would like to have 5 main categories listed on the main page and if you click on one-it shows you all the posts under this particular category. I have a problem:no matter which category I press, it always redirects me to the same set of posts. Could you please help me?
Here is my code:

<?php
	       	global $wpdb;
	       	global $post;
		    $tag = $_GET['avain'];
		    $postarr = array();
		    $blogs = $wpdb->get_results( "SELECT blog_id FROM {$wpdb->blogs} WHERE blog_id != {$wpdb->blogid} AND site_id = '{$wpdb->siteid}'  AND spam = '0' AND deleted = '0' AND archived = '0'", ARRAY_A);
		    array_unshift($blogs, 1);
		    foreach($blogs as $blog) {
		        $wpdb->set_blog_id($blog[ 'blog_id' ]);
		        $tax_id = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'post_tag' OR taxonomy = 'category' AND term_id IN (SELECT term_id FROM $wpdb->terms WHERE name='$tag')");
		        $post_id = $wpdb->get_results("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = $tax_id");
		        foreach ($post_id as $id) {
		            $postarr = $id->object_id;
		        }
		        $posts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE ID IN ($postarr) ORDER BY post_date DESC");
		        foreach ($posts as $post):setup_postdata($post); ?>

		            <div class="post"><header class="post-header"><div class="date-holder"><span><?php the_time('j F, Y') ?></span></div>
		            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></header>
		            <div class="post-content"><?php the_content(); ?></div></div>
		        <?php endforeach;
		        wp_reset_postdata();
		    }
		    ?>

This is the tags.php template. I also have the following .php template where I listed my categories:

<div class="category-holder list-center">
                <h5>Kategoriat</h5>
                <?php $site_url = get_bloginfo( 'wpurl' ); ?>
                <a target="_blank" href="<?php echo $site_url; ?>/tags/?avain=<?php the_field('cat1'); ?>"><?php the_field('cat1'); ?></a><br/>
                <a target="_blank" href="<?php echo $site_url; ?>/tags/?avain=<?php the_field('cat2'); ?>"><?php the_field('cat2'); ?></a><br/>
                <a target="_blank" href="<?php echo $site_url; ?>/tags/?avain=<?php the_field('cat3'); ?>"><?php the_field('cat3'); ?></a><br/>
                <a target="_blank" href="<?php echo $site_url; ?>/tags/?avain=<?php the_field('cat4'); ?>"><?php the_field('cat4'); ?></a><br/>
                <a target="_blank" href="<?php echo $site_url; ?>/tags/?avain=<?php the_field('cat5'); ?>"><?php the_field('cat5'); ?></a>
            </div>

I am a beginner in PHP and MySQL, also in Wordpress, would be grateful for any advice!Thank you!


Viewing all articles
Browse latest Browse all 5527

Trending Articles