Community Forums › Forums › Archived Forums › General Discussion › Trying to insert custom loop – News Child
Tagged: loop, template page, WP_query
- This topic has 1 reply, 1 voice, and was last updated 11 years, 1 month ago by Scottw.
-
AuthorPosts
-
February 5, 2013 at 9:51 pm #18373ScottwMember
Hi,
I'm stuck and need help with getting a custom loop to display in the news theme (on a template page) similar to what I do on my site at http://aviationglossary.com/
I've tried a variety of hooks with no success.If I take the code out of the function the content displays but not in the content area so the loop works but not in the function.
What am I doing wrong? (I'm fairly new to PHP)
Template
get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));if ($postids) {
$args=array(
'post__in' => $postids,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'post',//set this to custom post type if applicable
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);$my_query = null;
$my_query = new WP_Query($args);if( $my_query->have_posts() ) {
?>have_posts()) : $my_query->the_post(); ?>
<a href="" rel="bookmark" title=" definition">
functions.php
/* url paramter*/
add_filter('query_vars', 'parameter_queryvars' );function parameter_queryvars( $qvars )// Function to be able to pick up mychar value at end of url
{
$qvars[] = 'mychar';
return $qvars;
}
February 5, 2013 at 10:03 pm #18376ScottwMemberSorry, the code didn't display.. trying again
<?php
remove_action('genesis_loop', 'genesis_do_loop');
//begin glossary codeadd_action( 'genesis_loop', 'custom_loop' );
function custom_loop() {$first_char = $_GET["mychar"];// gets value of mychar in url
//get all post IDs for posts beginning with letter equal to value of mychar in url , in title order,
//display posts$postids=$wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));if ($postids) {
$args=array(
'post__in' => $postids,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'post',//set this to custom post type if applicable
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);$my_query = null;
$my_query = new WP_Query($args);if( $my_query->have_posts() ) {
?><h3> <?php echo 'Aviation Definitions beginning with the letter '. strtoupper($first_char);?>
</h3><?php
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?> definition"><?php the_title();?></a>
<?phpendwhile;
} else
{
echo 'Sorry, no definitions found beginning with the letter '. strtoupper($first_char) . ', How about adding one?'; ?><?php
}wp_reset_query(); // Restore global post data stomped by the_post().
}
} //end glossarygenesis();
?>
Functions.php file
/* url paramter*/
add_filter('query_vars', 'parameter_queryvars' );function parameter_queryvars( $qvars )// Function to be able to pick up mychar value at end of url
{
$qvars[] = 'mychar';
return $qvars;
} -
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.