• Resolved aminabbasian

    (@aminabbasian)


    Hopefully someone can help me on this.

    I have found this thread which is of great use.

    This creates a link to the latest post from a certain category, however I would like to do this 6 times in a page, with each link to a different category.

    I’ve tried a few simple edits on this but with no luck. This is my main query and would be delighted If I could resolve this.

    Further to this (not as important!) I would like to have this linked by image rather than text. I am using the custom field function to insert an image but the code below does not show the image:

    <?php
    //list post title for one post is specified category
    $cat = 5; //category ID
       $args=array(
       'category__in' => array($cat),
       'showposts'=>1,
       'caller_get_posts'=>1
       );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Fashion"><img src="<?php $fashion = get_post_meta($post->ID,'fashion',true); if ($fashion) { echo $fashion; } else {  img(); }?>"/></a></p>
    <?php
    endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Try:

    <?php
    // array of category ids
    $cat_array = array(1,2,3,4,5,6);
    foreach ($cat_array as $cat) {
    	$args=array(
    	'category__in' => array($cat),
    	'showposts'=>1,
    	'caller_get_posts'=>1
    	);
    	$my_query = new WP_Query($args);
    	if( $my_query->have_posts() ) {
    	while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Fashion"><img src="<?php $fashion = get_post_meta($post->ID,'fashion',true); if ($fashion) { echo $fashion; } else {  img(); }?>"/></a></p>
    	<?php
    	endwhile;
    	}
    	wp_reset_query();  // Restore global post data stomped by the_post()
    }
    endforeach;
    ?>
    Thread Starter aminabbasian

    (@aminabbasian)

    thanks but no luck I’m afraid.

    This is my current code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I want each image (which is a custom field) to link to the latest post from each category.

    Thread Starter aminabbasian

    (@aminabbasian)

    unfortunately the code has been deleted.

    please go to https://wordpress.pastebin.com/icrXJzda to view my current code.

    Thread Starter aminabbasian

    (@aminabbasian)

    bump

    Thread Starter aminabbasian

    (@aminabbasian)

    ok i have an update on this.

    i found this https://www.ads-software.com/support/topic/how-put-latest-post-from-each-category-on-homepage?

    this at the moment lists the categories in <p> tags.

    but I would like to edit it so that it works with my current layout.

    which is:

    <ul>
    <li>
    <a href="link for cat 1"><img src="image"/></a>
    <p>Fashion</p>
    <a href="link for cat 2"><img  src="image"/></a>
    <p>Shows and Backstage</p>
    </li> 
    
    <li>
    <a href="link for cat 3"><img src="image"/></a>
    <p>Nightlife</p>
    <a href="link for cat 4"><img src="image"/></a>
    <p>Personal</p>
    </li> 
    
    <li>
    <a href="link for cat 5"><img src="image"/></a>
    <p>Design</p>
    <a href="link for cat 6"><img src="image"/></a>
    <p>Illustration</p>
    </li>
    
    </ul>

    i’ve been working on this for the past few hours but with no luck, hopefully someone can help me!!

    Thread Starter aminabbasian

    (@aminabbasian)

    OK…

    Did find a work around at https://www.readywpthemes.com/wordpress-recent-posts-from-specific-category/

    I’ve simply repeated this code 6 times and replaced the title with the image.

    finally got there!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Link to latest post by category’ is closed to new replies.