• Hello,
    I’m currently working on a blog-theme for myself, and it’s built upon three categories. My index shows three columns, which are set to category id 1 to 3.

    //column 01
    $feature_post = get_posts( 'category=1&numberposts=3' );
    //column 02
    $feature_post = get_posts( 'category=2&numberposts=3' );
    //column 03
    $feature_post = get_posts( 'category=3&numberposts=3' );

    Everytime I change the name of a category the ID of the categorie changes? I’m working with WPMU. Is this a normal behaviour?

    Can use this three column theme somehow without using the category id’s. The thing is, I’m always using ONLY 3 Categories. There wont be more or less. And I’ll always have this three columns.

    Any idea how to solve that?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Use this as an example to get the id of category events

    $cat_id = get_cat_ID('events');

    Thread Starter sepp88

    (@sepp88)

    Thank you! Where and how do i paste/use it?

    $cat_id = get_cat_ID('events');
    $feature_post = get_posts( $cat_id&numberposts=3' );

    ?

    $cat_id = get_cat_ID('events');
    $feature_post = get_posts( 'cat='.$cat_id . '&numberposts=3' );
    Thread Starter sepp88

    (@sepp88)

    If I do so, all three columns are the same. It always retrieves the same category.

    Now every column looks like this: (so the following part repeats three times in my index.php, before it differed in the id of the categories like posted above)

    <!-- column and category 1 -->
    <?php
    $cat_id = get_cat_ID('events');
    $feature_post = get_posts( 'cat='.$cat_id . '&numberposts=1' );
     	foreach( $feature_post as $post ) : setup_postdata( $post );
    	foreach((get_the_category()) as $category) {
    		$currentcat = $category->cat_name . ' ';
    			if ($currentcat == $latestcat){ ?>
    				<ul class="latest" id="latestcat">
    				<li class="cat-title"><h2 class="latest"> <?php echo $currentcat; ?> </h2></li>
    			<?php } else { ?>
    				<ul class="latest">
    					<li class="cat-title"><h2 class="latest"> <?php echo $currentcat; ?> </h2></li>
    <?php		}
    	}
    endforeach;
    $cat_id = get_cat_ID('events');
    $feature_post = get_posts( 'cat='.$cat_id . '&numberposts=3' );
    foreach( $feature_post as $post ) : setup_postdata( $post ); ?>
    	<li class="news-element">
    		<ul>
      			<li class="list-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
      			<li class="latest-excerpt"><?php the_excerpt(); ?></li>
    		</ul>
    	</li>
    <?php endforeach; ?>
    </ul>

    What am I doing wrong. Of course it should get the first three (and only three, because i don’t have more) categories.

    Using the code you posted originally.. to recap..

    //column 01
    $feature_post = get_posts( 'category=1&numberposts=3' );
    
    //column 02
    $feature_post = get_posts( 'category=2&numberposts=3' );
    
    //column 03
    $feature_post = get_posts( 'category=3&numberposts=3' );

    .. with suggested addition..

    //column 01
    $the_cat = get_cat_ID('catonename');
    $feature_post = get_posts( 'cat='.$thecat.'&numberposts=3' );
    
    //column 02
    $the_cat = get_cat_ID('cattwoname');
    $feature_post = get_posts( 'cat='.$thecat.'&numberposts=3' );
    
    //column 03
    $the_cat = get_cat_ID('catthreename');
    $feature_post = get_posts( 'cat='.$thecat.'&numberposts=3' );

    … obviously update the names .. catonename, cattwoname, etc…

    Thread Starter sepp88

    (@sepp88)

    Sorry, i might be stupid but i don’t get it!

    <!-- column and category 1 -->
    <?php $the_cat1 = get_cat_ID('experimental');
    	$feature_post = get_posts( 'cat='.$thecat1.'&numberposts=1' );
     	foreach( $feature_post as $post ) : setup_postdata( $post );
    	foreach((get_the_category()) as $category) {
    		$currentcat = $category->cat_name . ' ';
    			if ($currentcat == $latestcat){ ?>
    				<ul class="latest" id="latestcat">
    				<li class="cat-title"><h2 class="latest"> <?php echo $currentcat; ?> </h2></li>
    			<?php } else { ?>
    				<ul class="latest">
    					<li class="cat-title"><h2 class="latest"> <?php echo $currentcat; ?> </h2></li>
    <?php		}
    	}
    endforeach;
    $the_cat1 = get_cat_ID('experimental');
    $feature_post = get_posts( 'cat='.$thecat1.'&numberposts=3' );
    foreach( $feature_post as $post ) : setup_postdata( $post ); ?>
    	<li class="news-element">
    		<ul>
      		<li class="list-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
      		<li class="latest-excerpt"><?php the_excerpt(); ?></li>
    		</ul>
    	</li>
    <?php endforeach; ?>
    </ul>

    For instance, my cat1’s name is “experimental”
    so i changed your script-suggestion to:

    $the_cat1 = get_cat_ID('experimental');
    $feature_post = get_posts( 'cat='.$thecat1.'&numberposts=3' );

    Of course i changed this for every of my three columns. Somehow all three columns show no the same title, and the same posts which got actualy posted into those three different categories.

    Even though i have no those three different columns, all three columns on my site look exactly the same.
    I even changed $the_cat to $the_cat1 to $the_cat3 so that i can be absolutely sure that every category uses a different cat-id.

    ?? Sorry for those annoying questions but i’m not much of a programmer.

    What are the names of your three categories you are wanting to display?

    Thread Starter sepp88

    (@sepp88)

    they’re called:
    experimental, interaction, process

    The in each of your sections you will need

    $cat_id = get_cat_ID('experimental');
    $cat_id = get_cat_ID('interaction');
    $cat_id = get_cat_ID('process');

    If you continue this thead please paste ALL the code from that template into a pastebin such as wordpress.pastebin.com, and report the link back here.

    Thread Starter sepp88

    (@sepp88)

    it’s now looking like this:
    https://wordpress.pastebin.com/qFiywf26

    same column three times side by side.

    Go through that code and make sure you are using the correct variable in each of the get_posts statements

    For example see where you assigning the category id for ‘experimental’ to the variable $the_cat1 but use $thecat1 in the get_posts statement.

    <?php $the_cat1 = get_cat_ID('experimental');
    $feature_post = get_posts( 'cat='.$thecat1.'&numberposts=1' );

    Change one or the other to something like:

    <?php $thecat1 = get_cat_ID('experimental');
    $feature_post = get_posts( 'cat='.$thecat1.'&numberposts=1' );

    Fix it is at least six places and of course for cat2 use $thecat2 and for cat 3 use $thecat3

    Sorry part of the error was mine, i made typos when i gave the implementation example, apologies guys.

    Thread Starter sepp88

    (@sepp88)

    oh, sorry, that was my fault. now it’s working perfectly.
    and there is no other chance to solve that problem, so that i can rename this three categories without having to chance the index.php file.

    Originally you started out asking how to NOT use IDs, but now you also want to change the name of the categories… Guess you could use a plugin such as https://www.ads-software.com/extend/plugins/custom-configs/ that would allow you to save those values via the admin interface then in your template read those values.

    This is similar to how theme authors allow an admin interface to specify a ‘featured category’ without having to modify the theme template.

    here is some outsider idea:
    if there are always only three categories in the whole blog (plus ‘uncategorized’ with id=0),
    then this snippet should automatically get these three category IDs:

    <?php $i=0; foreach (get_categories('orderby=id&order=asc') as $cat) {
    	$ca[$i]=$cat->cat_name; $i++;}
    	$cat1 = $ca[1]; $cat2 = $ca[2]; $cat3 = $ca[3]; ?>

    (uncategorized would be in array element 0 and ignored)

    then use these variables in here:

    //column 01
    $feature_post = get_posts( 'category='.$cat1.'&numberposts=3' );

    etc.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘show first three categories without id’s???’ is closed to new replies.