Div Module Class: Loop()

Get All Published Entries

The loop() method is part of the DIV_Module class which is used for setting up a custom post type module (CPT Module). This allows for quick implementation of content loops for your custom post types. To start you merely need two lines of code:

$my_cpt = new my_cpt();
$my_cpt->loop();

Above these two lines simply instantiate your CPT class and then call the loop() method without any options passed to the method. By default this will list 10 iterations of your custom post type and display them according to the loop-{cpt}.php within your CPT module. If you want to modify the number of iterations simply pass a number like so:

$my_cpt->loop(3);

Also, rather than passing an integer, you can pass an array of options just as you would to WP_Query directly. Any options you pass will be used to override the WP_Query.

Customize The Content

As for the content there are a couple available options. As mentioned, you can simply edit the loop-{cpt}.php file to get your desired result or perhaps your needs have varying results, simply use the loop_content_{cpt} filter like so:

add_filter( 'loop_content_{cpt}', function($s){
  return DIV_Helper::return_output(__DIR__.'/loop-custom.php');
});

There is also a second optional parameter, by default it is set to TRUE to return the content loop as described above, but if you set it to false then it will just return the query results from WP_Query allowing you to set up a custom loop.

NOTE: By default the page-{cpt}.php within the CPT Module directory uses the loop()

Leave a Reply

Your email address will not be published. Required fields are marked *