Yes you can pass the variable in category_name
. But I think it should be get_the_title()
instead of the_title()
.
$title = get_the_title();
$args = array(
'category_name' => $title
);
Try this. But remember category_name
only accepts category slug, not category name.
You can also use following method if your page title/slug are different. And I think it’s better.
if ( is_page( 'Page Name One' ) ) {
$cat_title="red"; // should be category slug
} elseif ( is_page( 'Page Name Two' ) ) {
$cat_title="yellow"; // should be category slug
} elseif ( is_page( 'Page Name Three' ) ) {
$cat_title="blue"; // should be category slug
}
$args = array(
'category_name' => $cat_title
);