How do I control the output of a custom post type in the loop?

I assume that you’re making your custom post type in a plugin if you mean for it to work with any theme. In that case, I would suggest building your templates in your plugin, and call all your meta data there. Example: /* Filter the single_template with our custom function*/ add_filter(‘single_template’, ‘my_custom_template’); function my_custom_template($single) { … Read more

How do I display an image before the first post of the loop when I’m using get_template_part?

The variable $count is accessible in the scope of the current function only. So whatever happens inside get_template_part() cannot know that variable. You could use a global variable, which is accessible everywhere as $GLOBALS[‘count’]. But then you risk collisions with plugins and maybe other code. I recommend a helper function for the functions.php: /** * … Read more

wp_query random post

Doing this in PHP is an awful idea: This page is impossible to cache ordering by random is extremely expensive to query, involving creating temporary database tables, and scans, as it has to copy the entire posts table, then randomly re-order the posts, then finally do the actual query on the new table before destroying … Read more

Display post list with different styles

You can just set a variable to count the loop iterations, and then apply different styles or output depending on the value. For example: $post_index = 1; /* Start the Loop */ while ( have_posts() ) : the_post(); $post_class = ($post_index > 3 ? ‘style-a col-sm-12’ : ‘style-b col-sm-6’); echo ‘<div class=”‘ . $post_class . … Read more

pagination not working for category.php (custom post types in categories)

<?php $catname = wp_title(”, false); $wp_query = new WP_Query(); $wp_query->query(‘category_name=”.$catname.”&showposts=5′.’&paged=’.$paged); ?> <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> <?php the_title(); ?> <?php } ?> <?php endwhile; ?> <?php next_posts_link(‘&laquo; Older Entries’) ?> <?php previous_posts_link(‘Newer Entries &raquo;’) ?> I think that you need to look at the $paged variables.

ajax for filtering posts by category in wordpress loops

A piece of javascript/jquery: $(“#queryMenu ul li a”).click(function(){ var currentCategory = $(this).attr(‘id’); $.ajax({ type: ‘post’, url: ‘ajax-category.php’, //sometimes I’m using bloginfo to get current path: url: ‘<?php bloginfo(‘template_url’); ?>/ajax.php’, data: { currentCategory: currentCategory }, success: function(data) { console.log(data); } }); }); Put right path in url. Use $_REQUEST[‘currentCategory’] inside ajax-category.php to get category ID…