6

I would like to add a post (from specific category) between posts in the WordPress Loop (such as 'Sponsors'). Example:

P P P S
P P S P
P S P P

How can I achieve this? I am a beginner with coding, so I don't know enough to modify a Loop on my own. Any Loop Coding Ninjas out there that can provide a solution?

Note that below is my current Loop. It is used to sort posts by price, or by random order:

index.php

<?php while (have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php  the_excerpt(); the_meta ();
endwhile; 

previous_posts_link();
next_posts_link();
?>

functions.php

function my_custom_query($query){
 if ( $query->is_home() && $query->is_main_query() ) {

  $sort= $_GET['sort'];

  if($sort == "A"){
   $query->set( 'orderby', 'rand' );
   $query->set( 'posts_per_page', '2' );
  }

  if($sort == "B"){
   $query->set( 'meta_key', 'price' );
   $query->set( 'orderby', 'meta_value_num' );
   $query->set( 'order', 'DESC' );
   $query->set( 'posts_per_page', '2' );
  }
}
}

add_action( 'pre_get_posts', 'my_custom_query' );

Edit: Update

Birgire's plugin works! Initially, I had problems getting the plugin to work on my theme. The problem was this piece of code that I use within the Loop in index.php (I use it to call for the custom field to appear).

<?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'price', true);
    wp_reset_query();
?>
  • 1
    Glad to hear it worked for you. Notice that you should't use wp_reset_query() within the loop, in fact you shouldn't need to call it, ever, except you're using query_posts(), which isn't recommended either. Regarding the custom field, use for example echo get_post_meta( get_the_ID(), 'price', true); instead. – birgire Aug 18 '14 at 7:50
  • That is SO much simpler! Thanks so much @birgire!! You've solved all of my issues =) Hope more people can benefit from this plugin. – leko Aug 18 '14 at 17:37
6

The Automatic Sponsor Posts Injector:

Here's one idea based on my answer for the question: How to show Y number of custom posts after every X normal posts?

I hopefully made it a little bit more useful here on Github, but it may be refined much more (future work).

The SponsorPostsInjector class will help you to automatically inject the sponsor posts into the theme using the filters the_post, loop_start and loop_end.

Activate the plugin and add the following example to your functions.php file to start the injections:

/**
 * Inject a sponsor post after the first post on the home page,
 * and then again for every third post within the main query.
 */

add_action( 'wp', 'my_sponsor_injections' );

function my_sponsor_injections()
{
    if( ! class_exists( 'SponsorPostsInjector' ) ) return;

    // We want the sponsor posts injections only on the home page:
    if( ! is_home()  ) return;

    // Setup the injection:
    $injector = new SponsorPostsInjector( 
        array(
            'items_before_each_inject' => 3,
            'items_per_inject'         => 1,
            'template_part'            => 'content-sponsor',
        ) 
    );

    // Setup the injection query:
    $injector->query(
        array(
            'post_type'  => 'sponsor',
            'tax_query'  => array( 
                array(
                   'taxonomy' => 'country',
                    'terms'   => 'sweden',    
                    'field'   => 'slug', 
                )
            )
        )
    );

    // Inject:
    $injector->inject();
}

where we have created the content-sponsor.php template file in our current theme directory, to control the layout of the injected sponsor posts.

The idea is that this should also take care of the pagination.

You can hopefully adjust this to your needs.

  • see my reply (made as a comment cause it was too long) – leko Aug 17 '14 at 3:19
  • Since you've read my reply, it is ok to let stackexchange delete my comment. I updated to 0.0.3 and tried it on default themes but no luck. The areas where I likely screw up in is functions.php and content-sponsor.php --- What do you put in content-sponsor.php? a clone of index.php? --- In functions.php the current $injector->query code is for custom post types. How would you code it for regular posts in the category "sponsor" (so I can see if I messed up here)? – leko Aug 17 '14 at 4:36
  • If you try the 2012 theme, you can use content-page.php as your testing template part. I also tested it with: $injector->query(array('category_name' => 'sponsor'));, that worked as well. Let's see if that works for you. If not then we could try to construct a debug version. – birgire Aug 17 '14 at 11:07
  • 1
    Hi, birgire, nice answer, +1 of course. Do you mind to add some license info on your repo? Maybe just a License header on plugin file. I want to make a fork of your code... – gmazzap Aug 17 '14 at 18:00
  • 1
    Thanks to you, @birgire GPL2+ it's perfect for me, I've already done some work, I'll ping you when finished if you want – gmazzap Aug 17 '14 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.