• Resolved tylerjay

    (@tylerjay)


    Whats an example PHP code for calling blog post in certain category from another page?

    I’m wanting to call excerpts form 2 different categories my blog page to my homepage.

    I’m not too savvy with php yet so baby me please ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter tylerjay

    (@tylerjay)

    Still cant seem to find the right information, any ideas?

    <?php
    //get 3 posts from category 5 or 7, display title
    $args=array(
      'showposts' => 3,
      'category__in' => array(5,7),
      'caller_get_posts'=>1
      );
    $posts=get_posts($args);
    if ($posts) {
      foreach($posts as $post) {
        setup_postdata($post); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      } // foreach($posts
    } // if ($posts
    ?>

    Related:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy
    query_posts()
    get_posts()

    Thread Starter tylerjay

    (@tylerjay)

    Thanks for that, however i couldn’t get it to work even with modifying the array. I actually couldnt get a post to display with that at all.
    I did get one display by

    <?php
    	 //get 3 posts from category 5 or 7, display title
    				  $args=array(
    				  'showposts' => 1,
    				  'category__in' => array(1,2),
    				  'caller_get_posts'=>1
    				  );
    				 $lastposts = get_posts('numberposts=1');
                     foreach($lastposts as $post) :
                        setup_postdata($post);
                     ?>
                     <h2><a>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
                     <?php the_excerpt(); ?>
    
                     <?php endforeach; ?>

    However im pretty sure category code isnt doing anything. Is there a way i can literally define which category? Like ‘Events’ or ‘News’

    Thread Starter tylerjay

    (@tylerjay)

    Ok I found the code I was looking for query_posts('category_name=Events');
    I’m just not sure where to input within

    <?php
    				 $lastposts = get_posts('numberposts=1');
                     foreach($lastposts as $post) :
                        setup_postdata($post);
                     ?>
                     <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
                     <?php the_excerpt(); ?>
    
                     <?php endforeach; ?>

    Thread Starter tylerjay

    (@tylerjay)

    still cant figure it out :/

    Okay what are the category IDs of the categories you want posts from?

    How many posts do you want from those categories?

    If you don’t know the category IDs install this plugin:
    https://www.ads-software.com/extend/plugins/reveal-ids-for-wp-admin-25/

    Thread Starter tylerjay

    (@tylerjay)

    Cool, thanks for the plugin! The ID i need to specify is 16…all of the other categories are lumped into 1

    $args=array(
      'category__in' => array(16),
      'showposts' => 1,
      'caller_get_posts'=>1
      );
    $lastposts = get_posts($args);
    Thread Starter tylerjay

    (@tylerjay)

    Brilliant. Worked great. I even got this to work..

    <?php
    				   $args=array(
    					'category__not_in' => array(16),
    					'showposts' => 1,
    					'caller_get_posts'=>1
    					);
    				  $lastposts = get_posts($args);
                     foreach($lastposts as $post) :
                        setup_postdata($post);
                     ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘PHP for calling blog post in certain category?’ is closed to new replies.