1
votes

I am using WordPress to put together a blog. I am using the Category Image(s) plugin to put an image for each category above the posts.

The main page is laid out as a big image and excerpt for each article and when you click on it, it takes you to the full article (I am using the 'Special Recent Posts' plugin for this). I want category image headers above each big image/excerpt.

Everything works fine for the first article but after that I get no headers. The reason is because the code I have in my header is calling the 'Category Image(s)' php function, which works. Then it calls the 'Special Recent Posts' php function which in effect runs the loop to grab the first five most recent articles. It doesn't run the category images function every time for every article, only the first time.

Here's the code:

<?php get_header(); ?>

<?php c2c_the_category_image($image_extensions='png gif jpg', $image_dir='/wp-content/images/', $use_name_if_no_image=true, $start_from='begin', $limit=999); ?>

<?php echo do_shortcode("[srp srp_number_post_option='5' srp_thumbnail_option='yes' srp_widget_title_hide_option='yes' srp_post_date_option='no' srp_wdg_excerpt_length='50' srp_wdg_excerpt_length_mode='fullexcerpt']"); ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

How can I get it to run the category images function for all the recent posts? Thanks for the help.


EDIT:

I attempting to go into the PHP of the Special Recent Posts plugin but when I attempted to enter the category images code in, it created some critical errors. I'm looking for the easiest solution if it's out there (I know this isn't a very simple question to start with). Any help? (I've placed a bounty)

1
since you are using a shortcode and not a "normal" loop (BTW - why?? ) you can add a small foreach loop , put the both snippts inside and change the number_post_option parameter to 1Obmerk Kronen
I'm relatively new to PHP (explaining the shortcode, that was sort of my 'easy' way of doing it). So make a foreach loop, change the number_post_option parameter to 1 and then have the foreach loop run until it gets to 5 (for the amount of posts)?MillerMedia
yep .. that seems to be the way of doing it if you still want the shortcode ... I would get rid of the shortcode and just construct a normal loop though . I am not familiar with those plugins, but look at the documentation to see the functions, or just read the plugin source code .. It is a good opportunity to learn some PHP ..Obmerk Kronen
Cool, thank you! I'm looking to improve my coding skills so this could be a good opportunity.MillerMedia
Well I'm not quite there yet. I may have a question about the foreach loop...I'm struggling to figure out how to call functions within the foreach loop (I'm new to this so excuse me!). Most of the tutorials that I'm coming across offer foreach loops as arrays.MillerMedia

1 Answers

0
votes

Your way of doing things is extremely complicated and convoluted.

The simplest way that is proved to work is by having images that correspond with either category ID or category slug!

http://baldino.rs/collection

As you can see, this is a page that lists all the categories with their images and descriptions.


I'm having little trouble understading if you want:

  • To have a category image above every post
  • To have a category image AND post image above every post

I'll demonstrate the first solution below

Here is the order in which we will create the blog page:

  1. Name the category images either by slug, or by ID (i recommend ID)
  2. Upload the images to the server - prefferably to the images folder of the theme
  3. Create our custom loop which will display our category image, category name, post title and excerpt

So the index page (blog page) should look something like this

<?php get_header();?>

<div id=:content">
    <div id="posts">
    <?php if (have_posts()) : while (have_posts()) : the_post();?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <?php
            $category = get_the_category();?> 
            <img src="<?php bloginfo('template_directory'); ?>/images/<?php echo $category[0]->term_id;?>.jpg"/>
            <h3 class="cat-title"><?php echo $category[0]->cat_name;?></h3>
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            <div class="entry">
                <?php the_content(); ?>
            </div>
        </div>
    <?php endwhile;endif; ?>    
    </div>

<?php get_sidebar(); ?>
</div>

<?php get_footer(); ?>

You should really avoid plugins for the simple stuff that is easily solved by the loop

I recommend that you start reading the WP codex - http://codex.wordpress.org

Every function, class, reference is documented in detail and it's very easy to understand.

If you have more questions, do not hesitate to ask