The reason you’re getting a white screen is you never defined “$string”. You only concatenated it.
In the future you can find your errors when creating functions by turning wp_debug to true in your wp_config.php file.
Here is code that should work:
function new_function() {
//Arguments: posts, in classifieds category, that are published, and from the past week
$args = array(
'post_type' => array( 'post' ),
'category_name' => 'classifieds',
'post_status' => array( 'publish' ),
'date_query' => array( 'after' => '1 week ago' ),
);
//Query
$new_query = new WP_Query( $args );
if ( $new_query->have_posts() ) {
$string = '<ul>';
while ( $new_query->have_posts() ) {
$new_query->the_post();
$string .= '<li>';
$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
} else {
}
}
wp_reset_postdata();
$string .= '</ul>'
return $string;
}
I would try to give your function a more practical name as well, but when you’re ready to call the function you can do that by (in a php file):
echo new_function();
UPDATED CODE:
function new_function() {
//Arguments: posts, in classifieds category, that are published, and from the past week
$args = array(
'post_type' => array( 'post' ),
'category_name' => 'classifieds',
'post_status' => array( 'publish' ),
'date_query' => array( 'after' => '1 week ago' ),
);
//Query
$new_query = new WP_Query( $args );
$string = '';
if ( $new_query->have_posts() ) {
$string = '<ul>';
while ( $new_query->have_posts() ) :
$new_query->the_post();
$string .= '<li>';
$string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
endwhile;
wp_reset_postdata();
}
$string .= '</ul>'
return $string;
}