Jump to content

[solved] Count # of PDFs in field and echo


louisstephens
 Share

Recommended Posts

So maybe has already figured this out, but I am stumped. I have a field (test_field) that is set to pdfs, and I was trying to get a count of the number of pdfs in that field so I could add it to a status bubble on the front end. I tried:

function testPDF() {
	$a = $page->test_field->count();
	echo "<span class=\"bubble\">" . $a . "</span>";
}

but it is returning "NULL" . I currently have 3 added to the field. I also tried putting this function in _func.php, though I need to use wire('pages'), but I only need to get the count for that specific page, so I am sort of at a loss of how to proceed.

Link to comment
Share on other sites

You have multiple options like :

function a() use($page) {
	count($page->field);
	// or
	$page->field->count();
}
// If you call field from different page, this one is useful
function b($page = null) {
	$page = is_null($page) ? wire('page') : $page;
	count($page->field);
	// or
	$page->field->count();
}
// If your using ProcessWire 3 and $config->useFunctionsAPI = true;
function c() {
	count(page()->field);
	// or
	page()->field->count();
}
// etc...

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...