• Hi,

    i built an archive in my footer. The archive displays the title from all my posts (i use 7 post types). I have 2 types of page templates, 1 where you see an overview of the chosen post-type. Then there’s the single — which displays a single post. The archive is present at all page-templates.

    Id need help with a function which understands which categories that the posts displayed in the overview uses then compare it with the posts in the archive, if one or more cat’s are the same id like to add a class to those posts in the archive..
    E.g in the overview there are 2 posts which have the same cat’s as 10 of the posts in the archive then the 10 posts would get an calss. Same would be for the single page.

    This is the code im using.
    generating the archive

    <?php $recentPosts = new WP_Query(array('orderby' => 'menu_order', 'post_type' => array('highlights',  'artiklar','intervju', 'portfolio', 'pressrum', 'myo_polling')));
    					while( $recentPosts->have_posts() ) :
    					$recentPosts->the_post(); ?>
    <a href="<?php echo get_permalink( get_page_by_path( 'fragebanken' ) ) ?>#<?php the_ID();?> ">
    								<?php the_title();?>
    							</a>
    				<? endwhile ;?>

    This is some code i’ve been trying with but it needs to be written together with above

    $categories = get_the_category($post->ID);
    if ($categories) {
    	$category_ids = array();
    	foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    
    	$args=array(
    		'category__in' => $category_ids,
    		'post__not_in' => array($post->ID),
    		'showposts'=>-1, // Number of related posts that will be shown.
    		'caller_get_posts'=>1
    	);

    This is the query im grabing post with to the overview.

    <?php $myQuery = new WP_Query(array('showposts' => -1, 'post_type' => ('artiklar')));
    							while( $myQuery->have_posts() ) :
    						    $myQuery->the_post(); ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    My approach would be to save all the category IDs of the overview into an array. Then in the archive loop, check if the current post’s category ID is in that array, and if so add the class to that post’s div.

    Thread Starter gerbsi

    (@gerbsi)

    Hey,

    thanks for the answer — Im still a bit lost (quite new to php and wordpress, or i dont really understand how i can save all cat IDs in an array then compare it to the archive loop. Could you help me or do you know where i can read about it?

    Best

    Thread Starter gerbsi

    (@gerbsi)

    I found som examples which i started to play with. Although I dont know if im on the right way.
    Now im getting this error:
    Argument #1 is not an array in footer.php .

    This is how overview.php looks like

    while( $myQuery->have_posts() ) :
    						    $myQuery->the_post();
    							$categoriesOverview = array();
    							foreach( get_the_category() as $cat ) {
    							$categoriesOverview[] = $cat->name  ;
    							}

    then in footer.php

    while( $myQuery->have_posts() ) : $myQuery->the_post();
    							$categoriesArchive = array();
    							foreach( get_the_category() as $cat ) {
    							$categoriesArchive[] = $cat->name  ;
    							}
    						   $result = array_intersect($categoriesOverwiev, $categoriesArchive);
    						   echo $result;

    Moderator bcworkz

    (@bcworkz)

    Looks like you got the concept quite well. Good work!

    The error must be referring to the array_intersect() line? Which would mean $categoriesOverwiev is not an array. That appears correct, as you have a typo error, a ‘v’ instead of ‘w’ at the end. Don’t be embarrassed, we’ve all done this. I nearly missed it myself ??

    To check variables contain what is expected and are the proper type, temporarily insert something like var_dump($categoriesOverwiev); die(); at various places as necessary to confirm.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘addclass to related posts’ is closed to new replies.