I have a page URL : https://mwallpaper.ir/mobile-wallpapers/birds

and I grabbed last part of URL with this filter :

add_filter( 'example_filter', 'catslug_callback' );
function catslug_callback( $cat_slug ) {
    preg_match("/[^\/]+$/", $_SERVER['REQUEST_URI'], $matches);
    $cat_slug = $matches[0];
    return $cat_slug;

and also have a query (for some reason in functions.php) to grab attachments from posts of a certain category, that query should get last part of url as category of posts. this is function for that query :

function my_wallpaper_listing( $args, $id ){
    $last_word = apply_filters( 'example_filter', '' );
    $query = new WP_Query( 
        'category_name' => $last_word,
        'posts_per_page' => -1,
        'fields' => 'ids'
    $args['post_type'] = 'attachment';
    $args['post_status'] = 'any';
    $args['post_parent__in'] = $query->posts;
    return $args;
add_filter( 'alm_query_args_wallpaper_listing', 'my_wallpaper_listing', 10, 2);

but this loads all attachments of all posts, and doesn't filter by category.

if I change 'category_name' => $last_word, to 'category_name' => 'birds', its worked, But this function needs to work dynamically (get last part of url as category).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.