In-text citation %num% format
-
Thanks for your great work, I’m really excited to use this plug-in heavily for a blog I’m launching. I started experimenting with the [zotpressInText] tag and found that when multiple of these tags reference the same item on a page, the value of %num% in a format string evaluates to the same number for all references.
I see that you introduced this feature in 5.0.5 and then added a bug fix in 5.0.6. I see that in 5.0.6 that /lib/shortcode/shortcode.intext.php:145 has the following:
$citation = str_replace("%num%", (count($GLOBALS['zp_shortcode_instances'][get_the_ID()])+1), str_replace("%a%", $item->author, str_replace("%d%", zp_get_year($item->zpdate), $format)));It seems the issue here is that the results being iterated in the surrounding foreach are for a single item and because you’re using this to build up the ‘zp_shortcode_instances’ array in the GLOBALS for later display in the bibliography, the count of this array does not represent the index of the current item.
I played around a bit and with the following order of operations inside of the foreach, I got the functionality to work as I would expect it:
1) Add item into to the zp_shortcode_instances array IFF it’s not already there, adding an attribute representing the index based on the existing size of the array
2) Do all str_replace logic, using the value from the zp_shortcode_instances array for the %num% instance
3) Append this to $zp_intext_citationAmounting to something like:
foreach ($zp_results as $id => $item) { // Shorten author if repeated if ($GLOBALS['zp_shortcode_instances'][get_the_ID()][$item->item_key] && count(explode(",", $item->author)) > 3) $item->author = substr($item->author, 0, strpos($item->author, ",")) . " <em>et al.</em>"; if (!isset($GLOBALS['zp_shortcode_instances'][get_the_ID()][$api_user_id.",".$item->item_key])) { // SET BIBLIOGRAPHY CITATIONS: Per item $GLOBALS['zp_shortcode_instances'][get_the_ID()][$api_user_id.",".$item->item_key] = array( "instance_id" => $zp_instance_id, "userid" => $api_user_id, "account_type" => $zp_account->account_type, "public_key" => $zp_account->public_key, "item_key" => $item->item_key, "author" => $item->author, "title" => $item->title, "date" => zp_get_year($item->zpdate), "download" => $item->download, "image" => $item->image, "json" => $item->json, "citation" => $item->citation, "style" => $item->style, "num" => count($GLOBALS['zp_shortcode_instances'][get_the_ID()])+1 ); } // Deal with pages if ($pages) { $citation = str_replace("%p%", $pages, $citation); } else // New way { if (is_array($items)) { if (count($items) == 2 && !is_array($items[0])) { $citation = str_replace("%p%", $items[1], $citation); } else // Multiple citations { if ($items[$id][1]) $citation = str_replace("%p%", $items[$id][1], $citation); else $citation = str_replace("%p%", "", str_replace(" %p%", "", str_replace(", %p%", "", $citation))); } } else // No pages { $citation = str_replace("%p%", "", str_replace(" %p%", "", str_replace(", %p%", "", $citation))); } } $num = $GLOBALS['zp_shortcode_instances'][get_the_ID()][$api_user_id.",".$item->item_key]['num']; // Fill in author, date and number $citation = str_replace("%num%", $num, str_replace("%a%", $item->author, str_replace("%d%", zp_get_year($item->zpdate), $format))); $zp_intext_citation .= $citation; }
The topic ‘In-text citation %num% format’ is closed to new replies.