0

I've made a custom post type "brands". I have a custom template that display's these brands on this page: http://www.southernms.com/new/brands/

I also want these brands added to the homepage. So I am using a widget to show them on the homepage: http://www.southernms.com/new/

My problem is. On the brands page, you can see the logo's and nothing else. However on the homepage it shows the logo's yes, but at the bottom of the page it also shows the name of all the brands like they are posts. I am using the same code to display the brand logos in my custom template, as I am in the widget. So I'm not sure why one of them is adding extra code I don't want and the other isn't.

Any suggestions?

Here's my code:

<?php
global $theme;
query_posts(array(
'post_type' => 'brands',
'order' => 'ASC',
'posts_per_page' => 99
));
?>

<ul class="brands">
 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  <li> 
    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'themater' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail( array(125,125) ); ?></a>
  </li>
  <?php endwhile; ?>
</ul>

<?php endif; ?>
5
  • 1
    Don't use query_posts. stephenharris.info/die-query_posts-die
    – shanebp
    May 28, 2015 at 19:03
  • is query_posts the reason that the code is showing twice? Or you just don't like using it?
    – KVDD
    May 28, 2015 at 19:07
  • 1
    query_posts breaks the main query and page functionalities, these are very important functions that are wrecked. This is how bad query_posts is May 28, 2015 at 19:14
  • Which template file is this? May 28, 2015 at 19:17
  • Thanks for the explanation @PieterGoosen If there's a legit reason to change the method then I don't mind. I think I've gotten it working using the other method shane mentioned
    – KVDD
    May 28, 2015 at 19:23

2 Answers 2

2

As already mentioned, don't use query_posts(). Even the WordPress docs state this:

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.

https://codex.wordpress.org/Function_Reference/query_posts

The problems with that function are many, including clobbering the main query which can cause unexpected issues with plugins and theme code and its use increases the number of queries to the database thus negatively effecting the page load time. I suspect the fact that you are clobbering the main query is the cause of your issue.

If this is the main query of the page, then you need:

function pregp_wpse_189739($qry) {
  if (is_main_query()) {
    $qry->set('post_type','brands');
    $qry->set('posts_per_page',99);
    $qry->set('order','ASC');
  }
}
add_action('pre_get_posts','pregp_wpse_189739');

If it is not the main query then:

$args = array(
  'post_type' => 'brands',
  'order' => 'ASC',
  'posts_per_page' => 99
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
  while ($my_query->have_posts()) {
    $my_query->the_post();
    // you Loop code
  }
}
wp_reset_postdata(); 

I find get_posts() to be a bit cumbersome and awkward as you have to code some of the loop yourself, but it should work though not all Loop hooks will fire.

1

This is what I changed it to, and it seems to be working on the Homepage without extra added code.

<ul class="brands">
  <?php
  global $post;
  $posts = get_posts( array( 'numberposts' => 99, 'post_type' => 'brands', 'order' => 'ASC' ) );
  if( $posts ):
     foreach( $posts as $post ) :   
        setup_postdata($post); ?>

       <li>
          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array(125,125) ); ?></a>
       </li>

     <?php endforeach; 
     wp_reset_postdata(); 
   endif; ?>
</ul>
1
  • 2
    I would use pre_get_posts as this seems to be the main query of the homepage. You should read this post May 28, 2015 at 19:27

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.